Claude Platform Docs
Messages思考

工具与多轮工作流中的思考

逐步演示一个完整的两轮工具使用往返流程,正确保留思考块,并了解交错思考如何改变流程。

本页逐步演示一个启用思考的完整两轮 tool use(工具使用)往返流程:Claude 进行思考、请求工具调用、接收结果并完成回答,且在每一步都正确处理 thinking blocks(思考块)。完整规则位于思考页面的思考与工具使用保留思考块部分;本页展示这些规则在可运行代码中的应用。

本演示所应用的规则

每个链接都指向思考页面上的完整说明:

示例使用自适应思考;在仅支持扩展思考的模型上,请替换为 thinking: {type: "enabled", budget_tokens: N}。往返规则完全相同。

逐步演示两轮工具使用往返流程

该示例定义了一个 get_weather 工具,让 Claude 思考并请求工具调用,然后返回工具结果,同时按接收到的原样回传助手轮次(包括思考块)。

  1. 发起第一个请求并提供可用工具

    发送一个启用自适应思考并定义了工具的请求。除了 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)
  2. 捕获要回传的 content 数组

    在 Claude 选择进行思考的运行中,您应该会在响应内容中看到 thinkingtexttool_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"
          }
        }
      ]
    }
  3. 返回工具结果,并原样回传助手轮次

    在您这一侧运行工具,然后发送第二个请求,向对话追加两条消息。第一条是按接收到的原样回传的助手内容,因此思考块与 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)
  4. 读取最终响应

    您应该会看到 Claude 以文本完成该轮次。由于 interleaved thinking(交错思考)在自适应模式下是自动的,因此续写内容也可能在最终文本之前以一个新的思考块开头,详见交错思考

    Output
    {
      "content": [
        {
          "type": "text",
          "text": "Currently in Paris, the temperature is 88°F (31°C)"
        }
      ]
    }

交错思考如何改变流程

交错思考让 Claude 能够在工具调用之间进行思考,在对每个工具结果采取行动之前先对其进行推理。其概念和各模型的可用性在思考页面的交错思考部分有介绍;交错改变的是思考块出现的位置,而不是工具调用能否链式进行。以下对比展示了交错思考在双工具工作流中带来的变化:

后续步骤

概述:开启思考、读取思考输出,并查看有关工具使用、缓存和流式传输的完整规则。

通过努力级别和基于提示的指导,引导 Claude 思考的频率和深度。

旧模型上的手动思考预算:budget_tokens 机制以及向自适应思考的迁移。

Was this page helpful?