Claude Platform Docs
Messages思考

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

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

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

本演練所套用的規則

每個連結都指向思考頁面上的完整說明:

範例使用自適應思考;在僅支援擴展思考的模型上,請改用 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?