工具與多輪工作流程中的思考
逐步演練一個完整的兩輪工具使用往返流程,正確保留思考區塊,並了解交錯思考如何改變流程。
本頁逐步演練一個啟用思考的完整兩輪「tool use」(工具使用)往返流程:Claude 進行思考、請求工具呼叫、接收結果,然後完成回答,且每一步都正確處理思考區塊。完整規則位於思考頁面的搭配工具使用的思考與保留思考區塊中;本頁展示這些規則在可執行程式碼中的應用。
本演練所套用的規則
每個連結都指向思考頁面上的完整說明:
- 在手動模式下將工具選擇限制為
auto或none:強制工具使用的tool_choice選項在手動「extended thinking」(擴展思考)(thinking: {type: "enabled"})下會回傳錯誤;自適應思考則支援強制工具使用。 - 每個助手輪次維持一種思考設定:一個工具使用迴圈即為一個助手輪次,因此僅在輪次之間變更設定。
- 完整且未經修改地傳回思考區塊:當您回傳工具結果時,助手訊息中的思考區塊必須一併傳回。
- 原樣回傳所接收的助手訊息:重建訊息或過濾掉
redacted_thinking區塊會觸發 400 錯誤。
範例使用自適應思考;在僅支援擴展思考的模型上,請改用 thinking: {type: "enabled", budget_tokens: N}。往返規則完全相同。
逐步演練兩輪工具使用往返流程
此範例定義一個 get_weather 工具,讓 Claude 思考並請求工具呼叫,然後回傳工具結果,並將助手輪次原樣回傳(包含思考區塊)。
發出第一個請求並提供可用工具
發送一個啟用自適應思考並定義了工具的請求。除了
thinking參數之外,這是一個標準的工具使用請求:client = 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-4-8", max_tokens=16000, thinking={"type": "adaptive"}, tools=[weather_tool], messages=[{"role": "user", "content": "What's the weather in Paris?"}], ) print(response)擷取要回傳的 content 陣列
在 Claude 選擇思考的執行中,您應該會在回應內容中看到
thinking、text和tool_use區塊(對於較簡單的請求,自適應模式可能會略過思考區塊)。請保持此 content 陣列完整:下一步會將其逐字傳回。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": "text", "text": "I can help you get the current weather information for Paris. Let me check that for you" }, { "type": "tool_use", "id": "toolu_01CswdEQBMshySk6Y9DFKrfq", "name": "get_weather", "input": { "location": "Paris" } } ] }回傳工具結果,並逐字回傳助手輪次
在您這端執行工具,然後發送第二個請求,在對話中附加兩則訊息。第一則是原樣回傳所接收的助手內容,因此思考區塊與
tool_use區塊一同保持不變。第二則是攜帶tool_result的使用者訊息。每個範例都是獨立的腳本:它會重複第一個請求,然後立即使用剛收到的回應發送後續請求。
client = 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-4-8", max_tokens=16000, thinking={"type": "adaptive"}, tools=[weather_tool], messages=[{"role": "user", "content": "What's the weather in Paris?"}], ) # 擷取 tool_use 區塊以取得其 ID,供 tool_result 使用 tool_use_block = next(block for block in response.content if block.type == "tool_use") # 呼叫您實際的天氣 API,此處即為您實際 API 呼叫的位置 # 假設這是我們取得的回傳結果 weather_data = {"temperature": 88} # 第二次請求 - 包含 assistant 回合與 tool_result continuation = client.messages.create( model="claude-opus-4-8", max_tokens=16000, thinking={"type": "adaptive"}, tools=[weather_tool], messages=[ {"role": "user", "content": "What's the weather in Paris?"}, # 原封不動地回傳 assistant 內容。當存在 thinking # 區塊時,它必須與 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後續步驟
Was this page helpful?