Claude Platform Docs
  • Messages
  • 託管代理
  • 管理

Search...
⌘K
第一步
Claude 簡介快速入門
使用 Claude 進行建構
功能概覽使用 Messages API停止原因與備援拒絕與備援備援額度
模型能力
擴展思考自適應思考Effort任務預算(測試版)快速模式(研究預覽)結構化輸出引用串流 Messages批次處理搜尋結果串流拒絕多語言支援嵌入
工具
概覽工具使用的運作方式教學:建構使用工具的代理定義工具處理工具呼叫平行工具使用Tool Runner (SDK)嚴格工具使用伺服器工具網頁搜尋工具網頁擷取工具程式碼執行工具顧問工具工具搜尋工具記憶工具Bash 工具文字編輯器工具電腦使用工具疑難排解
工具基礎架構
工具參考管理工具上下文工具組合工具使用與提示快取程式化工具呼叫細粒度工具串流
上下文管理
上下文視窗壓縮上下文編輯提示快取對話中系統訊息建構協調模式快取診斷(測試版)Token 計數
處理檔案
Files APIPDF 支援
技能
概覽快速入門最佳實踐企業技能API 中的技能
MCP
遠端 MCP 伺服器MCP 連接器
雲端平台上的 Claude
Amazon BedrockAmazon Bedrock(舊版)AWS 上的 Claude PlatformGoogle CloudMicrosoft Foundry

Log in
處理工具呼叫
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Claude Platform Docs

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Claude on AWS
  • Claude on Google Cloud

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
Messages/工具

處理工具呼叫

解析 tool_use 區塊、格式化 tool_result 回應,並使用 is_error 處理錯誤。

本頁面涵蓋工具呼叫的生命週期:從 Claude 的回應中讀取 tool_use 區塊、在您的回覆中格式化 tool_result 區塊,以及傳遞錯誤訊號。如需自動處理這些流程的 SDK 抽象層,請參閱 Tool Runner。



使用 Tool Runner 更簡單:本頁面所述的手動工具處理流程,可由 Tool Runner 自動管理。當您需要對工具執行進行自訂控制時,才需要參考本頁面。

Claude 的回應會根據其使用的是用戶端工具或伺服器工具而有所不同。

處理來自用戶端工具的結果

回應的 stop_reason 會是 tool_use,並包含一個或多個 tool_use 內容區塊,其中包括:

  • id:此特定工具使用區塊的唯一識別碼。稍後將用於比對工具結果。
  • name:正在使用的工具名稱。
  • input:一個物件,包含傳遞給工具的輸入,符合該工具的 input_schema。

當您收到用戶端工具的工具使用回應時,您應該:

  1. 從 tool_use 區塊中擷取 name、id 和 input。
  2. 在您的程式碼庫中執行對應該工具名稱的實際工具,並傳入工具的 input。
  3. 透過傳送一則 role 為 user 的新訊息來繼續對話,該訊息包含一個 tool_result 類型的 content 區塊,以及下列資訊:
    • tool_use_id:此結果所對應的工具使用請求的 id。
    • content(選用):工具的結果,可以是字串(例如 "content": "15 degrees")、巢狀內容區塊的清單(例如 "content": [{"type": "text", "text": "15 degrees"}]),或文件區塊的清單(例如 "content": [{"type": "document", "source": {"type": "text", "media_type": "text/plain", "data": "15 degrees"}}])。這些內容區塊可以使用 text、image、document 或 search_result 類型。
    • is_error(選用):如果工具執行導致錯誤,則設為 true。


重要的格式要求:

  • 工具結果區塊必須在訊息歷史記錄中緊接在其對應的工具使用區塊之後。您不能在助理的工具使用訊息與使用者的工具結果訊息之間插入任何訊息。
  • 在包含工具結果的使用者訊息中,tool_result 區塊必須位於 content 陣列的最前面。任何文字都必須放在所有工具結果之後。
  • 如果助理回合同時呼叫了尚無結果區塊的伺服器工具,則使用者訊息必須僅包含 tool_result 區塊。在結果之後加入文字會提前結束該回合;對於 Claude 直接呼叫的伺服器工具,該請求將會失敗並回傳 400 錯誤,指出未解決的伺服器工具名稱。請參閱停止原因與備援處理。

例如,以下寫法會導致 400 錯誤:

{
  "role": "user",
  "content": [
    { "type": "text", "text": "Here are the results:" }, // ❌ Text before tool_result
    { "type": "tool_result", "tool_use_id": "toolu_01" /* ... */ }
  ]
}

當助理回合僅呼叫用戶端工具時,以下寫法是正確的:

{
  "role": "user",
  "content": [
    { "type": "tool_result", "tool_use_id": "toolu_01" /* ... */ },
    { "type": "text", "text": "What should I do next?" } // ✅ Text after tool_result
  ]
}

如果您收到類似「tool_use ids were found without tool_result blocks immediately after」的錯誤,請檢查您的工具結果格式是否正確。



工具結果通常包含來自您無法控制之來源的內容:網頁、收到的電子郵件、使用者上傳的檔案、第三方 API。請將這些內容視為不受信任的內容:能夠影響這些內容的攻擊者可能會嵌入試圖重新導向 Claude 的指令(即「indirect prompt injection」(間接提示注入))。請將不受信任的內容保留在 tool_result 區塊中,而非 system 提示或一般使用者 text 區塊中,並參閱緩解越獄與提示注入以進一步強化防護。

收到工具結果後,Claude 將使用該資訊繼續生成對原始使用者提示的回應。

處理來自伺服器工具的結果

Claude 會在內部執行工具,並將結果直接整合到其回應中,無需額外的使用者互動。



一個回應可能同時包含用戶端的 tool_use 區塊,以及尚無結果區塊的 server_tool_use 區塊。該伺服器工具呼叫尚未完成,其結果區塊會在後續的回應中送達。請以僅包含用戶端工具 tool_result 區塊的使用者訊息回覆,並保持相同的 tools 陣列;對於 Claude 直接呼叫的伺服器工具,API 會在該請求中執行它,而下一個回應會以其結果區塊開始。請參閱停止原因與備援處理。



與其他 API 的差異

與將工具使用分離或使用 tool 或 function 等特殊角色的 API 不同,Claude API 將工具直接整合到 user 和 assistant 訊息結構中。

訊息包含 text、image、tool_use 和 tool_result 區塊的陣列。user 訊息包含用戶端內容和 tool_result,而 assistant 訊息包含 AI 生成的內容和 tool_use。

使用 is_error 處理錯誤

在搭配 Claude 使用工具時,可能會發生幾種不同類型的錯誤:

後續步驟

平行工具使用

處理 Claude 在單一回合中呼叫多個工具的回應。


Tool Runner (SDK)

讓 SDK 為您管理 tool_use 迴圈、結果格式化和重試。

定義工具

撰寫能引導 Claude 選擇正確工具的結構描述和說明。

Was this page helpful?

  • 處理來自用戶端工具的結果
  • 處理來自伺服器工具的結果
  • 使用 is_error 處理錯誤
  • 後續步驟