Adding Web Search to Langchain Agents using Tavily Search
Table of Contents
Introduction
I’ve been experimenting with building agents using the popular (and mature, with great docs!) framework langchain.
I wanted to understand how to add a web search capability so my agents could search the web to ground their responses in up-to-date information.
How Web Search is Done in Langchain
In langchain, this is done using tools, which as the name suggests, are utilities that the agent can use to perform its task.
Langchain provides a number of search integrations out of the box (correct as of July 2026):
- cloro
- Exa Search
- Google Search
- Linkup Search
- Nia Toolkit
- Nimble Search
- Parallel Search
- Perplexity Search
- Tavily Search
- Apify
- You.com Search
I went for Tavily as I’d heard good things about it and it had free tier, and I was happy with the results.
The Code
Below is a complete working example of creating an agent powered by a Mistral LLM which uses the Tavily Search integration to get results from the web.
Before you run this, you will need to create accounts for (Mistral Studio)[https://console.mistral.ai/] and (Tavily Search)[https://app.tavily.com/home] and create API keys and add some credits (Tavily has a free tier of 1000 searches per month with no credit card required, but Mistral is pay as you go - writing this program costs around £0.20, but the minimum credit amount you can buy is 10 euros, which is about £8.50)
After that, you’ll need to set the following env vars in a .env file:
MISTRAL_API_KEY=your-mistral-api-key
TAVILY_API_KEY=your-tavily-api-key
from datetime import date
from langchain.agents import create_agent
from langchain.tools import tool
from langchain_mistralai import ChatMistralAI
from langchain_tavily import TavilySearch
from dotenv import load_dotenv
# Load API keys from .env into the process environment.
load_dotenv()
# Configure the LLM the agent will use for reasoning and response generation.
llm = ChatMistralAI(
model="mistral-large-latest",
temperature=0,
max_retries=2,
)
# Configure Tavily as the web search tool and limit result count per search.
tavily_search = TavilySearch(max_results=5)
@tool
def get_today_date() -> str:
"""Return today's date in ISO format (YYYY-MM-DD)."""
return date.today().isoformat()
# System instructions guide when to use search and when to use the date tool.
system_prompt = """
You are a helpful assistant that answers user queries.
Use the Tavily search tool to find relevant information to answer the user's question.
Use the get_today_date tool when the user asks about today's date or a date-sensitive request mentions 'today'.
"""
# Create an agent that can call both Tavily and our custom date tool.
agent = create_agent(
model=llm,
tools=[tavily_search, get_today_date],
system_prompt=system_prompt,
)
# Send a user request into the agent; it decides when to call tools.
result = agent.invoke(
{
"messages": [
{
"role": "user",
"content": "Summarise the top 3 headlines for today only in the UK. Include a source, and the date the story is from.",
}
]
}
)
# Grab the final assistant response from the returned message list and print it.
final_message = result["messages"][-1]
print(final_message.text)