opncrafter

Search for Agents: Tavily

Dec 29, 2025 • 18 min read

RAG solves the stale knowledge problem for documents you own. But what about the live web? LLMs have a knowledge cutoff and no internet access by default. Tavily is a search API built specifically for AI agents — it handles scraping, JavaScript rendering, and content extraction so your agent receives clean, LLM-ready text rather than raw HTML soup that needs parsing.

1. Why Standard Search APIs Fall Short for Agents

When an agent searches "NVIDIA Q4 earnings" using Google's Custom Search API or Bing, it receives:

  • 10 URLs with 160-character snippets
  • No actual page content — just metadata
  • The agent then needs to call each URL, handle redirects, block cookies, deal with paywalls, parse HTML, and extract the relevant text
  • This adds 3-5 extra tool calls per search query and 5-10 seconds of latency

Tavily does all of that for you in a single API call and returns structured, clean excerpts ready to inject directly into an LLM context window.

2. Quick Start

pip install tavily-python

# .env
TAVILY_API_KEY=tvly-...  # Get free key with 1,000 req/month at app.tavily.com

from tavily import TavilyClient

client = TavilyClient(api_key="tvly-...")

# Basic search
results = client.search(
    query="NVIDIA Q4 2024 earnings results",
    search_depth="basic",   # "basic" (fast) or "advanced" (thorough)
    max_results=5,
    include_domains=[],     # Optional: restrict to specific sites
    exclude_domains=[],     # Optional: exclude sites
)

for result in results["results"]:
    print(f"Source: {result['url']}")
    print(f"Title: {result['title']}")
    print(f"Content: {result['content'][:300]}")
    print(f"Score: {result['score']}")  # Relevance score 0-1
    print()

3. Search Modes Comparison

FeatureBasic SearchAdvanced Search
Cost1 API credit2 API credits
Speed~1 second~3-5 seconds
Content depthSnippet-levelFull page extraction
JavaScript sitesLimitedFull JS rendering
Best forQuick fact lookups, news headlinesComprehensive research, technical docs

4. LangChain Integration

Tavily is a first-class tool in LangChain and LangGraph — use it in agents with one import:

from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

# Initialize with advanced search for better results
tavily_tool = TavilySearchResults(
    max_results=5,
    search_depth="advanced",
    include_answer=True,    # Get a synthesized direct answer in addition to sources
    include_raw_content=False,  # Raw page HTML (usually not needed)
    include_images=False,
)

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

# Create a ReAct agent with Tavily as its search tool
agent = create_react_agent(llm, [tavily_tool])

# The agent will automatically use Tavily when it needs real-time info
result = agent.invoke({
    "messages": [("user", "What is the current price of NVDA stock and what happened to it this week?")]
})

print(result["messages"][-1].content)

5. Advanced API Features

# Feature 1: Get a direct AI-synthesized answer (not just sources)
results = client.search(
    query="What is the current federal funds rate?",
    include_answer=True
)
print(results["answer"])  # "The Federal Reserve's federal funds rate target range is 4.25-4.50%..."

# Feature 2: Restrict to specific high-quality domains
results = client.search(
    query="React 19 new features",
    include_domains=["react.dev", "github.com", "blog.react.dev"],
)

# Feature 3: Get full page content for deep research
results = client.search(
    query="GPT-4o technical report methodology",
    search_depth="advanced",
    include_raw_content=True,  # Returns full extracted markdown of each page
    max_results=3,
)
for r in results["results"]:
    print(f"Full content ({len(r['raw_content'])} chars): {r['raw_content'][:500]}")

# Feature 4: Context-ready format for RAG injection
context = "\n\n".join([
    f"Source ({r['url']}):\n{r['content']}"
    for r in results["results"]
])
# Inject context directly into your LLM prompt

6. Usage Patterns for Agentic Systems

Different agent architectures use Tavily differently:

# Pattern 1: Iterative research (search → read → search more)
def deep_research(topic: str, max_iterations: int = 3) -> str:
    all_findings = []
    queries = [topic]  # Start with main query
    
    for i, query in enumerate(queries[:max_iterations]):
        results = client.search(query, search_depth="advanced", max_results=3)
        
        for r in results["results"]:
            all_findings.append(r["content"])
        
        # Ask GPT to generate follow-up queries based on findings
        follow_up = generate_follow_up_queries(topic, all_findings)
        queries.extend(follow_up)
    
    return "\n\n".join(all_findings)

# Pattern 2: Fact verification (search to confirm LLM claims)
async def verify_claim(claim: str) -> dict:
    search_result = client.search(
        query=f"fact check: {claim}",
        include_answer=True
    )
    return {
        "claim": claim,
        "verified": search_result["answer"],
        "sources": [r["url"] for r in search_result["results"]],
    }

# Pattern 3: News monitoring (search for recent events)
def monitor_topic(topic: str, days_ago: int = 7) -> list:
    return client.search(
        query=f"{topic} news last {days_ago} days",
        search_depth="basic",
        max_results=10,
    )["results"]

7. Cost Optimization

  • Cache results: Store Tavily results in Redis with a 1-hour TTL. Identical queries from different users within the hour reuse the cached result.
  • Use basic for real-time queries: Current stock prices, breaking news, weather — basic search is sufficient and twice as fast.
  • Use advanced for research tasks: Technical documentation, analyst reports, academic content — worth the extra credit.
  • Limit max_results: 3-5 results is usually enough. 10 results costs the same but adds token overhead.
  • Free tier: 1,000 requests/month. At 3-5 searches per agent session, that's 200-333 free agent interactions per month.

Frequently Asked Questions

How does Tavily compare to Serper or SerpAPI?

Serper and SerpAPI return Google search metadata (title, snippet, URL) — you still need to scrape and parse the actual page content yourself. Tavily returns extracted, clean text from the pages. For agents, Tavily's all-in-one approach is 3-5x fewer lines of code and significantly lower latency.

Can Tavily access paywalled content?

For most paywalls, no — Tavily sees the same public content your browser would see without a subscription. Exceptions: some sites expose full content via their API or SEO-accessible pages that Tavily can access. For critical paywalled sources, use domain-specific APIs (Bloomberg API, Reuters API) alongside Tavily.

Conclusion

Tavily is currently the most developer-friendly way to give AI agents real-time internet access. Its API eliminates the scraping, parsing, and HTML-cleanup work that would otherwise add complexity and latency to your agent loop. Combined with LangChain or LangGraph, it takes one import to create an agent that can answer questions about the live web — a capability that transforms LLMs from static knowledge bases into dynamic research tools.

Continue Reading

👨‍💻
Written by

Vivek

AI Engineer

Full-stack AI engineer with 4+ years building LLM-powered products, autonomous agents, and RAG pipelines. I've shipped AI features to production for startups and worked hands-on with GPT-4o, LangChain, LlamaIndex, and the Vercel AI SDK. I started OpnCrafter to share everything I wish I had when learning — no fluff, just working code and real-world context.

GPT-4oLangChainNext.jsVector DBsRAGVercel AI SDK