sndev
Project CategoriesDeveloper LearnDeveloper Insights
LoginGet Started
sndevv1.0.0
AboutProject FeedInsightsYouTubePrivacy PolicyTerms of Service

Β© 2026 sndev β€’ v1.0.0

Home
Categories
Dev Learn
Insights
Sign In
Agentic AI
Nov 15, 2026
s
sndev

Building a Web Search Tool for AI Agents with Tavily API | Deep Research Agent Part 6

GitHub RepositoryWatch on YouTube

β€œBuild an asynchronous web search tool for your AI agent using Tavily, HTTPX, and Tenacity, with automatic retries and structured source extraction.”

Project SeriesPart 6 of 13

Deep Research Agent

Series Progress46%
Series Episodes (13)
Build an Autonomous Deep Research Agent | End-to-End Agentic AI Project | Part 1
Available
Setup & Installation for Autonomous Deep Research Agent | End-to-End Agentic AI Project | Part 2
Available
Building the Agentic AI Brain with LangGraph | Autonomous Deep Research Agent | Part 3
Available
Building AI Agent with LangGraph | Planner Agent Implementation | Deep Research Agent | Part 4
Available
Building AI Agent Tools | Rate Limiting & Source Credibility | Deep Research Agent | Part 5
Available
P6
Building a Web Search Tool for AI Agents with Tavily API | Deep Research Agent Part 6
Current Part
Building arXiv Search, Cross-Referencing, and Citation Tools | Deep Research Agent Part 7
Available
Building the Researcher Agent with Web Search, arXiv & Source Ranking | Deep Research Agent Part 8
Available
Building the Critic Agent: Detecting Conflicting Research | Deep Research System Part 9
Available
Building the Writer Agent – Generating Structured AI Research Reports | Part 10
Available
Building a Multi-Agent Workflow with LangGraph | Deep Research Agent – Part 11
Available
Building a Real-Time WebSocket Streaming Engine with FastAPI | Deep Research Agent – Part 12
Available
Building and Deploying a Full-Stack AI Deep Research Agent | Part 13 – Final Chapter
Available
Part 5Part 7

An autonomous research agent needs access to reliable information from the outside world. To make that possible, we need to give it tools that can search the web, retrieve relevant results, and convert those results into structured data.

In Part 6 of our Autonomous Deep Research Agent series, we build our first real research tool using the Tavily API. We also integrate asynchronous HTTP requests, automatic retry handling, request throttling, and our custom SourceDoc model.

1. Why Tavily for Web Search?

For this project, we use Tavily because it is designed for search workflows involving AI applications and agents.

The goal is to provide our future Researcher Agent with a search function that accepts a query and returns a collection of structured sources.

We use HTTPX to make asynchronous requests. Unlike a synchronous request, an asynchronous HTTP call allows the application to handle other work while waiting for the external API response.

2. Adding Automatic Retry Handling with Tenacity

External APIs can fail for several reasons, including network timeouts and temporary HTTP errors.

Instead of immediately failing the entire research operation, we use Tenacity to retry requests when specific exceptions occur.

Our retry configuration includes:

  • Retry on HTTP timeout and HTTP errors.

  • Use exponential backoff between attempts.

  • Wait approximately 2 seconds initially, with a maximum wait of 10 seconds.

  • Stop after three attempts.

This gives temporary failures a chance to recover without creating unlimited retry loops.

3. Integrating Request Throttling

We previously created a throttle function to prevent our tools from sending requests too frequently.

We now integrate that function into the web search tool.

Before making a request, the function calls the throttle mechanism using the tool name web_search. This helps enforce the configured minimum interval between calls.

Combining throttling with bounded retries gives us better control over how our agent interacts with external services.

4. Making an Asynchronous Request to Tavily

Next, we load the Tavily API key from our environment variables and create an asynchronous HTTPX client.

We configure the search request with:

  • The user's search query.

  • The maximum number of results.

  • Advanced search depth.

  • include_answer set to false.

  • include_raw_content set to false.

We then send the request and use raise_for_status() to detect unsuccessful HTTP responses. This is important because Tenacity needs an exception to recognize a failed request and apply the retry policy.

5. Converting Search Results into SourceDoc

Receiving search results is only part of the job. Our agents need a consistent data structure that can be shared through the LangGraph state.

We therefore map each Tavily result into our custom SourceDoc model.

The mapping includes:

Field

Purpose

URL

Original source address

Title

Source title

Source type

Identifies the source as web

Snippet

Extracted content

Confidence score

Our domain-based confidence score

Metadata

Additional search information

We limit the extracted content to the first 500 characters per result. This is an initial context-management decision for our project, particularly while working with limited API resources.

We also store Tavily's own search score in the metadata, separately from our domain-based confidence score.

These two scores represent different signals: Tavily's search relevance score and our own heuristic assessment of the source's domain. Neither one independently proves that a source is factually correct.

6. The Result: Our First Web Search Tool

By the end of this implementation, we have a working asynchronous web_search function that:

  1. Applies request throttling.

  2. Checks for the Tavily API key.

  3. Sends an asynchronous search request.

  4. Retries eligible failures using Tenacity.

  5. Extracts the returned search results.

  6. Converts them into structured SourceDoc objects.

  7. Returns a list of sources for future agent workflows.

This is our first completed external research tool. The next step is to develop the remaining tools and connect them to the Researcher and Critic Agents.

Project Resources

  • GitHub Repository: https://github.com/snsupratim/research-agent

  • Project Documentation: https://www.sndev.co/project/988de038-1e9a-4411-aac5-828477b52df8

  • Video Series: Autonomous Deep Research Agent – Part 6

Follow sn dev for more practical tutorials on Agentic AI, Python, LangGraph, and AI Engineering.

AgenticAI
Python
FastAPI
AIEngineering
LangGraph
GenerativeAI
DeepResearchAgent
Series Navigation

Up Next: Part 7

Continue following "Deep Research Agent" with Building arXiv Search, Cross-Referencing, and Citation Tools | Deep Research Agent Part 7.

Part 5Read Part 7

Community Discussion
0

Ask questions, discuss architecture, and share insights with other developers.

Sort:

Sign in to join the discussion and share your thoughts with other developers.

Sign In to Comment
Loading discussions…