Man looking through binoculars across an open field, like an AI agent searching the web with the Tavily Search API
Photo by Annie Spratt on Unsplash · source · credits

Tavily Search API Tutorial: Add Web Search to an AI Agent Without Wasting Credits

Also available in Hindi: यह लेख हिंदी में पढ़ें

Your agent answers questions about last week with facts from its training data. A search tool fixes that, but a careless one burns credits and floods the prompt with text. Here is a lean Tavily Search setup in Python.

What a search costs

Tavily Search credit cost by setting
From Tavily docs and tavily.com/pricing. Illustration, not a screenshot.

The free Researcher plan gives 1,000 API credits a month, with no credit card. Credits reset on the first day of each month. A basic, fast or ultra-fast search costs 1 credit. An advanced search costs 2. Pay-as-you-go is $0.008 per credit.

Build the tool

pip install tavily-python
import os
from tavily import TavilyClient

tavily = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])

def web_search(query: str) -> str:
    r = tavily.search(
        query,
        search_depth="basic",
        max_results=5,
        topic="news",
        time_range="week",
        include_usage=True,
    )
    return "\n\n".join(
        f"{x['title']} ({x['url']})\n{x['content']}" for x in r["results"]
    )

Register web_search as a tool in your agent framework. Each result has a title, url, content snippet and a relevance score, so the model can cite its sources.

Tune the settings

Keep queries short

Agent search flow with Tavily: query, search, results, answer, extract
From Tavily docs (Search API, Best Practices, Python SDK). Illustration, not a screenshot.

Tavily's best-practices page asks for queries under 1,500 characters. Split a complex question into a few short sub-queries. When the agent needs a full page, call extract() on the chosen URL. A basic extract costs 1 credit per 5 successful URLs, and failed URLs are not charged.

Watch for errors too. A 429 means you sent requests too fast: wait for the Retry-After header. A 432 means you hit your plan's limit.

Next, read how to build an AI agent, add search to a CrewAI JSON crew, browse the best MCP servers for Claude Code, or visit the Tavily tool page.

Sources

FAQ

Is Tavily free?

The Researcher plan gives 1,000 free credits a month without a credit card.

Start with basic (1 credit). Use advanced (2 credits) for high-precision questions.

How many results should my agent get?

Around 5 for a focused answer. The default is 10 and the maximum is 20.

Can I limit results to certain websites?

Yes. Pass include_domains to restrict results, or exclude_domains to drop sites.

About the author

Suraj Kumar builds and edits AIUseFlow, a directory of AI tools for vibe coders, agent builders and creators. Guides like this one walk through a practical workflow with cited sources — not a news wire rewrite. How we rank tools.

Get the weekly stack

Rank movers and new AI tools for builders, once a week. Free, no spam, unsubscribe anytime.