Claude web search
This page explains how to let Claude search the web with the web_search tool. For model IDs, endpoints and providers, see the Claude family overview.
Key pointFor web search with Sonnet 5.5, use the Anthropic-native Messages API and pin the Anthropic provider. Check the Web Search capability on other models’ pages.
Add the official web_search server tool to tools. The model runs searches on its own and cites sources in its answer:
Python
claude_web_search.py
import os
import anthropic
client = anthropic.Anthropic(
base_url="https://api.ofox.ai/anthropic",
api_key=os.environ["OFOX_API_KEY"],
default_headers={"X-OfoxAI-Provider-Type": "anthropic"},
)
message = client.messages.create(
model="anthropic/claude-sonnet-5.5",
max_tokens=192,
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 1, "allowed_domains": ["python.org"]}],
messages=[{"role": "user", "content": "Use web search once to find the official Python downloads page on python.org. Answer with its URL only."}],
)
print("".join(b.text for b in message.content if b.type == "text"))
print("Searches:", message.usage.server_tool_use.web_search_requests)- The response
contentcontains, in order,server_tool_use(search queries),web_search_tool_result(search results) andtext(answer) blocks. usage.server_tool_use.web_search_requestsis the actual number of searches. Searches are billed per use; see the model page for the rate.max_useslimits the search count in one request. Search results can also contribute to input token charges.
For other errors, see Common errors.
Last updated on