How to Add Live Web Search to an LLM with Tavily API
Tavily is a search API built for AI agents rather than people. You send a query, it returns extracted page content ranked for relevance, and you drop that text into your model’s context. One endpoint, about ten lines of Python.
The difference from scraping Google yourself is that Tavily handles extraction, deduplication, and truncation so the output fits a context window.
How do you make the first API call?
Sign up, grab an API key from the dashboard, install the SDK.
pip install tavily-python
Then a minimal search:
from tavily import TavilyClient
client = TavilyClient(api_key="tvly-...")
r = client.search("EU AI Act compliance deadlines", max_results=5)
for item in r["results"]:
print(item["title"], item["url"])
print(item["content"][:400])
Each result carries a title, URL, a content snippet, and a relevance score. That snippet is the useful part — it is already cleaned of navigation junk that would otherwise eat your tokens.
Which parameters actually change quality?
The API has a lot of knobs. Four matter.
| Parameter | Effect | Recommendation |
|---|---|---|
search_depth |
basic is fast, advanced runs more retrieval passes |
Use advanced for research tasks, basic for chat |
max_results |
Number of sources returned | 5 for answering, 10–20 for reports |
include_domains |
Whitelist of sites | Restrict when factual accuracy matters |
include_raw_content |
Returns full page text instead of snippets | Use it, then chunk — snippets can clip mid-claim |
Switching from basic to advanced costs more credits per call but measurably improves results on multi-hop questions — the kind where the answer is spread over three pages.
Setting include_raw_content=True and doing your own chunking beats relying on snippets when the model needs to quote precisely. Snippets get cut at fixed lengths regardless of sentence boundaries.
How much does the Tavily API cost?
There is a free tier with a monthly credit allowance — enough for development and light use. Paid plans scale with credits, and advanced searches consume more than basic ones.
Do the math before you ship. A chatbot that searches on every message at 5 results and basic depth is cheap. An agent that runs 12 searches per task at advanced depth is not, and the bill arrives a month after the traffic does.
Two cost controls worth building in from day one: cache identical queries for 24 hours, and cap searches per conversation turn.
How do you wire it into an LLM call?
Pattern that works with any model:
- Take the user question.
- Send it to Tavily with
max_results=5. - Concatenate the top results as numbered sources in the system prompt.
- Instruct the model to cite source numbers inline and to say “not found” if the sources do not cover the question.
- Return both the answer and the source list to the user.
Step 4 is where answers stop being confident nonsense. Without an explicit “say you don’t know” instruction, models fill gaps from training data and the citations become decoration.
Also worth adding: a relevance floor. Drop results scoring below about 0.4 before they reach the model. Low-scoring results add tokens and confusion in equal measure.
Where does it break?
- Paywalled sources come back as snippet-only. No full content, and the snippet may be the marketing blurb.
- Recency depends on the underlying index. Confirming that a page is current is still your job.
- Small sites sometimes get skipped entirely, even when they hold the exact answer.
- Structured data — tables, PDFs — extracts unevenly. Formulas and grids lose their shape.
For financial filings or scientific tables, add a PDF parser or hit the source directly. The search API is a router, not a data extraction pipeline.
Who is this for?
Developers building research agents, RAG systems needing fresh content, and support bots that must answer from current documentation rather than a six-month-old embedding index.
Not for: teams that just need a chat UI (use a hosted assistant), or anyone who cannot monitor API spend weekly.
Quick Summary
- Tavily is a search API returning LLM-ready extracted content — roughly ten lines of Python to wire in.
- Use
search_depth=advancedfor research andbasicfor chat; credits differ substantially. - Set
include_raw_content=Trueand chunk yourself when the model must quote precisely. - Add a “say you don’t know” instruction and a 0.4 relevance floor or citations turn decorative.
- Free tier covers development; production cost scales with searches per task, so cache aggressively.
