How to Add Web Search to Your LangChain Agent with Keiro

A complete guide to integrating Keiro's AI search API as a LangChain tool — with agent code, streaming, and multi-tool examples.

7 min readKeiro Team

LangChain agents need web search to answer questions about current events, look up documentation, and ground their responses in facts. While Tavily is LangChain's default search tool, it costs $4 per 1,000 queries — and you can get the same results with Keiro at a fraction of the price.

This guide shows you how to set up Keiro as a LangChain tool in under 5 minutes.

Install Dependencies

pip install langchain langchain-openai requests

Create the Keiro Search Tool

import requests
from langchain.tools import Tool

KEIRO_API_KEY = "your_keiro_key"

def keiro_search(query: str) -> str:
    """Search the web using Keiro API."""
    response = requests.post(
        "https://kierolabs.space/api/v2/keiro",
        headers={"Authorization": f"Bearer {KEIRO_API_KEY}"},
        json={"query": query, "maxResults": 5}
    )
    results = response.json()["results"]
    return "\n\n".join([
        f"**{r['title']}**\n{r['snippet']}\nSource: {r['url']}"
        for r in results
    ])

def keiro_content_search(query: str) -> str:
    """Search the web and extract page content for deep research."""
    response = requests.post(
        "https://kierolabs.space/api/v2/search/content",
        headers={"Authorization": f"Bearer {KEIRO_API_KEY}"},
        json={"query": query, "maxResults": 3, "mode": "medium"}
    )
    results = response.json()["results"]
    return "\n\n---\n\n".join([
        f"**{r['title']}** ({r['url']})\n{r['content'][:3000]}"
        for r in results
    ])

# Create LangChain tools
search_tool = Tool(
    name="web_search",
    func=keiro_search,
    description="Search the web for current information. Use for quick lookups, recent events, and factual queries."
)

deep_search_tool = Tool(
    name="deep_research",
    func=keiro_content_search,
    description="Search the web and extract full page content. Use when you need detailed information, tutorials, or comprehensive answers."
)

Build the Agent

from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful research assistant. Use web search to find current, accurate information. Always cite your sources."),
    ("human", "{input}"),
    MessagesPlaceholder("agent_scratchpad"),
])

agent = create_openai_tools_agent(llm, [search_tool, deep_search_tool], prompt)
executor = AgentExecutor(agent=agent, tools=[search_tool, deep_search_tool], verbose=True)

# Run it
result = executor.invoke({"input": "What are the latest AI search APIs and how do they compare on pricing?"})
print(result["output"])

Why Keiro Over Tavily for LangChain?

FeatureTavily (built-in)Keiro (custom tool)
Setup time0 min (built-in)5 min
Cost per 1K queries$4.00$0.50
Search + contentSeparate callsSingle call (medium mode)
Speed tiers13 (flash/fast/keiro)
Batch for datasetsNoYes (10K/job)
Monthly cost at 50K queries$200$25

Tavily saves 5 minutes of setup. Keiro saves $175/month at 50K queries. For any production agent, the custom tool pays for itself on day one.

Tips for Production

  • Use /search/flash for agent loops where latency matters (500ms vs 1s)
  • Use /search/content medium mode when the agent needs to read full articles
  • Use /search/batch for offline evaluation and dataset generation
  • Cache frequent queries to reduce credit usage

Keiro's /keiro and /search/fast endpoints cost 0.5 credits each — the Essential plan ($15/mo, 5K credits) gives you 10,000 agent search calls per month. Start free with 300 credits at platform.keirolabs.cloud.

Ready to build something?

Join developers using Keiro — 10× cheaper with superior performance.

Get started