What You’ll Build
An interactive chatbot that uses Tavily tools to answer questions with real-time web data. It dynamically chooses between lightweight search (for simple factual questions) and deep research (for complex, multi-source analysis) — then synthesizes answers with numbered citations.View Source on GitHub
Architecture
Key behavior:- The agent can call
search_and_formatmultiple times until it has enough information - The agent can only call
stream_researchonce per query (comprehensive but expensive) - All responses include numbered citations linking to sources
Tools Used
| Tool | When Used | Description |
|---|---|---|
search_and_format | Simple, factual questions (“What is the capital of France?”) | Runs parallel web searches and returns formatted results |
stream_research | Complex queries requiring analysis, comparisons, or trends | Uses Tavily’s deep research endpoint for multi-source synthesis |
Quick Start
- Anthropic SDK
- LangGraph
Source File
How It Works
Tool Definitions
Tool Definitions
The chatbot exposes two tools to the LLM. The agent decides which to call based on the query:
search_and_format— wrapstavily_agent_toolkit.search_and_formatto run parallel web searches across one or more queries. Accepts an optionaltime_rangefilter.stream_research— calls Tavily’s research API in streaming mode viatavily_agent_toolkit.handle_research_stream, returning a comprehensive report.
Routing Logic
Routing Logic
The system prompt instructs the agent to pick the right tool:This keeps costs low for quick lookups while enabling deep research when needed.
Agent Loop
Agent Loop
The chatbot runs a standard agent loop:
- Send the user message + tool definitions to the LLM
- If the LLM returns a tool call, execute it and feed the result back
- Repeat until the LLM returns a final text response
- Print the response with citations and continue the conversation
Citation Handling
Citation Handling
The system prompt enforces citation discipline:
Example Interaction
Key Parameters to Tune
| Parameter | Where | Effect |
|---|---|---|
model | Agent creation | Controls reasoning quality and cost |
max_results | search_and_format | Number of search results per query (default: 5) |
model | research call | "mini" for faster, "default" for more thorough |
time_range | search_and_format | Filter results by recency ("day", "week", "month", "year") |
Next Steps
Tools Reference
Full parameter docs for search_and_format and all other tools.
Company Intelligence
Add website crawling and extraction to your agent’s capabilities.