Complete Guide to LlamaIndex Web Search: Tools, Setup, and Best Practices
LlamaIndex is the leading framework for building LLM applications with context augmentation. One of the most common requirements for LlamaIndex applications is web search -- giving your agents and RAG pipelines access to real-time web data.
This guide covers how to integrate web search into LlamaIndex, the available tools, code examples, and practical recommendations for production deployments.
Background
LlamaIndex provides a framework for building context-augmented LLM applications. It handles data ingestion, indexing, retrieval, and agent orchestration. But out of the box, LlamaIndex works with your own documents and databases. When you need real-time web data -- current news, competitor information, live pricing -- you need to add web search capabilities.
LlamaIndex supports this through its Tool system. You define tools that your agent can call, and the framework handles the orchestration. The challenge is choosing the right search API and integrating it effectively.
Challenge: Choosing a Web Search Tool for LlamaIndex
LlamaIndex doesn't include a built-in web search API. Instead, it provides integration patterns -- you bring your own search provider and wrap it as a LlamaIndex Tool or QueryEngine.
The main options for the search backend:
- SearchHive -- Search, scraping, and AI research in one API. $0.0001/credit, 500 free credits.
- Tavily -- AI-optimized search, $0.008/search, 1K free/mo.
- SerpAPI -- Google search API, $50/mo for 5K searches.
- Brave Search API -- $5/1K searches, $5 free credits/mo.
- Google Custom Search -- Deprecated for new customers (being shut down Jan 2027).
Solution: SearchHive as a LlamaIndex Web Search Tool
SearchHive works well with LlamaIndex for three reasons:
- Three APIs in one -- SwiftSearch for search, ScrapeForge for page extraction, DeepDive for research
- Markdown output -- ScrapeForge returns markdown, which LlamaIndex's document loaders handle natively
- Cost-effective -- At $0.0001/credit, it's significantly cheaper than alternatives
Implementation
Step 1: Install Dependencies
pip install llama-index-core llama-index-openai searchhive-client requests
Step 2: Create SearchHive Tools for LlamaIndex
from llama_index.core.tools import FunctionTool
import requests
import json
SEARCHHIVE_API_KEY = "your-key"
SEARCHHIVE_BASE = "https://api.searchhive.dev/v1"
HEADERS = {"Authorization": f"Bearer {SEARCHHIVE_API_KEY}"}
def web_search(query: str, num_results: int = 5) -> str:
"""Search the web for current information using SearchHive SwiftSearch.
Returns formatted search results with titles, snippets, and URLs.
"""
resp = requests.get(f"{SEARCHHIVE_BASE}/swiftsearch", headers=HEADERS, params={
"q": query,
"engine": "google",
"num": num_results
})
results = resp.json().get("organic", [])
if not results:
return f"No results found for: {query}"
output = []
for i, r in enumerate(results, 1):
output.append(f"{i}. {r['title']}\n {r.get('snippet', 'No snippet')}\n URL: {r['url']}")
return "\n\n".join(output)
def scrape_webpage(url: str) -> str:
"""Extract clean content from a web page using SearchHive ScrapeForge.
Returns markdown-formatted page content.
"""
resp = requests.post(f"{SEARCHHIVE_BASE}/scrapeforge", headers={
**HEADERS,
"Content-Type": "application/json"
}, json={
"url": url,
"format": "markdown"
})
data = resp.json()
content = data.get("content", "")
if not content:
return f"Failed to scrape: {url}"
return content[:5000]
def deep_research(query: str) -> str:
"""Perform AI-powered deep research using SearchHive DeepDive.
Synthesizes information from multiple web sources into a comprehensive summary.
"""
resp = requests.post(f"{SEARCHHIVE_BASE}/deepdive", headers={
**HEADERS,
"Content-Type": "application/json"
}, json={
"query": query,
"max_results": 10
})
data = resp.json()
summary = data.get("summary", "No summary generated.")
sources = data.get("sources", [])
output = f"Research Summary:\n{summary}"
if sources:
output += "\n\nSources:\n"
for s in sources[:5]:
output += f"- {s.get('title', s)}\n"
return output
# Create LlamaIndex FunctionTool instances
search_tool = FunctionTool.from_defaults(
fn=web_search,
name="web_search",
description="Search the web for current information. Use for questions about recent events, current prices, news, or anything that requires up-to-date data."
)
scrape_tool = FunctionTool.from_defaults(
fn=scrape_webpage,
name="scrape_webpage",
description="Extract full content from a specific URL as clean markdown. Use when you need detailed information from a page found in search results."
)
research_tool = FunctionTool.from_defaults(
fn=deep_research,
name="deep_research",
description="Perform deep research on a topic, synthesizing from multiple sources. Use for complex analysis, comparisons, or when simple search results aren't sufficient."
)
Step 3: Build a LlamaIndex Agent with Web Search
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI
# Initialize LLM
llm = OpenAI(model="gpt-4o", temperature=0)
# Create agent with search tools
agent = ReActAgent.from_tools(
[search_tool, scrape_tool, research_tool],
llm=llm,
verbose=True,
system_prompt=(
"You are a research assistant with access to real-time web data. "
"Use web_search to find relevant sources, scrape_webpage to get "
"detailed content from specific pages, and deep_research for "
"comprehensive analysis. Always cite your sources."
),
max_iterations=10
)
# Query the agent
response = agent.chat("What are the latest features released by OpenAI in 2025?")
print(response)
Step 4: Build a Web-Enhanced RAG Pipeline
Combine web search with your own document index for the best of both worlds -- internal knowledge + real-time web data.
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, QueryEngineTool
from llama_index.core.query_engine import SubQuestionQueryEngine
# Load your own documents
documents = SimpleDirectoryReader("./data").load_data()
local_index = VectorStoreIndex.from_documents(documents)
local_query_engine = local_index.as_query_engine()
# Create a query engine tool from the local index
local_tool = QueryEngineTool.from_defaults(
query_engine=local_query_engine,
name="internal_knowledge",
description="Search internal company documents and knowledge base."
)
# Combine local + web tools
hybrid_agent = ReActAgent.from_tools(
[local_tool, search_tool, scrape_tool, research_tool],
llm=llm,
verbose=True,
system_prompt=(
"You have access to internal company knowledge and real-time web data. "
"Check internal knowledge first, then use web search for anything "
"current or not in the internal documents."
),
max_iterations=10
)
# The agent routes to the right data source automatically
response = hybrid_agent.chat("What is our refund policy and how does it compare to competitors?")
print(response)
Alternative: LlamaIndex Web Search with Tavily
If you prefer Tavily's AI-optimized search, the integration is similar:
TAVILY_API_KEY = "your-tavily-key"
def tavily_search(query: str) -> str:
"""Search using Tavily API."""
resp = requests.post("https://api.tavily.com/search", json={
"api_key": TAVILY_API_KEY,
"query": query,
"max_results": 5
})
results = resp.json().get("results", [])
return "\n".join(f"- {r['title']}: {r['content']}" for r in results)
tavily_tool = FunctionTool.from_defaults(
fn=tavily_search,
name="web_search",
description="Search the web for current information using Tavily."
)
Note: Tavily only provides search. You'd still need a separate scraping tool (like ScrapeForge) for extracting full page content. SearchHive gives you both in one API.
Comparison: Web Search Options for LlamaIndex
| Provider | Search | Scrape | AI Research | Free Tier | Per-Query Cost |
|---|---|---|---|---|---|
| SearchHive | Yes | Yes | Yes | 500 credits | $0.0001 |
| Tavily | Yes | No | No | 1K/mo | $0.008 |
| SerpAPI | Yes | No | No | 100/mo | $0.005-$0.01 |
| Brave Search | Yes | No | No | $5/mo credit | $0.005 |
SearchHive is 80x cheaper than Tavily per query and includes scraping and research that Tavily doesn't offer. For LlamaIndex applications that need both search and content extraction, SearchHive eliminates the need for a second API.
Results
Integrating SearchHive with LlamaIndex gives you:
- Real-time web search in your RAG pipelines and agents
- Full page extraction for detailed content analysis
- AI-powered research synthesis for complex queries
- Unified API -- one key, one SDK, one billing relationship
- Significant cost savings compared to using multiple search and scraping APIs
Lessons Learned
-
Markdown output matters. LlamaIndex's document loaders work best with markdown. SearchHive ScrapeForge returns markdown natively, making integration seamless.
-
Use DeepDive for complex queries. Simple search works for "what is X" questions, but DeepDive produces much better results for "compare X and Y" or "analyze the market for Z."
-
Truncate long content. LLMs have context limits. ScrapeForge returns full page content -- truncate to the most relevant sections before passing to LlamaIndex.
-
Cache search results. If your agent searches for the same topic repeatedly, cache results in a simple dict or Redis. SearchHive credits are cheap, but not free.
-
Use ReActAgent for complex workflows. The ReAct pattern (Reason + Act) lets the agent decide when to search, scrape, or use internal knowledge. This is more flexible than a simple RAG pipeline.
-
Start with the free tier. 500 credits is enough to build and test your integration. Scale up to the Builder plan ($49/mo, 100K credits) when you go to production.
Get started with 500 free credits at searchhive.dev/pricing -- no credit card required. Check the LlamaIndex docs for framework details and the SearchHive docs for API reference. See also /blog/searchhive-vs-tavily-for-ai-search and /blog/how-to-ai-agent-realtime-data-access-step-by-step.