How to Build a Real-Time Web Research Bot Using Anthropic’s New Web Search API
In the rapidly evolving world of AI, staying updated with the latest information is crucial. Traditional AI models often rely on static training data, which can be months or even years old. That’s why Anthropic’s recent announcement of web search capabilities via their cloud API is a game changer. It enables developers to build AI-powered research bots that fetch real-time data from the web — without relying on external scraping tools or additional servers.
In this blog post, we’ll explore how to leverage Anthropic’s new web search API to build a real-time research assistant in Python. We’ll cover a demo example, key implementation details, useful options, and pricing considerations.
What’s New with Anthropic’s Web Search API?
Before this update, building a research bot that accessed up-to-date information meant integrating third-party tools or managing your own scraping infrastructure. Anthropic’s web search API simplifies this by allowing direct querying of the web through their cloud API, keeping everything streamlined in one place.
For example, imagine wanting to know about a breaking news event that happened just hours ago — such as the recent selection of the first American Pope (recorded May 8th, 2025). Since this information isn’t part of the AI’s training data, it needs to perform a live web search to generate an accurate and current report.
Building Your First Web Search Request: A “Hello World” Example
Getting started is straightforward. Here’s an overview of the basic steps using the Anthropic Python client:
-
Set your Anthropic API Key as an environment variable. This allows the client to authenticate requests seamlessly.
-
Instantiate the Anthropic client in Python.
-
Send a message to the model with your question. For example, “Who is the new pope?”
-
Add the
tools
parameter withweb_search
enabled. This tells the model to access live web data.
Here’s a snippet summarizing this:
```python
from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT
client = Anthropic()
response = client.completions.create(
model="claude-3",
messages=[{"role": "user", "content": "Who is the new pope?"}],
tools=["web_search"], # Enable web search tool
max_tokens=1000
)
print(response.choices[0].message.content)
```
Without web search enabled, the model might respond with outdated information (e.g., “As of the last update, it was Pope Francis”). But with web search active, it fetches the latest details, complete with citations from recent news sources.
Understanding the Web Search Response
The response from the API when using web search is richer and more complex than standard completions. It includes:
- Initial text from the model indicating it is performing a search.
- Tool usage details showing search queries and pages found.
- Encrypted content blocks representing scraped snippets (Anthropic encrypts these to avoid direct scraping exposure).
- Summarized text with citations — a distilled answer referencing URLs, page titles, and quoted text snippets.
Parsing this response can be a bit challenging. The Python client lets you convert the response to a dictionary or JSON format for easier inspection.
For example, you can iterate over the response’s message blocks, extract the main text, and gather citations like URLs and titles. This lets you assemble a report with clickable sources, ideal for building research assistants or automated reporting tools.
Improving Performance with Streaming
Waiting 20 seconds for a full response might be too slow for some applications. Anthropic supports streaming responses through an asynchronous client.
Using the async client, you can receive partial results as they become available and display them in real-time, improving user experience in chatbots or interactive assistants.
Customizing Search Domains: Allowed and Blocked Domains
Anthropic’s API offers parameters to restrict searches to certain domains (allowed_domains
) or exclude others (blocked_domains
). For example, if you only want information from Reuters, you can specify that in your request:
python
tools=[{"name": "web_search", "allowed_domains": ["reuters.com"]}]
However, note that some domains are off-limits due to scraping restrictions (e.g., BBC.co.uk, Reddit). Trying to search those will result in an error.
You can use either allowed_domains
or blocked_domains
in a single request, but not both simultaneously.
Pricing Overview: How Much Does It Cost?
Anthropic’s web search API pricing stands out as very competitive:
- $10 per 1,000 searches plus token usage for the normal API calls.
- Compared to OpenAI’s web search pricing of $30 to $50 per 10,000 calls, Anthropic’s is more affordable.
The pricing difference might be due to different search context sizes or optimizations, but it makes Anthropic a cost-effective choice for integrating live web data.
Wrapping Up
Anthropic’s new web search API opens exciting possibilities for developers building AI applications that require fresh, real-time data from the web. With simple integration, customizable domain filters, streaming support, and competitive pricing, it’s a compelling option for research bots, news aggregators, and knowledge assistants.
If you want to try this out yourself, check out the Anthropic Python client, set your API key, and start experimenting with live web queries today!
Useful Links
Author: Greg
Recorded May 8th, 2025
Feel free to leave comments or questions below if you want help building your own web research bot!