Tavily Search
The Tavily Search integration allows AG2 agents to perform real-time, AI-powered web searches. Tavily is a search API purpose-built for AI agents and LLMs, delivering accurate and factual results optimized for retrieval-augmented generation (RAG) workflows.
Configuring Your Tavily API Key#
- Create a Tavily Account:
- Visit Tavily
-
Click Sign Up and create an account
-
Get Your API Key:
- Navigate to Tavily API Dashboard
-
Generate an API key under API Keys
-
Set the
TAVILY_API_KEYEnvironment Variable:
Package Installation#
Install AG2 with the tavily extra (and openai for the example below):
Note:
autogenandag2are aliases for the same PyPI package:
Implementation#
Imports#
import os
from autogen import AssistantAgent, UserProxyAgent, LLMConfig
from autogen.tools.experimental import TavilySearchTool
Agent Configuration#
llm_config = LLMConfig({"api_type": "openai", "model": "gpt-4o"})
assistant = AssistantAgent(
name="assistant",
llm_config=llm_config,
)
user_proxy = UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
)
Tool Setup#
tavily_search_tool = TavilySearchTool(tavily_api_key=os.getenv("TAVILY_API_KEY"))
# Register the tool for LLM recommendation and execution.
tavily_search_tool.register_for_llm(assistant)
tavily_search_tool.register_for_execution(user_proxy)
Usage Example#
response = user_proxy.initiate_chat(
recipient=assistant,
message="What are the latest developments in multi-agent AI systems?",
max_turns=2,
)
print(f"Final Answer: {response.summary}")
Parameters#
TavilySearchTool accepts the following search parameters at call time:
| Parameter | Type | Default | Description |
|---|---|---|---|
query | str | required | The search query string |
search_depth | str | "basic" | Search depth — "basic" for fast results or "advanced" for deeper, more comprehensive search |
include_answer | str | "basic" | Whether to include an AI-generated answer — "basic" or "advanced" |
include_raw_content | bool | False | Whether to include the raw page content in results |
include_domains | list[str] | [] | Restrict search to specific domains (e.g., ["arxiv.org", "github.com"]) |
num_results | int | 5 | Maximum number of results to return |
Output#
Each search returns a list of dictionaries with:
title— the result titlelink— the result URLsnippet— a relevant content excerpt
Deep Research with Tavily#
Tavily powers the web search layer in both of AG2's deep research approaches:
- DeepResearchTool — AG2's built-in tool for multi-step research tasks. Uses Tavily under the hood for web retrieval, tightly integrated into your agent pipeline.
- GPT Researcher — a standalone multi-agent research system that uses Tavily for recursive querying and produces structured reports (Markdown, PDF, Docx).
Both approaches require a TAVILY_API_KEY. See the Deep Research guide for setup and configuration.