βBuild an asynchronous web search tool for your AI agent using Tavily, HTTPX, and Tenacity, with automatic retries and structured source extraction.β
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.
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.
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.
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.
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.
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.
By the end of this implementation, we have a working asynchronous web_search function that:
Applies request throttling.
Checks for the Tavily API key.
Sends an asynchronous search request.
Retries eligible failures using Tenacity.
Extracts the returned search results.
Converts them into structured SourceDoc objects.
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.
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.
Continue following "Deep Research Agent" with Building arXiv Search, Cross-Referencing, and Citation Tools | Deep Research Agent Part 7.
Ask questions, discuss architecture, and share insights with other developers.
Sign in to join the discussion and share your thoughts with other developers.
Sign In to Comment