Skip to main content
Tavily Search API connects large language models (LLMs) to the web, enabling fast, persistent web search results you can inject as context for retrieval-augmented generation (RAG) workflows. This guide shows how to fetch results from Tavily and use them with an LLM (for example, via LangChain) to create search-augmented prompts or document stores.
The image is a webpage for Tavily AI, promoting a search API that connects large language models to the web for efficient, quick, and persistent search results. It features buttons to open GitHub and join the community.
Overview
  • Use Tavily to perform web searches and collect the returned page snippets and URLs.
  • Store your Tavily API key securely (environment variable recommended) and never commit secrets to source control.
  • Aggregate, chunk, or index the returned content to provide up-to-date, cited context to your LLM.
Getting your API key
  1. Sign up for Tavily and visit your dashboard.
  2. Copy the API key and set it as an environment variable on your machine or deployment environment (example below).
The image shows a web page interface for "Tavily AI" with an "Overview" section displaying a "Researcher" plan, API usage details, and an API key authentication area. There are menu options on the left and a contact button at the bottom.
Tavily often provides a free developer tier (for example, 1,000 calls/month at the time of writing). Store your API key in an environment variable such as TAVILY_API_KEY and avoid hard-coding secrets in source files.
Quick example using the LangChain community tool wrapper
  • The LangChain community package provides a TavilySearchResults wrapper to simplify queries and return structured results.
  • The wrapper typically reads the TAVILY_API_KEY environment variable when initialized.
Setup and usage
  1. Export your API key locally:
  1. Minimal Python usage:
Example of the returned structure
  • The invoke method returns a list of dictionaries. Each dictionary typically includes url and content keys (snippet of the page).
  • Example (trimmed):
Result fields summary Combining and chunking results for RAG
  • The default response often returns five results. Aggregate and chunk content fields, then include the most relevant chunks (with URLs) as context in your LLM prompt.
  • Example: join all content into one string before chunking or indexing.
Best practices and tips
  • Prioritize relevance: sort or filter results by relevance before concatenating content.
  • Chunking: split large combined text into smaller chunks that respect your LLM context window.
  • Citation: always include source URLs in your final output so results are verifiable.
  • Rate limits: respect Tavily’s API rate limits; implement retries and backoff where appropriate.
  • Security: keep API keys in secrets management (environment variables, secret managers, or vaults).
Never commit your TAVILY_API_KEY (or any secret) to version control. Use environment variables or a secrets manager in production to avoid accidental exposure.
Integrations and next steps
  • Use Tavily results directly in prompts for short answers (with citations).
  • For larger systems, index the returned snippets into a vector store and perform semantic retrieval before calling your LLM.
  • Integrate the tool into a LangChain toolset or agent to automate search-and-answer workflows.
Further reading and references Key takeaways
  • Tavily provides search results optimized for LLM-driven RAG workflows.
  • Store your API key securely and use the TavilySearchResults wrapper to fetch structured results.
  • Aggregate, chunk, and cite returned content and url fields when constructing LLM context for accurate, up-to-date answers.

Watch Video