Web Search Tool
Search the internet for current information using Brave Search.
Overview
The web search tool lets AI access real-time information from the internet. It uses Brave Search for fast, accurate results.
Configuration
Set up Brave Search API key:
# In .env file
BRAVE_SEARCH_API_KEY=your-brave-search-api-key
Get your API key from Brave Search.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
query | string | Yes | - | Search query text |
count | integer | No | 5 | Number of results |
time_range | string | No | no_limit | Time filter: day, week, month, year |
Usage Examples
Basic Search
import requests
response = requests.post(
"http://localhost/api/v1/agent/tool-calling",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"tool_name": "web_search",
"parameters": {
"query": "latest AI developments 2024",
"count": 5
}
}
)
results = response.json()["results"]
for result in results:
print(f"Title: {result['title']}")
print(f"URL: {result['url']}")
print(f"Snippet: {result['snippet']}\n")
Search with Time Filter
response = requests.post(
"http://localhost/api/v1/agent/tool-calling",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"tool_name": "web_search",
"parameters": {
"query": "tech news",
"count": 10,
"time_range": "week"
}
}
)
results = response.json()["results"]
print(f"Found {len(results)} results from the past week")
Search for Current Events
response = requests.post(
"http://localhost/api/v1/agent/tool-calling",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"tool_name": "web_search",
"parameters": {
"query": "today's stock market",
"count": 5,
"time_range": "day"
}
}
)
Use in Agent
# Create agent with web search
response = requests.post(
"http://localhost/api/v1/agent/configs",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"name": "research_agent",
"system_prompt": "You are a research assistant. Use web search to find current information and real-time data.",
"model": "gpt-4",
"builtin_tools": ["web_search"],
"tool_config": {
"web_search": {
"count": 10,
"time_range": "week"
}
},
"tool_choice": "auto",
"max_iterations": 3
}
)
agent_id = response.json()["id"]
# Chat with agent
chat_response = requests.post(
"http://localhost/api/v1/agent/chat",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={
"agent_id": agent_id,
"message": "What are the latest developments in AI?"
}
)
result = chat_response.json()
print(f"Response: {result['response']}")
print(f"Sources used: {[t.get('result', {}).get('url', '') for t in result.get('tool_calls', [])]}")
JavaScript Usage
const response = await fetch('http://localhost/api/v1/agent/tool-calling', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
tool_name: 'web_search',
parameters: {
query: 'latest AI developments',
count: 5,
time_range: 'week'
}
})
});
const results = await response.json();
results.results.forEach(result => {
console.log(`Title: ${result.title}`);
console.log(`URL: ${result.url}`);
console.log(`Snippet: ${result.snippet}\n`);
});
cURL Usage
curl -X POST http://localhost/api/v1/agent/tool-calling \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"tool_name": "web_search",
"parameters": {
"query": "AI regulation news 2024",
"count": 10,
"time_range": "month"
}
}'
Response Format
{
"results": [
{
"title": "Latest AI Regulations in 2024",
"url": "https://example.com/ai-regulations",
"snippet": "Breaking news about AI regulations...",
"published_date": "2024-01-15"
}
]
}
Time Range Options
| Time Range | Description |
|---|---|
day | Results from the last 24 hours |
week | Results from the last 7 days |
month | Results from the last 30 days |
year | Results from the last 365 days |
no_limit | No time filter (default) |
Use Cases
Research
# Research recent developments
query = "AI regulation developments 2024"
time_range = "month"
Current Events
# Get today's news
query = "breaking news today"
time_range = "day"
Trending Topics
# Find trending topics
query = "trending technology topics"
time_range = "week"
Price Updates
# Check current prices
query = "iPhone 15 price today"
time_range = "day"
Best Practices
Query Quality
| Good Queries | Poor Queries |
|---|---|
| "What are the latest AI regulations?" | "AI regulations" |
| "Current stock price of AAPL" | "AAPL price" |
| "Breaking news about tech today" | "tech news" |
| "Recent developments in quantum computing" | "quantum computing" |
Time Range Selection
- Use
dayfor time-sensitive queries (prices, breaking news) - Use
weekfor recent developments - Use
monthfor broader research - Use
no_limitfor general queries
Result Count
- Use 3-5 for quick answers
- Use 10-15 for comprehensive research
- Use 20-30 for extensive research
Troubleshooting
API Key Not Configured
Problem: {"error": "BRAVE_SEARCH_API_KEY not configured"}
Solution:
- Set
BRAVE_SEARCH_API_KEYin .env file - Restart Enclava service
- Verify API key is valid
Rate Limiting
Problem: Too many search requests
Solution:
- Reduce search frequency
- Implement caching
- Upgrade API tier if needed
- Use longer time ranges
No Relevant Results
Problem: Search returns irrelevant results
Solution:
- Refine query terms
- Add more specific keywords
- Try different time range
- Increase result count
Best Practices
- Be specific - Use detailed, precise queries
- Set time range - Use appropriate time filters
- Limit results - Don't fetch unnecessary data
- Cache results - Store frequent searches
- Monitor usage - Track API costs and quota
Next Steps
- RAG Search Tool - Search your documents
- Code Execution Tool - Run code securely
- Agent Documentation - Use web search in agents