AgentsLesson 8 of 9
Search Tool
Calculator Tool kept math in Python. Search goes the other way: call Tavily when the answer is not in your indexed docs.
Outside your index
Your RAG chatbot only sees chunks you loaded. "Who hosted Euro 2024?" is not in an HTML cheat sheet. Point the agent at TavilySearch and let it pull web snippets into the message list.
RAG index
your HTML notes (no Euro 2024)
tavily_search
query → web hits titles + snippets agent reads JSON
TavilySearch
Built-in tool from langchain-tavily, not a custom @tool. One required arg: query.
max_results=3 limits hits. The agent registers it as tavily_search.
from langchain_tavily import TavilySearch search = TavilySearch(max_results=3) # Registered name: tavily_search # Required arg: query
When search runs:
user: Who hosted Euro 2024?
↓
tavily_search("Euro 2024 host nation")
↓
tool: JSON with titles, URLs, snippets
↓
ai: Germany hosted UEFA Euro 2024.
create_agent
tools=[search]. Nothing else changes from the last few lessons.
from langchain.agents import create_agent
agent = create_agent(model="openai:gpt-4o-mini", tools=[search])
result = agent.invoke({
"messages": [{"role": "user", "content": "Who hosted UEFA Euro 2024? Use search."}],
})
print(result["messages"][-1].content)Run the demo
Venv from Project Setup. Install the package, download, then:
pip install langchain-tavily
search_tool_demo.py
Euro 2024 host, then winner
OPENAI_API_KEY same as Tool Calling. Add TAVILY_API_KEY from tavily.com (free tier).◇search_tool_demo.py
"""search_tool_demo.py"""
from langchain_tavily import TavilySearch
# TavilySearch(max_results=3), tools=[search]
python search_tool_demo.py
PowerShell — (.venv) active
(.venv) PS C:\projects\langchain-course> python search_tool_demo.py
Question: Who hosted UEFA Euro 2024? Use the search tool.
search returned: {"query": "Euro 2024 host nation", "results": [{"title": "UEFA Euro 2024 - Wikipedia"…
Answer: Germany hosted UEFA Euro 2024.
Question: Who won UEFA Euro 2024? Use search.
search returned: {"query": "UEFA Euro 2024 winner", "results": [{"title": "UEFA Euro 2024"…
Answer: Spain won UEFA Euro 2024.
include_domains, time_range: Tavily Search docs.