关于"zero data retention"(零数据保留),即 ZDR 如何适用于此功能,请参阅 API 与数据保留。
本页将逐步演示一个启用思考功能的完整两轮工具使用往返流程:Claude 进行思考、请求工具调用、接收结果并完成其回答,且在每一步都正确处理思考块。完整规则位于思考页面的工具使用中的思考和保留思考块部分;本页通过可运行的代码展示这些规则的应用。
每个链接都指向思考页面上的完整说明:
auto 或 none:在手动扩展思考(thinking: {type: "enabled"})下,强制工具使用的 tool_choice 选项会返回错误;自适应思考支持强制工具使用。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)捕获要回传的内容数组
在 Claude 选择进行思考的运行中,您应该会在响应内容中看到 thinking、text 和 tool_use 块(对于较简单的请求,自适应模式可能会跳过思考块)。请保持此内容数组完整:下一步将原样回传它。
要看到类似此输出的思考文本,请在请求中添加 display: "summarized"。在显示默认为省略的模型上(包括 claude-opus-4-8),thinking 字段否则会以空字符串返回,仅填充 signature。无论哪种方式,都要原样回传内容数组;请参阅控制思考显示。
{
"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}
# 第二次请求——包含助手轮次和 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?"},
# 原样回传收到的助手内容。当存在 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 以文本完成该轮次。由于在自适应模式下交错思考是自动的,续写也可能在最终文本之前以一个新的思考块开始:
{
"content": [
{
"type": "text",
"text": "Currently in Paris, the temperature is 88°F (31°C)"
}
]
}交错思考让 Claude 能够在工具调用之间进行思考,在对每个工具结果采取行动之前对其进行推理。其概念和各模型的可用性在思考页面的交错思考中有所介绍;交错改变的是思考块出现的位置,而不是工具调用能否链式进行。以下对比展示了交错思考在双工具工作流中带来的变化:
概述:开启思考、阅读思考输出,并查看工具使用、缓存和流式传输的完整规则。
通过努力程度级别和基于提示的引导来控制 Claude 思考的频率和深度。
旧模型上的手动思考预算:budget_tokens 机制以及向自适应模式的迁移。
Was this page helpful?