Claude Platform Docs
Messages思考

工具與多輪工作流程中的思考

逐步演練一個完整的兩輪工具使用往返流程,正確保留思考區塊,並了解交錯思考如何改變流程。

本頁逐步演練一個啟用思考的完整兩輪「tool use」(工具使用)往返流程。Claude 進行思考、請求工具呼叫、接收結果,然後完成回答。在每一步中,您的程式碼都會正確處理思考區塊。完整規則位於思考頁面的搭配工具使用的思考與保留思考區塊中。

本演練所套用的規則

每條規則都連結至其完整說明:

範例使用自適應思考。在 Claude Opus 4.5、Claude Sonnet 4.5、Claude Haiku 4.5 及更早的 Claude 4 模型上,請改用 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-5-5",
        max_tokens=16000,
        thinking={"type": "adaptive"},
        tools=[weather_tool],
        messages=[{"role": "user", "content": "What's the weather in Paris?"}],
    )
    print(response)
  2. 擷取要回傳的 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"
          }
        }
      ]
    }
  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-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)
  4. 讀取最終回應

    您應該會看到 Claude 以文字完成該輪次。由於「interleaved thinking」(交錯思考)在自適應模式下是自動的,後續內容也可能在最終文字之前以新的思考區塊開頭:

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

交錯思考如何改變流程

交錯思考讓 Claude 能在工具呼叫之間進行思考,在對每個工具結果採取行動之前先進行推理。其概念與各模型的可用性在思考頁面的交錯思考中說明;交錯改變的是思考區塊出現的位置,而非工具呼叫能否串接。以下比較展示交錯思考在雙工具工作流程中所帶來的改變:

後續步驟

總覽:開啟思考、讀取思考輸出,並檢視工具使用、快取與串流的完整規則。

透過努力程度與基於提示的引導,控制 Claude 思考的頻率與深度。

手動思考預算:budget_tokens 機制以及遷移至自適應思考。

這個頁面有幫助嗎?