工具与多轮工作流中的思考
逐步演示一个完整的两轮工具使用往返流程,正确保留思考块,并了解交错思考如何改变流程。
本页逐步演示一个启用思考的完整两轮 tool use(工具使用)往返流程:Claude 进行思考、请求工具调用、接收结果并完成回答,且在每一步都正确处理 thinking blocks(思考块)。完整规则位于思考页面的思考与工具使用和保留思考块部分;本页展示这些规则在可运行代码中的应用。
本演示所应用的规则
每个链接都指向思考页面上的完整说明:
- 在手动模式下将工具选择限制为
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?