For how zero data retention (ZDR) applies to this feature, see API and data retention.
The web search tool gives Claude direct access to real-time web content, allowing it to answer questions with up-to-date information beyond its knowledge cutoff. The response includes citations for sources drawn from search results.
With web_search_20260209 and later versions, Claude can write and run code that filters the search results before they reach the context window (dynamic filtering), keeping only relevant information. Dynamic filtering is available with Claude Fable 5, Claude Opus 4.8, Claude Mythos 5, Claude Mythos Preview, Claude Opus 4.7, Claude Opus 4.6, Claude Sonnet 5, and Claude Sonnet 4.6.
Three versions of the web search tool are available:
web_search_20250305: basic web searchweb_search_20260209: adds dynamic filteringweb_search_20260318: adds response inclusion control for agentic workflowsThe examples on this page use web_search_20250305 for basic search and web_search_20260318 for dynamic filtering.
For Claude Mythos Preview, web search is supported on the Claude API, Google Cloud, and Microsoft Foundry. Web search is not available for Mythos Preview on Amazon Bedrock or Claude Platform on AWS.
For web search's Zero Data Retention eligibility and the related allowed_callers configuration, see Server tools.
For model support, see the Tool reference.
When you add the web search tool to your API request:
Claude searches when the request depends on information that is current, changing, or outside its training data:
Claude answers directly without searching when the request draws on stable knowledge:
Triggering is steerable through your system prompt: you can encourage Claude to search more readily or to prefer answering directly. For a hard constraint, use max_uses to cap the number of searches for each request.
With basic web search, every search result is loaded into Claude's context window, and much of that content can be irrelevant to the request. With web_search_20260209 or later, Claude instead writes and runs code that filters the results first, so only relevant content reaches the context window. This reduces token use on search-heavy requests.
Dynamic filtering runs web search from inside code execution: on web_search_20260209 and later, the tool's allowed_callers field defaults to ["code_execution_20260120"], and when dynamic filtering runs, the API provisions the code execution it needs for the request automatically. You don't need to add the code execution tool to tools yourself. There are no additional charges for code execution calls made this way beyond the standard token costs.
To call web search directly, without dynamic filtering, set allowed_callers: ["direct"]. Models that don't support programmatic tool calling require this setting. Without it, the API returns a 400 error that tells you to set it.
The web search tool (with and without dynamic filtering) is available on the Claude API, Claude Platform on AWS, and Microsoft Foundry. On Microsoft Foundry, web search requires a Hosted on Anthropic deployment. On Google Cloud, only the basic web search tool (without dynamic filtering) is available. Web search is not available on Amazon Bedrock.
The following examples use web_search_20260318:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=4096,
messages=[
{
"role": "user",
"content": "Search for the current prices of AAPL and GOOGL, then calculate which has a better P/E ratio.",
}
],
tools=[{"type": "web_search_20260318", "name": "web_search"}],
)
print(response)Web search is enabled for your organization unless an administrator has disabled it in the Claude Console, where they can also restrict which domains it searches. If it's disabled, a request that includes the tool fails with a 400 invalid_request_error that says web search is not enabled, rather than an error code inside a search result.
Provide the web search tool in your API request:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
messages=[{"role": "user", "content": "What's the weather in NYC?"}],
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 5}],
)
print(response)The web search tool supports the following parameters:
{
"type": "web_search_20250305",
"name": "web_search",
// Optional: Limit the number of searches per request
"max_uses": 5,
// Optional: Only include results from these domains.
// Use allowed_domains or blocked_domains, not both.
"allowed_domains": ["example.com", "trusteddomain.org"],
// Optional: Never include results from these domains
"blocked_domains": ["untrustedsource.com"],
// Optional: Localize search results
"user_location": {
"type": "approximate",
"city": "San Francisco",
"region": "California",
"country": "US",
"timezone": "America/Los_Angeles"
}
}All web search tool versions accept allowed_callers, which controls whether Claude calls web search directly or from code execution through dynamic filtering. On web_search_20260209 and later it defaults to ["code_execution_20260120"] instead of ["direct"]. See Server tools for how to configure it. web_search_20260318 and later also accept response_inclusion.
The max_uses parameter limits the number of searches performed. If Claude attempts more searches than allowed, the web_search_tool_result is an error with the max_uses_exceeded error code.
Simple factual queries typically use 1–3 searches; comparative or multientity research can use 10 or more. For guidance on choosing a value, see Server tools.
Provide allowed_domains or blocked_domains, not both. If a request includes both, the API returns a 400 error. Entries are bare domains with an optional path, for example example.com or example.com/blog, without a scheme.
For the full domain filtering rules, see Domain filtering in the Server tools guide.
The user_location parameter allows you to localize search results based on a user's location. Provide at least one of city, region, country, or timezone.
type: The type of location (must be approximate)city: The city nameregion: The region or statecountry: The two-letter ISO 3166-1 alpha-2 country code. The API rejects unsupported country codes with a 400 error.timezone: The IANA timezone ID.Requires web_search_20260318 or later.
The response_inclusion parameter controls how search result blocks appear in the API response when the result was consumed by a completed code execution call in the same turn. Set "response_inclusion": "excluded" to drop those nested server_tool_use and result block pairs entirely from the response, reducing output token costs for agentic workflows that don't need to echo raw search content back to the client. The default is "full". Results from direct calls, or from code execution calls that paused before completing, are always returned in full so they can be sent back on the next turn.
{
"tools": [
{
"type": "web_search_20260318",
"name": "web_search",
"response_inclusion": "excluded"
}
]
}Here's an example response structure:
{
"role": "assistant",
"content": [
// 1. Claude's decision to search
{
"type": "text",
"text": "I'll search for when Claude Shannon was born."
},
// 2. The search query used
{
"type": "server_tool_use",
"id": "srvtoolu_01WYG3ziw53XMcoyKL4XcZmE",
"name": "web_search",
"input": {
"query": "claude shannon birth date"
}
},
// 3. Search results
{
"type": "web_search_tool_result",
"tool_use_id": "srvtoolu_01WYG3ziw53XMcoyKL4XcZmE",
"content": [
{
"type": "web_search_result",
"url": "https://en.wikipedia.org/wiki/Claude_Shannon",
"title": "Claude Shannon - Wikipedia",
"encrypted_content": "EqgfCioIARgBIiQ3YTAwMjY1Mi1mZjM5LTQ1NGUtODgxNC1kNjNjNTk1ZWI3Y...",
"page_age": "April 30, 2025"
}
]
},
{
"text": "Based on the search results, ",
"type": "text"
},
// 4. Claude's response with citations
{
"text": "Claude Shannon was born on April 30, 1916, in Petoskey, Michigan",
"type": "text",
"citations": [
{
"type": "web_search_result_location",
"url": "https://en.wikipedia.org/wiki/Claude_Shannon",
"title": "Claude Shannon - Wikipedia",
"encrypted_index": "Eo8BCioIAhgBIiQyYjQ0OWJmZi1lNm..",
"cited_text": "Claude Elwood Shannon (April 30, 1916 – February 24, 2001) was an American mathematician, electrical engineer, computer scientist, cryptographer and i..."
}
]
}
],
"id": "msg_a930390d3a",
"usage": {
"input_tokens": 6039,
"output_tokens": 931,
"server_tool_use": {
"web_search_requests": 1
}
},
"stop_reason": "end_turn"
}This example shows a direct search. When a search runs through dynamic filtering, the response also contains the code execution tool's result blocks, and each nested server_tool_use and web_search_tool_result pair carries a caller field identifying the code execution call that made it.
Search results include:
url: The URL of the source pagetitle: The title of the source pagepage_age: When the site was last updatedencrypted_content: Encrypted content that you must pass back in multi-turn conversationsTo continue a conversation that contains search results, send the assistant's content blocks back exactly as you received them, including each result's encrypted_content. The API decrypts that content on later turns to restore the search results in Claude's context. If encrypted_content is missing or modified, the request fails with a 400 validation error.
Citations are always enabled for web search, and each web_search_result_location includes:
url: The URL of the cited sourcetitle: The title of the cited sourceencrypted_index: A reference that must be passed back for multi-turn conversationscited_text: Up to 150 characters of the cited contentThe web search citation fields cited_text, title, and url do not count toward input or output token usage.
When displaying API outputs directly to end users, citations must be included to the original source. If you are making modifications to API outputs, including by reprocessing or combining them with your own material before displaying them to end users, display citations as appropriate based on consultation with your legal team.
When the web search tool encounters an error (such as hitting rate limits), the Claude API still returns a 200 (success) response. The error is represented within the response body using the following structure:
{
"type": "web_search_tool_result",
"tool_use_id": "srvtoolu_a93jad",
"content": {
"type": "web_search_tool_result_error",
"error_code": "max_uses_exceeded"
}
}On an error, content is a single error object rather than a list of result blocks. A search that succeeds but matches no results returns an empty content list, not an error.
These are the possible error codes:
too_many_requests: Rate limit exceededinvalid_tool_input: Invalid search query parametermax_uses_exceeded: Maximum web search tool uses exceededquery_too_long: Query exceeds maximum lengthrequest_too_large: The search request is too large, typically because of a long domain filter listunavailable: An internal error occurredpause_turn stop reasonThe API can pause a long-running search turn and return stop_reason: "pause_turn". To continue, send the paused assistant message back unchanged in a new request.
If Claude calls web search and one of your client tools in the same group of parallel tool calls, the API returns stop_reason: "tool_use" instead and does not run the search yet. To continue, return the client tool results, and the API runs the search in the next request. See Mixing server tools and client tools in one turn.
For the server-side loop and pause_turn handling, see The server-side loop and pause_turn in the Server tools guide.
For caching tool definitions across turns, see Tool use with prompt caching.
With streaming enabled, you'll receive search events as part of the stream. There will be a pause while the search runs:
event: message_start
data: {"type": "message_start", "message": {"id": "msg_abc123", "type": "message"}}
event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}
// Claude's decision to search
event: content_block_start
data: {"type": "content_block_start", "index": 1, "content_block": {"type": "server_tool_use", "id": "srvtoolu_xyz789", "name": "web_search"}}
// Search query streamed
event: content_block_delta
data: {"type": "content_block_delta", "index": 1, "delta": {"type": "input_json_delta", "partial_json": "{\"query\":\"latest quantum computing breakthroughs 2025\"}"}}
// Pause while search executes
// Search results streamed
event: content_block_start
data: {"type": "content_block_start", "index": 2, "content_block": {"type": "web_search_tool_result", "tool_use_id": "srvtoolu_xyz789", "content": [{"type": "web_search_result", "title": "Quantum Computing Breakthroughs in 2025", "url": "https://example.com"}]}}
// Claude's response with citations (omitted in this example)You can include the web search tool in the Messages Batches API. Web search tool calls through the Messages Batches API are priced the same as those in regular Messages API requests.
To protect shared capacity, the Batches API throttles web search requests per organization, so large batches with many searches might take longer to complete. You can see your organization's web search rate limit on the Limits page in the Claude Console. To request a higher limit, contact sales from that page.
Web search usage is charged in addition to token usage:
{
"usage": {
"input_tokens": 105,
"output_tokens": 6039,
"cache_read_input_tokens": 7123,
"cache_creation_input_tokens": 7345,
"server_tool_use": {
"web_search_requests": 1
}
}
}Web search is available on the Claude API for $10 per 1,000 searches, plus standard token costs for search-generated content. Web search results retrieved throughout a conversation are counted as input tokens, in search iterations executed during a single turn and in subsequent conversation turns.
Each web search counts as one use, regardless of the number of results returned. If an error occurs during web search, the web search will not be billed.
Fetch and read content from specific URLs to augment Claude's context with live web content.
Work with Anthropic-executed tools: server_tool_use blocks, pause_turn continuation, and domain filtering.
Directory of Anthropic-provided tools and reference for optional tool definition properties.
Was this page helpful?