How to Integrate a Search API with LangChain? Complete Answer
LangChain is the most popular framework for building LLM-powered applications, and adding web search capabilities is one of the first things developers want to do. Whether you are building a RAG pipeline, an AI agent, or a research tool, a reliable search API integration with LangChain gives your application access to real-time, up-to-date information.
This FAQ covers everything you need to know about integrating search APIs with LangChain, using SearchHive SwiftSearch as the primary example.
Q: How do I add a search tool to LangChain?
LangChain provides a Tool abstraction that wraps any API as a callable function. Here is how to wrap a search API:
from langchain_core.tools import tool
import requests
@tool
def web_search(query: str) -> str:
"""Search the web for current information. Use this for any question about recent events, pricing, or facts that may change over time."""
response = requests.post(
"https://api.searchhive.dev/v1/search",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"query": query, "num_results": 5}
)
data = response.json()
results = data.get("results", [])
if not results:
return "No results found."
output = []
for r in results:
output.append(f"- {r.get('title', '')}: {r.get('snippet', '')}")
return "\n".join(output)
Then pass web_search to your agent's tool list:
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
llm = ChatOpenAI(model="gpt-4o")
agent = create_react_agent(llm, [web_search])
result = agent.invoke({"messages": [("user", "What is the current price of Bitcoin?")]})
print(result["messages"][-1].content)
Q: What is the difference between using a search Tool vs. a Retriever?
Search Tools are called by the LLM agent on-demand. The agent decides when to search based on the user's question. Best for open-ended questions where the agent needs to decide what to look up.
Retrievers are called automatically before every LLM invocation. They retrieve relevant documents from a vector store. Best for RAG pipelines where you always want grounded context.
You can combine both: use a Retriever for your knowledge base and a search Tool for real-time web queries.
Q: How do I build a RAG pipeline with web search?
Here is a complete RAG pipeline that searches the web, fetches page content, and generates grounded answers:
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
import requests
SEARCH_API_KEY = "YOUR_API_KEY"
BASE_URL = "https://api.searchhive.dev/v1"
HEADERS = {"Authorization": f"Bearer {SEARCH_API_KEY}"}
def search_and_fetch(query):
"""Search and fetch full page content."""
# Step 1: Search
search_resp = requests.post(
f"{BASE_URL}/search",
headers=HEADERS,
json={"query": query, "num_results": 3}
)
results = search_resp.json().get("results", [])
# Step 2: Fetch top pages
context = []
for r in results[:3]:
url = r.get("url", "")
scrape_resp = requests.post(
f"{BASE_URL}/scrape",
headers=HEADERS,
json={"url": url, "format": "markdown", "render_js": True}
)
content = scrape_resp.json().get("content", "")
context.append(f"## {r.get('title', '')}\n{content[:2000]}")
return "\n\n".join(context)
def web_rag(question):
"""RAG pipeline using web search."""
context = search_and_fetch(question)
prompt = ChatPromptTemplate.from_template(
"Answer the question based on the context below. "
"If the context does not contain enough information, say so.\n\n"
"Context:\n{context}\n\n"
"Question: {question}"
)
chain = prompt | ChatOpenAI(model="gpt-4o") | StrOutputParser()
return chain.invoke({"context": context, "question": question})
# Usage
answer = web_rag("What are the newest features in Python 3.13?")
print(answer)
Q: Can I use search as a LangChain retriever?
Yes. Create a custom retriever class:
from langchain_core.retrievers import BaseRetriever
from langchain_core.documents import Document
class WebSearchRetriever(BaseRetriever):
"""Retriever that uses web search to find relevant documents."""
api_key: str = ""
def _get_relevant_documents(self, query):
response = requests.post(
"https://api.searchhive.dev/v1/search",
headers={"Authorization": f"Bearer {self.api_key}"},
json={"query": query, "num_results": 5}
)
results = response.json().get("results", [])
documents = []
for r in results:
documents.append(Document(
page_content=r.get("snippet", ""),
metadata={"url": r.get("url"), "title": r.get("title")}
))
return documents
# Use it in a RAG chain
retriever = WebSearchRetriever(api_key="YOUR_KEY")
Q: How do I add search to a LangGraph agent?
LangGraph agents can use search tools to make decisions:
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
@tool
def search_web(query: str) -> str:
"""Search the web for current information."""
response = requests.post(
"https://api.searchhive.dev/v1/search",
headers={"Authorization": "Bearer YOUR_KEY"},
json={"query": query, "num_results": 5}
)
results = response.json().get("results", [])
return "\n".join([f"{r['title']}: {r['snippet']}" for r in results])
@tool
def scrape_page(url: str) -> str:
"""Fetch and extract content from a web page."""
response = requests.post(
"https://api.searchhive.dev/v1/scrape",
headers={"Authorization": "Bearer YOUR_KEY"},
json={"url": url, "format": "markdown", "render_js": True}
)
return response.json().get("content", "Failed to fetch page.")
agent = create_react_agent(
ChatOpenAI(model="gpt-4o"),
[search_web, scrape_page],
prompt="You are a research assistant. Search the web and scrape pages to find accurate, up-to-date information."
)
result = agent.invoke({"messages": [("user", "Research the top 5 AI startups in 2026 and summarize their funding")]})
print(result["messages"][-1].content)
Q: Which search APIs work with LangChain?
Most search APIs work with LangChain through the custom Tool pattern above. Here are the popular options:
- SearchHive SwiftSearch -- $9/mo for 5K searches, part of unified platform with scraping and research. Clean REST API, no SDK needed. Get started free.
- Tavily -- $0.008/credit, optimized for AI/RAG use cases. Has a native LangChain integration (
langchain_community.utilities.tavily_search). - SerpAPI -- $25/mo for 1K searches (Google results). Native LangChain integration available.
- Brave Search API -- $5/1K searches. Simple REST API.
- Google Custom Search -- Being deprecated, closed to new customers since 2025.
For new projects, SearchHive offers the best value with search, scraping, and research in one platform.
Q: How do I handle rate limits in a LangChain pipeline?
Add retry logic and rate limiting to your search tool:
import time
from tenacity import retry, stop_after_attempt, wait_exponential
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=10))
@tool
def reliable_search(query: str) -> str:
"""Search with automatic retries and rate limiting."""
try:
response = requests.post(
"https://api.searchhive.dev/v1/search",
headers={"Authorization": "Bearer YOUR_KEY"},
json={"query": query, "num_results": 5},
timeout=10
)
response.raise_for_status()
results = response.json().get("results", [])
return "\n".join([f"{r['title']}: {r['snippet']}" for r in results])
except requests.exceptions.RequestException as e:
return f"Search failed: {str(e)}"
Q: How much does it cost to add search to LangChain?
Cost depends on your search API choice and query volume:
| API | Per-Search Cost | 1K searches/day | 10K searches/day |
|---|---|---|---|
| SearchHive | ~$0.0018 (Starter) | ~$54/mo | ~$540/mo |
| Tavily | $0.008 | ~$240/mo | ~$2,400/mo |
| SerpAPI | $0.025 | ~$750/mo | ~$7,500/mo |
| Brave | $0.005 | ~$150/mo | ~$1,500/mo |
SearchHive is 4-14x cheaper than alternatives at scale.
Q: How do I test search integration without an LLM?
Use LangChain's tool executor directly:
from langchain_core.tools import tool
# Define your search tool (see above)
result = web_search.invoke({"query": "latest Python release"})
print(result)
This lets you verify your search API works correctly before connecting it to an LLM agent.
Summary
Integrating a search API with LangChain is straightforward -- wrap the API call in a @tool decorator and pass it to your agent or retriever. SearchHive SwiftSearch offers the best value for this use case at $9/mo for 5,000 searches, with the added benefit of ScrapeForge (scraping) and DeepDive (research) APIs in the same platform.
Get 500 free credits on SearchHive and start adding web search to your LangChain applications today. No credit card required.