LangChain
Using Tools
Using Tavily Search Tool
In this guide, you’ll learn how to integrate Tavily—a search API built for large language models (LLMs) and retrieval-augmented generation (RAG)—into your workflows. Tavily provides real-time, up-to-date context by connecting your LLM directly to the web.
What Is Tavily?
Tavily.com offers a fast, persistent search engine that you can call via API. Think of it as search-augmented retrieval, supplying fresh information your LLM can consume to improve accuracy and relevance.
Note
Tavily’s free tier includes 1,000 API calls per month—perfect for experimentation and small projects.
Sign Up and Obtain Your API Key
- Go to Tavily.com and create your account.
- Navigate to the dashboard to view your plan and API usage.
Setting Your API Key
Export your key as an environment variable before running any code.
Operating System | Command |
---|---|
macOS/Linux | export TAVILY_API_KEY="Your Tavily API Key" |
Windows PowerShell | $env:TAVILY_API_KEY="Your Tavily API Key" |
Warning
Never commit your TAVILY_API_KEY
to public repositories. Treat it like a password!
Install the LangChain Community Tool
Make sure you have the required package:
pip install langchain-community
Using TavilySearchResults
in Python
Below is a sample script that queries Tavily and prints out the results:
import os
from langchain_community.tools.tavily_search import TavilySearchResults
# Load your API key from the environment
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
# Initialize the Tavily search tool
tool = TavilySearchResults()
# Perform a search query
response = tool.invoke({"query": "When is ICC Men's T20 World Cup 2024 starting?"})
# Inspect the results
print("Number of results:", len(response))
print("First URL:", response[0]["url"])
print("First snippet:", response[0]["content"])
Response Structure
Tavily returns a list of dictionaries with the following fields:
Field | Description |
---|---|
url | The source link for the search result |
content | A short snippet or summary from the linked page |
Example output:
Number of results: 5
First URL: https://www.espncricinfo.com/series/icc-men-s-t20-world-cup-2024-1411166/match-schedule-fixtures-and-results
First snippet: "Get 2024 T20 World Cup schedule, fixtures, scorecard updates, and results on ESPNcricinfo. Track latest match scores, schedule, and results of ICC Men's T20 World Cup 2024."
Next Steps
- Combine multiple snippets into a single context string for your LLM.
- Explore additional LangChain Community tools for advanced workflows.
- Experiment with different queries to see how real-time search data improves your applications.
Links and References
Watch Video
Watch video content