Tavily Tools

Search the web and extract page content with Tavily, tuning depth and output format.

tavily_tools.py
"""
Tavily Tools
=============================

Demonstrates tavily tools.
"""

from agno.agent import Agent
from agno.tools.tavily import TavilyTools

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------


# Example 1: default TavilyTools
agent = Agent(tools=[TavilyTools()])

# Example 1a: TavilyTools with custom API base URL
# useful for self-hosted or alternative Tavily endpoints
agent_custom = Agent(tools=[TavilyTools(api_base_url="https://custom.tavily.com")])

# Example 2: Enable all Tavily functions (search + extract)
agent_all = Agent(tools=[TavilyTools(all=True)])

# Example 3: Use advanced search with context
context_agent = Agent(
    tools=[
        TavilyTools(
            enable_search=True,
        )
    ]
)

# ============================================================================
# EXTRACT EXAMPLES
# ============================================================================

# Example 4: URL content extraction with markdown format
extract_agent = Agent(
    tools=[
        TavilyTools(
            enable_search=False,  # Disable search for this example
            enable_extract=True,
            extract_depth="basic",  # basic = 1 credit/5 URLs
            extract_format="markdown",
        )
    ]
)

# Example 5: Advanced extraction with images in text format
advanced_extract_agent = Agent(
    tools=[
        TavilyTools(
            enable_search=False,
            enable_extract=True,
            extract_depth="advanced",  # advanced = 2 credits/5 URLs
            extract_format="text",
            include_images=True,
            include_favicon=True,
        )
    ]
)

# Example 6: Combined search and extract
combined_agent = Agent(
    tools=[
        TavilyTools(
            enable_search=True,
            enable_extract=True,
            search_depth="basic",
            extract_depth="basic",
            format="markdown",  # Format for search results
            extract_format="markdown",  # Format for extracted content
        )
    ]
)

# ============================================================================
# TEST THE AGENTS
# ============================================================================

# Test search agents

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    print("=" * 80)
    print("SEARCH EXAMPLES")
    print("=" * 80)

    agent.print_response(
        "Search for 'language models' and recent developments", markdown=True
    )

    context_agent.print_response(
        "Get detailed context about artificial intelligence trends", markdown=True
    )

    # Test extract agents
    print("\n" + "=" * 80)
    print("EXTRACT EXAMPLES")
    print("=" * 80)

    extract_agent.print_response(
        "Extract the main content from https://docs.tavily.com/documentation/api-reference/endpoint/extract",
        markdown=True,
    )

    advanced_extract_agent.print_response(
        "Extract content with images from https://github.com/anthropics/anthropic-sdk-python",
        markdown=True,
    )

    # Test combined agent
    print("\n" + "=" * 80)
    print("COMBINED SEARCH & EXTRACT")
    print("=" * 80)

    combined_agent.print_response(
        "Search for 'Tavily API documentation' and extract content from the most relevant result",
        markdown=True,
    )

Select the intended mode

context_agent currently uses ordinary search. To use Tavily's context helper, add enable_search_context=True to its toolkit. all=True enables extraction plus one search mode; it does not expose both search implementations. The configured agent_custom and agent_all are not called. Replace https://custom.tavily.com with a real compatible endpoint before calling that configuration.

extract_format="text" changes the wrapper's output headings; it does not request plain text from the provider, and returned content can still contain Markdown. The wrapper also drops image and favicon fields from the formatted response. For the image example, either request the page's text only or use the Tavily SDK's extract response directly to consume those fields.

In ordinary search, max_tokens is an approximate serialized-character budget for results, not a hard token limit on the whole answer. The context helper delegates its limit to the SDK.

Run the Example

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U agno openai tavily-python

Export your API keys

export OPENAI_API_KEY="your_openai_api_key_here"
export TAVILY_API_KEY="your_tavily_api_key_here"

Run the example

Save the code above as tavily_tools.py, then run:

python tavily_tools.py

Full source: cookbook/91_tools/tavily_tools.py