網頁擷取工具允許 Claude 從指定的網頁和 PDF 文件中檢索完整內容。
最新的網頁擷取工具版本 (web_fetch_20260209) 支援與 Claude Mythos Preview、Claude Opus 4.7、Claude Opus 4.6 和 Claude Sonnet 4.6 的動態篩選。Claude 可以編寫並執行程式碼來篩選擷取的內容,然後再將其載入上下文視窗,只保留相關資訊並捨棄其餘部分。這可以減少代幣消耗,同時保持回應品質。先前的工具版本 (web_fetch_20250910) 仍可用,但不具有動態篩選功能。
對於 Claude Mythos Preview,網頁擷取在 Claude API 和 Microsoft Foundry 上受支援。在 Amazon Bedrock 或 Google Vertex AI 上的 Mythos Preview 不可用。
使用意見回饋表單提供有關模型回應品質、API 本身或文件品質的意見回饋。
如需零資料保留資格和 allowed_callers 解決方案,請參閱伺服器工具。
在 Claude 處理不受信任的輸入以及敏感資料的環境中啟用網頁擷取工具會帶來資料外洩風險。只在受信任的環境中或處理非敏感資料時使用此工具。
為了最小化外洩風險,Claude 不允許動態構造 URL。Claude 只能擷取使用者明確提供的 URL,或來自先前網頁搜尋或網頁擷取結果的 URL。但是,仍然存在使用此工具時應仔細考慮的殘餘風險。
如果資料外洩是一個問題,請考慮:
max_uses 參數限制請求數量allowed_domains 參數限制為已知的安全網域如需模型支援,請參閱工具參考。
當您將網頁擷取工具新增至 API 請求時:
網頁擷取工具目前不支援透過 JavaScript 動態呈現的網站。
擷取完整網頁和 PDF 可能會快速消耗代幣,特別是當只需要從大型文件中提取特定資訊時。使用 web_fetch_20260209 工具版本,Claude 可以編寫並執行程式碼來篩選擷取的內容,然後再將其載入上下文。
動態篩選特別適用於:
動態篩選需要啟用程式碼執行工具。網頁擷取工具(有和沒有動態篩選)在 Claude API 和 Microsoft Azure 上可用。
若要啟用動態篩選,請使用 web_fetch_20260209 工具版本:
在 API 請求中提供網頁擷取工具:
網頁擷取工具支援以下參數:
{
"type": "web_fetch_20250910",
"name": "web_fetch",
// 可選:限制每個請求的擷取次數
"max_uses": 10,
// 可選:只從這些網域擷取
"allowed_domains": ["example.com", "docs.example.com"],
// 可選:永遠不從這些網域擷取
"blocked_domains": ["private.example.com"],
// 可選:為擷取的內容啟用引用
"citations": {
"enabled": true
},
// 可選:最大內容長度(以代幣計)
"max_content_tokens": 100000
}max_uses 參數限制執行的網頁擷取次數。如果 Claude 嘗試的擷取次數超過允許的次數,web_fetch_tool_result 將是一個錯誤,錯誤代碼為 max_uses_exceeded。目前沒有預設限制。
如需使用 allowed_domains 和 blocked_domains 進行網域篩選,請參閱伺服器工具。
max_content_tokens 參數限制上下文中包含的內容量。如果擷取的內容超過此限制,工具會將其截斷。這有助於在擷取大型文件時控制代幣使用。
max_content_tokens 參數限制是近似的。實際使用的輸入代幣數量可能會有少量變化。
與網頁搜尋不同(引用始終啟用),網頁擷取的引用是可選的。設定 "citations": {"enabled": true} 以啟用 Claude 引用擷取文件中的特定段落。
在直接向終端使用者顯示 API 輸出時,必須包含原始來源的引用。如果您在顯示給終端使用者之前對 API 輸出進行修改,包括重新處理和/或將其與您自己的材料結合,請根據與法律團隊的協商適當顯示引用。
以下是一個範例回應結構:
{
"role": "assistant",
"content": [
// 1. Claude 的擷取決定
{
"type": "text",
"text": "I'll fetch the content from the article to analyze it."
},
// 2. 擷取請求
{
"type": "server_tool_use",
"id": "srvtoolu_01234567890abcdef",
"name": "web_fetch",
"input": {
"url": "https://example.com/article"
}
},
// 3. 擷取結果
{
"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 的分析(如果啟用引用)
{
"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 上可用的最新版本。快取行為是自動管理的,可能會隨著時間推移而改變,以針對不同的內容類型和使用模式進行最佳化。
對於 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(成功)回應,錯誤在回應本文中表示:
{
"type": "web_fetch_tool_result",
"tool_use_id": "srvtoolu_a93jad",
"content": {
"type": "web_fetch_tool_error",
"error_code": "url_not_accessible"
}
}以下是可能的錯誤代碼:
invalid_input:無效的 URL 格式url_too_long:URL 超過最大長度(250 個字元)url_not_allowed:URL 被網域篩選規則和模型限制阻止url_not_accessible:無法擷取內容(HTTP 錯誤)too_many_requests:超過速率限制unsupported_content_type:不支援的內容類型(僅限文字和 PDF)max_uses_exceeded:超過最大網頁擷取工具使用次數unavailable:發生內部錯誤出於安全原因,網頁擷取工具只能擷取先前在對話上下文中出現過的 URL。這包括:
該工具無法擷取 Claude 生成的任意 URL 或來自容器型伺服器工具(程式碼執行、Bash 等)的 URL。
網頁擷取與網頁搜尋無縫協作,以進行全面的資訊蒐集:
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-7",
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},
},
],
)在此工作流程中,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 的擷取決定
event: content_block_start
data: {"type": "content_block_start", "index": 1, "content_block": {"type": "server_tool_use", "id": "srvtoolu_xyz789", "name": "web_fetch"}}
// 擷取 URL 串流
event: content_block_delta
data: {"type": "content_block_delta", "index": 1, "delta": {"type": "input_json_delta", "partial_json": "{\"url\":\"https://example.com/article\"}"}}
// 在擷取執行時暫停
// 擷取結果串流
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 的回應繼續...您可以在訊息批次 API 中包含網頁擷取工具。透過訊息批次 API 的網頁擷取工具呼叫的定價與常規訊息 API 請求中的相同。
Web fetch usage has no additional charges beyond standard token costs:
"usage": {
"input_tokens": 25039,
"output_tokens": 931,
"cache_read_input_tokens": 0,
"cache_creation_input_tokens": 0,
"server_tool_use": {
"web_fetch_requests": 1
}
}The web fetch tool is available on the Claude API at no additional cost. You only pay standard token costs for the fetched content that becomes part of your conversation context.
To protect against inadvertently fetching large content that would consume excessive tokens, use the max_content_tokens parameter to set appropriate limits based on your use case and budget considerations.
Example token usage for typical content:
Was this page helpful?
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-7",
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_20260209", "name": "web_fetch"}],
)
print(response)client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4-7",
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)