關於「zero data retention」(零資料保留),即 ZDR 如何適用於此功能,請參閱 API 與資料保留。
網頁擷取工具允許 Claude 從指定的網頁和 PDF 文件中檢索完整內容。
最新的網頁擷取工具版本(web_fetch_20260318)在 Claude Fable 5、Claude Opus 4.8、Claude Mythos 5、Claude Mythos Preview、Claude Opus 4.7、Claude Opus 4.6、Claude Sonnet 5 和 Claude Sonnet 4.6 上支援動態過濾。Claude 可以撰寫並執行程式碼,在擷取的內容進入上下文視窗之前對其進行過濾,只保留相關資訊並捨棄其餘部分。這可以在維持回應品質的同時減少 token 消耗。web_fetch_20260318 還為代理式工作流程新增了回應包含控制。先前的版本(web_fetch_20260309 支援動態過濾和快取繞過、web_fetch_20260209 僅支援動態過濾、web_fetch_20250910 支援基本擷取)仍然可用。
網頁擷取(無論是否使用動態過濾)可在 Claude API、Claude Platform on AWS 和 Microsoft Foundry 上使用。在 Microsoft Foundry 上,網頁擷取需要 Hosted on Anthropic 部署。目前在 Amazon Bedrock 或 Google Cloud 上尚不可用。
對於 Claude Mythos Preview,網頁擷取可在 Claude API 和 Microsoft Foundry 上使用。目前 Mythos Preview 在 Amazon Bedrock 或 Google Cloud 上尚不可用。
請使用意見回饋表單提供有關模型回應品質、API 本身或文件品質的意見回饋。
有關零資料保留(Zero Data Retention)資格和 allowed_callers 解決方法,請參閱伺服器工具。
在 Claude 同時處理不受信任的輸入和敏感資料的環境中啟用網頁擷取工具,會帶來資料外洩風險。請僅在受信任的環境中或處理非敏感資料時使用此工具。
為了將外洩風險降至最低,Claude 不被允許動態建構 URL。Claude 只能擷取使用者明確提供的 URL,或來自先前網頁搜尋或網頁擷取結果的 URL。然而,使用此工具時仍存在您應仔細考慮的殘餘風險。
如果您擔心資料外洩,請考慮:
max_uses 參數限制請求數量allowed_domains 參數限制為已知的安全網域有關模型支援,請參閱工具參考。
網頁擷取是一種伺服器工具:API 在請求期間擷取內容並將結果插入對話中。您不需要執行任何操作或回傳 tool_result。例外情況是當 Claude 在同一組平行工具呼叫中同時呼叫網頁擷取和您的其中一個用戶端工具時:API 會在該擷取執行之前回傳帶有 stop_reason: "tool_use" 的回應,然後在您送回用戶端 tool_result 區塊時執行擷取。請參閱在單一回合中混合使用伺服器工具和用戶端工具。
當您將網頁擷取工具新增到 API 請求時:
網頁擷取工具目前不支援使用 JavaScript 動態渲染的網站。
當請求指向特定頁面或文件時,Claude 會進行擷取:
對於未引用特定頁面的一般知識或開放式問題,Claude 不會進行擷取。「總結這篇文章:<url>」會觸發擷取。「REST API 設計的最佳實踐是什麼?」則會直接回答。
擷取完整的網頁和 PDF 可能會快速消耗 token,尤其是當只需要從大型文件中取得特定資訊時。使用 web_fetch_20260209 或更新版本,Claude 可以撰寫並執行程式碼,在將擷取的內容載入上下文之前對其進行過濾。
這種動態過濾特別適用於:
動態過濾在程式碼執行工具上執行,API 會自動為該請求啟用此工具。您不需要將程式碼執行工具新增到 tools 陣列中。
若要啟用動態過濾,請使用 web_fetch_20260209 或任何更新版本。以下範例使用 web_fetch_20260318:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=4096,
messages=[
{
"role": "user",
"content": "Fetch the content at https://example.com/research-paper and extract the key findings.",
}
],
tools=[{"type": "web_fetch_20260318", "name": "web_fetch"}],
)
print(response)在您的 API 請求中提供網頁擷取工具:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Please analyze the content at https://example.com/article",
}
],
tools=[{"type": "web_fetch_20250910", "name": "web_fetch", "max_uses": 5}],
)
print(response)網頁擷取工具支援以下參數:
{
"type": "web_fetch_20250910",
"name": "web_fetch",
// Optional: Limit the number of fetches per request
"max_uses": 10,
// Optional: Only fetch from these domains
"allowed_domains": ["example.com", "docs.example.com"],
// Optional: Never fetch from these domains (cannot be combined with allowed_domains)
"blocked_domains": ["private.example.com"],
// Optional: Enable citations for fetched content
"citations": {
"enabled": true
},
// Optional: Maximum content length in tokens
"max_content_tokens": 100000
}較新的工具版本新增了兩個額外的可選參數:use_cache 需要 web_fetch_20260309 或更新版本(請參閱快取繞過),而 response_inclusion 需要 web_fetch_20260318 或更新版本(請參閱回應包含)。
max_uses 參數限制執行的網頁擷取次數。失敗的擷取也會計入限制。如果 Claude 嘗試的擷取次數超過允許的次數,web_fetch_tool_result 會是帶有 max_uses_exceeded 錯誤代碼的錯誤。目前沒有預設限制。
有關使用 allowed_domains 和 blocked_domains 進行網域過濾的資訊,請參閱伺服器工具。
max_content_tokens 參數限制包含在上下文中的內容量。如果擷取的內容超過此限制,工具會將其截斷。這有助於在擷取大型文件時控制 token 使用量。此限制適用於文字內容,不適用於 PDF 等二進位內容。
max_content_tokens 參數限制是近似值。實際使用的 input_tokens 數量可能會有少量差異。
需要 web_fetch_20260309 或更新版本(包括 web_fetch_20260318)。
use_cache 參數控制是否可以回傳快取的內容。設定 "use_cache": false 以繞過快取並擷取最新內容。預設值為 true。只有在使用者明確要求最新內容或擷取快速變化的來源時才停用快取,因為繞過快取會增加延遲(latency)。
{
"tools": [
{
"type": "web_fetch_20260309",
"name": "web_fetch",
"use_cache": false
}
]
}需要 web_fetch_20260318 或更新版本。
response_inclusion 參數控制當擷取結果在同一回合中被已完成的程式碼執行呼叫消耗時,擷取結果區塊在 API 回應中的呈現方式。設定 "response_inclusion": "excluded" 可將那些巢狀的 server_tool_use 和結果區塊配對完全從回應中移除,為不需要將原始頁面內容回傳給用戶端的代理式工作流程降低 output_tokens 成本。預設值為 "full"。來自直接呼叫的結果,或來自在完成前暫停的程式碼執行呼叫的結果,始終會完整回傳,以便在下一回合送回。
{
"tools": [
{
"type": "web_fetch_20260318",
"name": "web_fetch",
"response_inclusion": "excluded"
}
]
}與始終啟用引用的網頁搜尋不同,網頁擷取的引用是可選的,且預設為停用。設定 "citations": {"enabled": true} 以讓 Claude 能夠引用擷取文件中的特定段落。
當直接向終端使用者顯示 API 輸出時,請包含原始來源的引用。如果您要修改 API 輸出,包括在向終端使用者顯示之前重新處理和/或將其與您自己的素材結合,請在諮詢您的法律團隊後酌情顯示引用。
以下是回應結構的範例:
{
"role": "assistant",
"content": [
// 1. Claude's decision to fetch
{
"type": "text",
"text": "I'll fetch the content from the article to analyze it."
},
// 2. The fetch request
{
"type": "server_tool_use",
"id": "srvtoolu_01234567890abcdef",
"name": "web_fetch",
"input": {
"url": "https://example.com/article"
}
},
// 3. Fetch results
{
"type": "web_fetch_tool_result",
"tool_use_id": "srvtoolu_01234567890abcdef",
"content": {
"type": "web_fetch_result",
"url": "https://example.com/article",
"content": {
"type": "document",
"source": {
"type": "text",
"media_type": "text/plain",
"data": "Full text content of the article..."
},
"title": "Article Title",
"citations": { "enabled": true }
},
"retrieved_at": "2025-08-25T10:30:00Z"
}
},
// 4. Claude's analysis with citations (if enabled)
{
"text": "Based on the article, ",
"type": "text"
},
{
"text": "the main argument presented is that artificial intelligence will transform healthcare",
"type": "text",
"citations": [
{
"type": "char_location",
"document_index": 0,
"document_title": "Article Title",
"start_char_index": 1234,
"end_char_index": 1456,
"cited_text": "Artificial intelligence is poised to revolutionize healthcare delivery..."
}
]
}
],
"id": "msg_a930390d3a",
"usage": {
"input_tokens": 25039,
"output_tokens": 931,
"server_tool_use": {
"web_fetch_requests": 1
}
},
"stop_reason": "end_turn"
}擷取結果包括:
url:被擷取的 URLcontent:包含擷取內容的文件區塊retrieved_at:內容被檢索時的時間戳記網頁擷取工具會快取結果以提高效能並減少重複請求。回傳的內容可能不一定反映該 URL 上可用的最新版本。快取行為是自動管理的,並可能隨時間變化以針對不同的內容類型和使用模式進行最佳化。若要擷取最新內容,請設定 "use_cache": false(請參閱快取繞過)。
對於 PDF 文件,內容以 base64 編碼資料的形式回傳:
{
"type": "web_fetch_tool_result",
"tool_use_id": "srvtoolu_02",
"content": {
"type": "web_fetch_result",
"url": "https://example.com/paper.pdf",
"content": {
"type": "document",
"source": {
"type": "base64",
"media_type": "application/pdf",
"data": "JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmo..."
},
"citations": { "enabled": true }
},
"retrieved_at": "2025-08-25T10:30:02Z"
}
}當網頁擷取工具遇到錯誤時,Claude API 會回傳 200(成功)回應,錯誤會呈現在回應主體中。Claude 會看到錯誤結果並繼續該回合。例如:
{
"type": "web_fetch_tool_result",
"tool_use_id": "srvtoolu_a93jad",
"content": {
"type": "web_fetch_tool_result_error",
"error_code": "url_not_accessible"
}
}以下是可能的錯誤代碼:
invalid_tool_input:無效的工具輸入,例如格式錯誤的 URL 或非 HTTP(S) 協定url_too_long:URL 超過最大長度(250 個字元)url_not_allowed:URL 被網域過濾規則(包括您組織的設定)或 Anthropic 端的限制(例如私有位址和 robots.txt)封鎖url_not_in_prior_context:URL 未在對話中先前出現(請參閱 URL 驗證)url_not_accessible:擷取內容失敗(HTTP 錯誤)too_many_requests:超過速率限制unsupported_content_type:不支援的內容類型(僅支援文字、HTML 和 PDF)max_uses_exceeded:超過網頁擷取工具的最大使用次數unavailable:發生內部錯誤基於安全考量,網頁擷取工具只能擷取先前已在對話上下文中出現過的 URL。這包括:
此工具無法擷取 Claude 生成的任意 URL,或來自基於容器的伺服器工具(例如程式碼執行和 Bash)的 URL。
當同時啟用網頁搜尋和網頁擷取工具,且使用者指名特定頁面或文件但未提供 URL 時(例如「讀取 anthropics/anthropic-sdk-python 儲存庫的 README」),Claude 會使用網頁搜尋來找到它,然後擷取結果。以下範例在單一請求中要求搜尋和分析:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=4096,
messages=[
{
"role": "user",
"content": "Find recent articles about quantum computing and analyze the most relevant one in detail",
}
],
tools=[
{"type": "web_search_20250305", "name": "web_search", "max_uses": 3},
{
"type": "web_fetch_20250910",
"name": "web_fetch",
"max_uses": 5,
"citations": {"enabled": True},
},
],
)
print(response)在此工作流程中,Claude 會:
有關跨回合快取工具定義的資訊,請參閱工具使用與提示快取。
啟用串流後,擷取事件會成為串流的一部分,並在內容檢索期間暫停:
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 fetch
event: content_block_start
data: {"type": "content_block_start", "index": 1, "content_block": {"type": "server_tool_use", "id": "srvtoolu_xyz789", "name": "web_fetch"}}
// Fetch URL streamed
event: content_block_delta
data: {"type": "content_block_delta", "index": 1, "delta": {"type": "input_json_delta", "partial_json": "{\"url\":\"https://example.com/article\"}"}}
// Pause while fetch executes
// Fetch results streamed
event: content_block_start
data: {"type": "content_block_start", "index": 2, "content_block": {"type": "web_fetch_tool_result", "tool_use_id": "srvtoolu_xyz789", "content": {"type": "web_fetch_result", "url": "https://example.com/article", "content": {"type": "document", "source": {"type": "text", "media_type": "text/plain", "data": "Article content..."}}}}}
// Claude's response continues...您可以在 Messages Batches API 中包含網頁擷取工具。透過 Messages Batches API 進行的網頁擷取工具呼叫,其定價與一般 Messages API 請求中的定價相同。
網頁擷取的使用除了標準 token 費用外,不會產生額外費用:
{
"usage": {
"input_tokens": 25039,
"output_tokens": 931,
"cache_read_input_tokens": 0,
"cache_creation_input_tokens": 0,
"server_tool_use": {
"web_fetch_requests": 1
}
}
}網頁擷取工具在 Claude API 上可供使用,且無需額外費用。您只需為成為對話上下文一部分的擷取內容支付標準 token 費用。
為了避免無意間擷取大量內容而消耗過多 token,請使用 max_content_tokens 參數,根據您的使用情境和預算考量設定適當的限制。
典型內容的 token 使用量範例:
在沙箱容器中執行 Python 和 bash 程式碼,以分析資料、生成檔案並迭代解決方案。
使用由 Anthropic 執行的工具:server_tool_use 區塊、pause_turn 接續和網域過濾。
Anthropic 提供的工具目錄以及可選工具定義屬性的參考。
Was this page helpful?