關於「zero data retention」(零資料保留),即 ZDR 如何適用於此功能,請參閱 API 與資料保留。
網頁搜尋工具讓 Claude 能直接存取即時網頁內容,使其能夠以超出其知識截止日期的最新資訊回答問題。回應中包含從搜尋結果中取得的來源引用。
使用 web_search_20260209 及更新版本時,Claude 可以撰寫並執行程式碼,在搜尋結果進入上下文視窗之前先進行過濾(dynamic filtering,動態過濾),只保留相關資訊。動態過濾適用於 Claude 4.6 及更新的模型,以及 Claude Mythos Preview。
網頁搜尋工具有三個版本可用:
本頁的範例使用 web_search_20250305 進行基本搜尋,並使用 web_search_20260318 進行動態過濾。
對於 Claude Mythos Preview,網頁搜尋在 Claude API、Google Cloud 和 Microsoft Foundry 上受支援。Mythos Preview 在 Amazon Bedrock 或 Claude Platform on AWS 上不提供網頁搜尋。
關於網頁搜尋的零資料保留(Zero Data Retention)資格以及相關的 allowed_callers 設定,請參閱伺服器工具。
關於模型支援,請參閱工具參考。
當您將網頁搜尋工具加入 API 請求時:
當請求依賴於最新的、變動中的或超出其訓練資料範圍的資訊時,Claude 會進行搜尋:
當請求依賴於穩定的知識時,Claude 會直接回答而不進行搜尋:
搜尋的觸發可以透過您的系統提示來引導:您可以鼓勵 Claude 更積極地搜尋,或偏好直接回答。若需要硬性限制,請使用 max_uses 來限制每個請求的搜尋次數。
使用基本網頁搜尋時,每個搜尋結果都會載入 Claude 的上下文視窗,而其中許多內容可能與請求無關。使用 web_search_20260209 或更新版本時,Claude 會改為撰寫並執行程式碼先過濾結果,因此只有相關內容會進入上下文視窗。這可以減少搜尋密集型請求的 token 使用量。
動態過濾是從程式碼執行內部執行網頁搜尋:在 web_search_20260209 及更新版本上,工具的 allowed_callers 欄位預設為 ["code_execution_20260120"],當動態過濾執行時,API 會自動為該請求佈建所需的程式碼執行。您不需要自行將程式碼執行工具加入 tools。以這種方式進行的程式碼執行呼叫,除了標準 token 費用外,不會產生額外費用。
若要直接呼叫網頁搜尋而不使用動態過濾,請設定 allowed_callers: ["direct"]。不支援程式化工具呼叫的模型需要此設定。若未設定,API 會回傳 400 錯誤並提示您進行設定。
網頁搜尋工具(無論是否使用動態過濾)可在 Claude API、Claude Platform on AWS 和 Microsoft Foundry 上使用。在 Microsoft Foundry 上,網頁搜尋需要 Hosted on Anthropic 部署。在 Google Cloud 上,只有基本網頁搜尋工具(不含動態過濾)可用。網頁搜尋在 Amazon Bedrock 上不可用。
以下範例使用 web_search_20260318:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5",
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)除非管理員已在 Claude Console 中停用網頁搜尋,否則您的組織已啟用此功能;管理員也可以在該處限制其搜尋的網域。如果已停用,包含此工具的請求會失敗並回傳 400 invalid_request_error,指出網頁搜尋未啟用,而不是在搜尋結果中回傳錯誤代碼。
在您的 API 請求中提供網頁搜尋工具:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5",
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)網頁搜尋工具支援以下參數:
{
"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"
}
}所有網頁搜尋工具版本都接受 allowed_callers,它控制 Claude 是直接呼叫網頁搜尋,還是透過動態過濾從程式碼執行中呼叫。在 web_search_20260209 及更新版本上,其預設值為 ["code_execution_20260120"] 而非 ["direct"]。關於如何設定,請參閱伺服器工具。web_search_20260318 及更新版本也接受 response_inclusion。
max_uses 參數限制執行的搜尋次數。如果 Claude 嘗試的搜尋次數超過允許的數量,web_search_tool_result 會是一個帶有 max_uses_exceeded 錯誤代碼的錯誤。
簡單的事實性查詢通常使用 1–3 次搜尋;比較性或多實體研究可能使用 10 次或更多。關於如何選擇數值的指引,請參閱伺服器工具。
請提供 allowed_domains 或 blocked_domains,但不能同時提供兩者。如果請求同時包含兩者,API 會回傳 400 錯誤。項目為不含協定(scheme)的純網域,可選擇附加路徑,例如 example.com 或 example.com/blog。
關於完整的網域過濾規則,請參閱伺服器工具指南中的網域過濾。
user_location 參數允許您根據使用者的位置在地化搜尋結果。請至少提供 city、region、country 或 timezone 其中之一。
type:位置類型(必須為 approximate)city:城市名稱region:地區或州country:兩個字母的 ISO 3166-1 alpha-2 國家代碼。API 會以 400 錯誤拒絕不支援的國家代碼。timezone:IANA 時區 ID。需要 web_search_20260318 或更新版本。
response_inclusion 參數控制當搜尋結果已被同一回合中已完成的程式碼執行呼叫所使用時,搜尋結果區塊在 API 回應中的呈現方式。設定 "response_inclusion": "excluded" 可將那些巢狀的 server_tool_use 和結果區塊配對完全從回應中移除,為不需要將原始搜尋內容回傳給用戶端的代理式工作流程降低輸出 token 成本。預設值為 "full"。來自直接呼叫的結果,或來自在完成前暫停的程式碼執行呼叫的結果,一律會完整回傳,以便在下一回合中送回。
{
"tools": [
{
"type": "web_search_20260318",
"name": "web_search",
"response_inclusion": "excluded"
}
]
}以下是一個回應結構的範例:
{
"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"
}此範例顯示的是直接搜尋。當搜尋透過動態過濾執行時,回應也會包含程式碼執行工具的結果區塊,且每個巢狀的 server_tool_use 和 web_search_tool_result 配對都帶有一個 caller 欄位,用以識別發出該呼叫的程式碼執行呼叫。
搜尋結果包含:
url:來源頁面的 URLtitle:來源頁面的標題page_age:網站最後更新的時間encrypted_content:在多回合對話中必須回傳的加密內容若要繼續包含搜尋結果的對話,請將助理的內容區塊完全按照您收到的樣子送回,包括每個結果的 encrypted_content。API 會在後續回合中解密該內容,以在 Claude 的上下文中還原搜尋結果。如果 encrypted_content 遺失或被修改,請求會以 400 驗證錯誤失敗。
網頁搜尋一律啟用引用功能,每個 web_search_result_location 包含:
url:被引用來源的 URLtitle:被引用來源的標題encrypted_index:在多回合對話中必須回傳的參照cited_text:最多 150 個字元的被引用內容網頁搜尋引用欄位 cited_text、title 和 url 不計入輸入或輸出 token 用量。
當直接向終端使用者顯示 API 輸出時,必須包含對原始來源的引用。如果您對 API 輸出進行修改,包括在向終端使用者顯示之前重新處理或將其與您自己的素材結合,請在諮詢您的法律團隊後,視情況適當地顯示引用。
當網頁搜尋工具遇到錯誤(例如達到速率限制)時,Claude API 仍會回傳 200(成功)回應。錯誤會以下列結構呈現在回應主體中:
{
"type": "web_search_tool_result",
"tool_use_id": "srvtoolu_a93jad",
"content": {
"type": "web_search_tool_result_error",
"error_code": "max_uses_exceeded"
}
}發生錯誤時,content 是單一錯誤物件,而不是結果區塊的清單。搜尋成功但沒有符合結果時,會回傳空的 content 清單,而不是錯誤。
以下是可能的錯誤代碼:
too_many_requests:超過速率限制invalid_tool_input:無效的搜尋查詢參數max_uses_exceeded:超過網頁搜尋工具的最大使用次數query_too_long:查詢超過最大長度request_too_large:搜尋請求過大,通常是因為網域過濾清單過長unavailable:發生內部錯誤pause_turn 停止原因API 可以暫停長時間執行的搜尋回合並回傳 stop_reason: "pause_turn"。若要繼續,請在新請求中原封不動地送回已暫停的助理訊息。
如果 Claude 在同一組平行工具呼叫中同時呼叫網頁搜尋和您的其中一個用戶端工具,API 會改為回傳 stop_reason: "tool_use",且尚不會執行搜尋。若要繼續,請回傳用戶端工具結果,API 會在下一個請求中執行搜尋。請參閱在同一回合中混合使用伺服器工具和用戶端工具。
關於伺服器端迴圈和 pause_turn 的處理,請參閱伺服器工具指南中的伺服器端迴圈與 pause_turn。
關於跨回合快取工具定義,請參閱工具使用與提示快取。
啟用串流後,您會在串流中收到搜尋事件。搜尋執行期間會有一段暫停:
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)您可以在 Messages Batches API 中包含網頁搜尋工具。透過 Messages Batches API 進行的網頁搜尋工具呼叫,其定價與一般 Messages API 請求中的呼叫相同。
為了保護共享容量,Batches API 會以組織為單位對網頁搜尋請求進行節流,因此包含大量搜尋的大型批次可能需要更長的時間才能完成。您可以在 Claude Console 的限制頁面上查看您組織的網頁搜尋速率限制。若要申請更高的限制,請從該頁面聯絡銷售團隊。
網路搜尋的使用費用會在 token 使用費用之外額外收取:
{
"usage": {
"input_tokens": 105,
"output_tokens": 6039,
"cache_read_input_tokens": 7123,
"cache_creation_input_tokens": 7345,
"server_tool_use": {
"web_search_requests": 1
}
}
}網路搜尋在 Claude API 上的費用為每 1,000 次搜尋 $10 美元,外加搜尋生成內容的標準 token 費用。在整個對話過程中擷取的網路搜尋結果會被計為輸入 token,包括在單一回合中執行的搜尋迭代以及後續對話回合中的結果。
每次網路搜尋都計為一次使用,無論傳回的結果數量為何。如果在網路搜尋期間發生錯誤,該次網路搜尋將不會被計費。
從特定 URL 擷取並讀取內容,以即時網頁內容擴充 Claude 的上下文。
使用由 Anthropic 執行的工具:server_tool_use 區塊、pause_turn 接續,以及網域過濾。
Anthropic 提供的工具目錄,以及可選工具定義屬性的參考。
Was this page helpful?