web_search_20250305
、text_editor_20250124
)來確保跨模型版本的相容性。為 Claude 提供工具和使用者提示
Claude 決定使用工具
stop_reason
為 tool_use
,表示 Claude 的意圖。執行工具並返回結果
tool_result
內容區塊的新 user
訊息中返回結果Claude 使用工具結果來制定回應
Claude 執行伺服器工具
Claude 使用伺服器工具結果來制定回應
單一工具範例
get_weather
函數,並在新的 user
訊息中返回結果:並行工具使用
多工具範例
get_weather
和 get_time
工具的範例,以及詢問兩者的使用者查詢。get_weather
,然後在收到天氣結果後呼叫 get_time
tool_use
區塊user
訊息中返回所有工具結果,每個結果都在自己的 tool_result
區塊中。缺少資訊
get_weather
工具,如果您問 Claude「天氣如何?」而沒有指定位置,Claude,特別是 Claude Sonnet,可能會對工具輸入進行猜測:順序工具
get_location
工具獲取使用者位置,然後將該位置傳遞給 get_weather
工具的範例:get_location
工具來獲取使用者的位置。在您在 tool_result
中返回位置後,Claude 會然後使用該位置呼叫 get_weather
來獲取最終答案。完整的對話可能看起來像這樣:角色 | 內容 |
---|---|
使用者 | 我所在地方的天氣如何? |
助理 | 我會先找到您目前的位置,然後檢查那裡的天氣。[使用 get_location 工具] |
使用者 | [get_location 的工具結果,匹配 id 和結果為 San Francisco, CA] |
助理 | [使用 get_weather 工具,輸入如下]{ “location”: “San Francisco, CA”, “unit”: “fahrenheit” } |
使用者 | [get_weather 的工具結果,匹配 id 和結果為 “59°F (15°C), mostly cloudy”] |
助理 | 根據您在舊金山,加州的目前位置,現在的天氣是 59°F (15°C) 且大部分多雲。這是一個相當涼爽和陰天的日子。如果您要外出,可能需要帶一件輕外套。 |
get_location
工具。get_location
函數,並在 tool_result
區塊中返回結果「San Francisco, CA」。get_weather
工具,傳入「San Francisco, CA」作為 location
參數(以及猜測的 unit
參數,因為 unit
不是必需參數)。get_weather
函數,並在另一個 tool_result
區塊中返回天氣資料。思維鏈工具使用
使用相關工具(如果可用)回答使用者的請求。在呼叫工具之前,進行一些分析。首先,思考提供的工具中哪個是回答使用者請求的相關工具。其次,檢查相關工具的每個必需參數,並確定使用者是否直接提供或給出足夠的資訊來推斷值。在決定是否可以推斷參數時,仔細考慮所有上下文,看它是否支持特定值。如果所有必需參數都存在或可以合理推斷,則繼續工具呼叫。但是,如果必需參數的值之一缺失,請不要調用函數(甚至不要為缺失的參數使用填充值),而是要求使用者提供缺失的參數。如果沒有提供可選參數,請不要要求更多資訊。
JSON 模式
tool_choice
(請參閱強制工具使用)來指示模型明確使用該工具input
傳遞給工具,所以工具的名稱和描述應該從模型的角度來看。record_summary
工具按照特定格式描述圖像。tools
parameter)tools
parameter in API requests (tool names, descriptions, and schemas)tool_use
content blocks in API requests and responsestool_result
content blocks in API requeststools
, we also automatically include a special system prompt for the model which enables tool use. The number of tool use tokens required for each model are listed below (excluding the additional tokens listed above). Note that the table assumes at least 1 tool is provided. If no tools
are provided, then a tool choice of none
uses 0 additional system prompt tokens.
Model | Tool choice | Tool use system prompt token count |
---|---|---|
Claude Opus 4.1 | auto , none any , tool | 346 tokens 313 tokens |
Claude Opus 4 | auto , none any , tool | 346 tokens 313 tokens |
Claude Sonnet 4 | auto , none any , tool | 346 tokens 313 tokens |
Claude Sonnet 3.7 | auto , none any , tool | 346 tokens 313 tokens |
Claude Sonnet 3.5 (Oct) (deprecated) | auto , none any , tool | 346 tokens 313 tokens |
Claude Sonnet 3.5 (June) (deprecated) | auto , none any , tool | 294 tokens 261 tokens |
Claude Haiku 3.5 | auto , none any , tool | 264 tokens 340 tokens |
Claude Opus 3 (deprecated) | auto , none any , tool | 530 tokens 281 tokens |
Claude Sonnet 3 | auto , none any , tool | 159 tokens 235 tokens |
Claude Haiku 3 | auto , none any , tool | 264 tokens 340 tokens |
usage
指標的一部分。