Complete Tavily Tutorial: AI Search API for RAG and AI Agents
Tavily is a search API designed specifically for AI applications, Large Language Models (LLMs), and Retrieval-Augmented Generation (RAG). Unlike traditional search APIs like Google Custom Search or Bing API that return lists of URLs, Tavily optimizes search results for direct AI consumption — providing extracted, summarized content ready to be used as LLM context.
In this tutorial, we will learn how to use Tavily from installation, basic usage, integration with LangChain and LlamaIndex, to building powerful RAG applications using Tavily as a real-time knowledge source.
Why Tavily?
When building AI applications that require up-to-date information from the internet, developers face several challenges:
Tavily solves all these problems by providing:
- Pre-extracted and cleaned search results
- Content optimized for LLM consumption
- Support for different search depths (basic and advanced)
- Native integration with popular AI frameworks
- Affordable pricing for production usage
Installation
Python SDK
pip install tavily-python
Node.js SDK
npm install @tavily/core
Getting Your API Key
export TAVILYAPIKEY="tvly-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Or save it in a .env file:
TAVILYAPIKEY=tvly-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Tavily offers a free tier with 1,000 API calls per month, sufficient for development and prototyping.
Basic Usage
Python — Simple Search
from tavily import TavilyClient
client = TavilyClient(apikey="tvly-YOURAPIKEY")
response = client.search("What is Retrieval Augmented Generation?")
for result in response["results"]:
print(f"Title: {result['title']}")
print(f"URL: {result['url']}")
print(f"Content: {result['content'][:200]}...")
print("---")
Response Structure
{
"query": "What is Retrieval Augmented Generation?",
"followupquestions": None,
"answer": None,
"images": [],
"results": [
{
"title": "What is RAG? - Retrieval-Augmented Generation Explained",
"url": "https://example.com/rag-explained",
"content": "Retrieval-Augmented Generation (RAG) is...",
"score": 0.98,
"rawcontent": None
}
],
"responsetime": 1.23
}
Node.js — Simple Search
const { tavily } = require("@tavily/core");
const client = tavily({ apiKey: "tvly-YOURAPIKEY" });
async function search() {
const response = await client.search("Latest developments in AI agents 2026");
response.results.forEach(result => {
console.log(Title: ${result.title});
console.log(URL: ${result.url});
console.log(Score: ${result.score});
console.log("---");
});
}
search();
Search with Advanced Parameters
from tavily import TavilyClient
client = TavilyClient(apikey="tvly-YOURAPIKEY")
response = client.search(
query="machine learning model deployment best practices",
searchdepth="advanced", # "basic" or "advanced"
topic="general", # "general" or "news"
maxresults=10, # number of results (default: 5)
includedomains=["arxiv.org", "huggingface.co"], # domain filter
excludedomains=["pinterest.com"], # exclude domain