How to Integrate Search API with LangChain? — Complete Answer
Integrating a search API with LangChain gives your LLM applications access to real-time information from the web. Whether you are building a RAG pipeline, a research agent, or a customer support chatbot, connecting a search API to LangChain is one of the most practical upgrades you can make. This guide covers the exact steps, code examples, and common pitfalls.
Key Takeaways
- LangChain supports any REST API as a tool through its
ToolandAPIChainabstractions - SearchHive's SwiftSearch API works with LangChain out of the box using a simple HTTP wrapper
- Most search APIs (SerpAPI, Tavily, Brave) have built-in LangChain integrations or community wrappers
- For RAG workflows, combine search results with LangChain's document loaders and vector stores
- SearchHive offers 500 free credits to get started -- no credit card required
How does LangChain work with search APIs?
LangChain abstracts external services into "tools" and "chains." A search API becomes a tool that your LLM agent can call when it needs up-to-date information. LangChain handles the API call, formats the response, and passes results into the LLM context window.
The workflow looks like this:
- User asks a question
- Agent decides whether it needs web data
- Agent calls the search tool (your search API)
- Search results are injected into the prompt
- LLM generates an answer grounded in real data
This pattern is the foundation of most production AI agents today.
How do I integrate SearchHive SwiftSearch with LangChain?
SearchHive's SwiftSearch API returns structured search results from a single REST endpoint. Here is how to wrap it as a LangChain tool:
import requests
from langchain_core.tools import tool
SEARCHHIVE_API_KEY = "your-api-key-here"
@tool
def swift_search(query: str, num_results: int = 5) -> str:
"""Search the web using SearchHive SwiftSearch API.
Returns titles, URLs, and snippets for the top results."""
url = "https://api.searchhive.dev/swift/search"
params = {
"q": query,
"num": num_results,
"api_key": SEARCHHIVE_API_KEY
}
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
results = []
for r in data.get("results", []):
results.append(
f"- {r['title']}\n URL: {r['url']}\n {r['snippet']}"
)
return "\n\n".join(results)
Then use it with a LangChain agent:
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
llm = ChatOpenAI(model="gpt-4o", temperature=0)
agent = initialize_agent(
tools=[swift_search],
llm=llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True
)
response = agent.run(
"What are the latest pricing changes for SerpAPI in 2026?"
)
print(response)
That is it. Two files, one API key, and your agent can search the web.
Can I use SearchHive for RAG with LangChain?
Yes. SearchHive's DeepDive API returns full page content, which you can feed directly into a RAG pipeline:
@tool
def deep_dive(url: str) -> str:
"""Extract full content from a webpage using SearchHive DeepDive."""
api_url = "https://api.searchhive.dev/deepdive/extract"
params = {"url": url, "api_key": SEARCHHIVE_API_KEY}
response = requests.get(api_url, params=params)
response.raise_for_status()
data = response.json()
return data.get("content", "")
Combine SwiftSearch + DeepDive for a search-then-read pipeline:
@tool
def search_and_read(query: str) -> str:
"""Search the web, then read the top result in full."""
search_url = "https://api.searchhive.dev/swift/search"
params = {"q": query, "num": 3, "api_key": SEARCHHIVE_API_KEY}
resp = requests.get(search_url, params=params).json()
if not resp.get("results"):
return "No results found."
top_url = resp["results"][0]["url"]
dive_url = "https://api.searchhive.dev/deepdive/extract"
dive_resp = requests.get(
dive_url,
params={"url": top_url, "api_key": SEARCHHIVE_API_KEY}
).json()
return dive_resp.get("content", "")[:4000]
This pattern lets your agent search for relevant sources, then read the full content before answering. It dramatically improves answer quality compared to search snippets alone.
What about SerpAPI + LangChain?
SerpAPI has an official LangChain integration via langchain-community:
from langchain_community.utilities import SerpAPIWrapper
search = SerpAPIWrapper(serpapi_api_key="your-key")
result = search.run("latest AI news")
The downside: SerpAPI pricing starts at $25/month for just 1,000 searches and jumps to $725/month for 100K searches. At scale, costs add up fast. /compare/serpapi
What about Tavily + LangChain?
Tavily also has a built-in LangChain tool:
from langchain_community.tools.tavily_search import TavilySearchResults
search = TavilySearchResults(max_results=5, tavily_api_key="your-key")
results = search.invoke("Python web scraping best practices")
Tavily charges $0.008 per credit and includes 1,000 free credits per month. Reasonable for light use, but it only does search -- no scraping, no deep extraction. You need a separate tool for that.
Which search API is best for LangChain?
| Feature | SearchHive | SerpAPI | Tavily | Brave Search |
|---|---|---|---|---|
| Free tier | 500 credits | 250 searches | 1,000/mo | $5 credits/mo |
| Price per 1K searches | ~$1.80 | $25-75 | ~$8 | $5 |
| Web scraping | Yes (ScrapeForge) | No | No | No |
| Deep extraction | Yes (DeepDive) | No | No | No |
| LangChain integration | Custom tool (2 lines) | Built-in | Built-in | Custom tool |
| Rate limits | High | Low on starter | Medium | Medium |
SearchHive wins on price and capability breadth. One API key gives you search, scraping, and deep extraction -- all usable as LangChain tools. SerpAPI and Tavily only handle search, so you end up paying for multiple providers.
How do I handle API rate limits in LangChain?
Add retry logic and respect rate limits to avoid getting blocked:
import time
from tenacity import retry, stop_after_attempt, wait_exponential
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=1, max=10))
def safe_swift_search(query: str) -> str:
"""SwiftSearch with automatic retries on rate limits."""
url = "https://api.searchhive.dev/swift/search"
params = {"q": query, "num": 5, "api_key": SEARCHHIVE_API_KEY}
resp = requests.get(url, params=params)
if resp.status_code == 429:
retry_after = int(resp.headers.get("Retry-After", 5))
time.sleep(retry_after)
raise Exception("Rate limited, retrying...")
resp.raise_for_status()
return resp.text
Can I use multiple search APIs as fallbacks?
Yes. A common pattern is primary + fallback:
def multi_source_search(query: str) -> str:
"""Try SearchHive first, fall back to an alternative."""
try:
return swift_search.invoke({"query": query, "num_results": 5})
except Exception:
return brave_search_fallback.invoke({"query": query})
This keeps your agent running even if one provider has an outage.
What are common pitfalls?
- Truncating results too aggressively. LLMs need enough context. Pass at least 3-5 search results or 2,000+ characters of content.
- Not validating API responses. Search APIs can return empty results, HTML errors, or rate limit messages. Always check
response.status_codeand validate the free JSON formatter structure. - Forgetting to set
max_tokens. Long search results eat up your context window. Setmax_tokenson the search tool to cap the output size. - Using search for everything. Not every question needs web search. Let the agent decide via tool descriptions.
Getting started
Set up your free SearchHive account, grab an API key, and drop the @tool decorator into your LangChain app. The SearchHive docs have full endpoint references, and the free tier gives you 500 credits to experiment with -- no credit card required.
If you are comparing providers, check out our SerpAPI alternatives comparison and our guide on building AI research agents.