
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
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
- max_results: the default is 10 and the maximum is 20. Tavily suggests 5 for focused answers, which also keeps the prompt short.
- chunks_per_source: 1 to 3 snippets per page (default 3), each roughly 500 characters on basic depth or 800 on advanced.
- topic and dates:
topiccan begeneral,newsorfinance. Usetime_range(day, week, month, year) orstart_date/end_datein YYYY-MM-DD. - Domains:
include_domains(up to 300) andexclude_domains(up to 150). Include works as a hard filter unless you setinclude_domains_mode="prefer". - include_answer: off by default. Turn it on only if you want Tavily's own short LLM answer.
- auto_parameters: may switch the search to advanced, which costs 2 credits. Set
search_depth="basic"to prevent that.
Keep queries short
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
- Search endpoint, Tavily API reference
- Credits & Pricing, Tavily docs
- Best Practices for Search, Tavily docs
- Python Quickstart, Tavily docs
- Pricing, Tavily
- tavily-python README, GitHub
FAQ
Is Tavily free?
The Researcher plan gives 1,000 free credits a month without a credit card.
Basic or advanced search?
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.