工具與多輪工作流程中的思考
逐步演練一個完整的兩輪工具使用往返流程,正確保留思考區塊,並了解交錯思考如何改變流程。
本頁逐步演練一個啟用思考的完整兩輪「tool use」(工具使用)往返流程。Claude 進行思考、請求工具呼叫、接收結果,然後完成回答。在每一步中,您的程式碼都會正確處理思考區塊。完整規則位於思考頁面的搭配工具使用的思考與保留思考區塊中。
本演練所套用的規則
每條規則都連結至其完整說明:
- 在手動模式下將工具選擇限制為
auto或none:在手動「extended thinking」(擴展思考)(thinking: {type: "enabled"})下,強制工具使用的tool_choice選項會傳回錯誤。 - 在自適應模式下確認您的模型對強制工具使用的處理方式:使用自適應思考時,結果取決於模型:
- Claude Opus 5.5、Claude Sonnet 5.5、Claude Fable 5.2、Claude Mythos 5.2、Claude Fable 5.1 和 Claude Mythos 5.1: 強制工具使用會傳回 400 錯誤。
- 其他模型: 接受強制工具使用,且會略過思考。回應會直接以工具呼叫開始。
- 每個助手輪次維持一種思考設定:一個工具使用迴圈即為一個助手輪次,因此僅在輪次之間變更設定。
- 完整且未經修改地傳回思考區塊:當您回傳工具結果時,助手訊息中的思考區塊必須一併傳回。
- 原樣回傳所接收的助手訊息:捨棄思考區塊會遺失其中的推理,即使沒有錯誤也是如此。
範例使用自適應思考。在 Claude Opus 4.5、Claude Sonnet 4.5、Claude Haiku 4.5 及更早的 Claude 4 模型上,請改用 thinking: {type: "enabled", budget_tokens: N}。請以相同方式回傳助手輪次。
逐步演練兩輪工具使用往返流程
此範例定義一個 get_weather 工具,讓 Claude 思考並請求工具呼叫,然後回傳工具結果,並將助手輪次原樣回傳(包含思考區塊)。
發出第一個請求並提供可用工具
發送一個啟用自適應思考並定義了工具的請求。除了
thinking參數之外,這是一個標準的工具使用請求:Pythonclient = anthropic.Anthropic() weather_tool = { "name": "get_weather", "description": "Get current weather for a location", "input_schema": { "type": "object", "properties": {"location": {"type": "string", "description": "City name"}}, "required": ["location"], }, } # 第一個請求:Claude 以思考內容與工具請求作為回應 response = client.messages.create( model="claude-opus-5-5", max_tokens=16000, thinking={"type": "adaptive"}, tools=[weather_tool], messages=[{"role": "user", "content": "What's the weather in Paris?"}], ) print(response)擷取要回傳的 content 陣列
在包含思考的執行中,您應該會在回應內容中看到
thinking和tool_use區塊(在較簡單的請求中,自適應模式可能會略過思考區塊)。Claude 在工具呼叫前撰寫的說明會以text區塊傳回;若模型會撰寫進度更新,則會以另一個thinking區塊傳回。請保持此內容陣列完整:下一個步驟會原封不動地將其傳回。Output{ "content": [ { "type": "thinking", "thinking": "The user wants to know the current weather in Paris. I have access to a function `get_weather`...", "signature": "BDaL4VrbR2Oj0hO4XpJxT28J5T...." }, { "type": "tool_use", "id": "toolu_01CswdEQBMshySk6Y9DFKrfq", "name": "get_weather", "input": { "location": "Paris" } } ] }回傳工具結果,並逐字回傳助手輪次
在您這端執行工具,然後發送第二個請求,在對話中附加兩則訊息。第一則是原樣回傳所接收的助手內容,因此思考區塊與
tool_use區塊一同保持不變。第二則是攜帶tool_result的使用者訊息。每個範例都是獨立的腳本:它會重複第一個請求,然後立即使用剛收到的回應發送後續請求。
Pythonclient = anthropic.Anthropic() weather_tool = { "name": "get_weather", "description": "Get current weather for a location", "input_schema": { "type": "object", "properties": {"location": {"type": "string", "description": "City name"}}, "required": ["location"], }, } response = client.messages.create( model="claude-opus-5-5", max_tokens=16000, thinking={"type": "adaptive"}, tools=[weather_tool], messages=[{"role": "user", "content": "What's the weather in Paris?"}], ) # 擷取工具使用區塊以取得其 ID,供工具結果使用 tool_use_block = next(block for block in response.content if block.type == "tool_use") # 實際呼叫天氣 API 的程式碼應放在此處 # 假設這是天氣 API 傳回的結果 weather_data = {"temperature": 88} # 第二個請求:包含助理輪次與工具結果 continuation = client.messages.create( model="claude-opus-5-5", max_tokens=16000, thinking={"type": "adaptive"}, tools=[weather_tool], messages=[ {"role": "user", "content": "What's the weather in Paris?"}, # 原封不動地傳回收到的助理內容。當存在思考 # 區塊時,它必須與 tool_use 區塊一同附上。 {"role": "assistant", "content": response.content}, { "role": "user", "content": [ { "type": "tool_result", "tool_use_id": tool_use_block.id, "content": f"Current temperature: {weather_data['temperature']}°F", } ], }, ], ) print(continuation)讀取最終回應
您應該會看到 Claude 以文字完成該輪次。由於「interleaved thinking」(交錯思考)在自適應模式下是自動的,後續內容也可能在最終文字之前以新的思考區塊開頭:
Output{ "content": [ { "type": "text", "text": "Currently in Paris, the temperature is 88°F (31°C)" } ] }
交錯思考如何改變流程
交錯思考讓 Claude 能在工具呼叫之間進行思考,在對每個工具結果採取行動之前先進行推理。其概念與各模型的可用性在思考頁面的交錯思考中說明;交錯改變的是思考區塊出現的位置,而非工具呼叫能否串接。以下比較展示交錯思考在雙工具工作流程中所帶來的改變:
不使用交錯思考時,Claude 在助手輪次開始時思考一次。收到工具結果後的後續回應會繼續進行,而不會產生新的思考區塊。
User: "What's the total revenue if we sold 150 units at $50 each,
and how does this compare to our average monthly revenue?"
Response 1: [thinking] "I need to calculate 150 * $50, then check the database..."
[tool_use: calculator] { "expression": "150 * 50" }
↓ tool result: "7500"
Response 2: [tool_use: database_query] { "query": "SELECT AVG(revenue)..." }
↑ no thinking block
↓ tool result: "5200"
Response 3: [text] "The total revenue is $7,500, which is 44% above your
average monthly revenue of $5,200."
↑ no thinking block啟用交錯思考後,Claude 可以在收到每個工具結果後進行思考,使其能在繼續之前對中間結果進行推理。
User: "What's the total revenue if we sold 150 units at $50 each,
and how does this compare to our average monthly revenue?"
Response 1: [thinking] "I need to calculate 150 * $50 first..."
[tool_use: calculator] { "expression": "150 * 50" }
↓ tool result: "7500"
Response 2: [thinking] "Got $7,500. Now I should query the database to compare..."
[tool_use: database_query] { "query": "SELECT AVG(revenue)..." }
↑ thinking after receiving calculator result
↓ tool result: "5200"
Response 3: [thinking] "$7,500 vs $5,200 average - that's a 44% increase..."
[text] "The total revenue is $7,500, which is 44% above your
average monthly revenue of $5,200."
↑ thinking before final answer後續步驟
這個頁面有幫助嗎?