關於「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)擷取要回傳的 content 陣列
在 Claude 選擇進行思考的執行中,您應該會在回應內容中看到 thinking、text 和 tool_use 區塊(對於較簡單的請求,自適應模式可能會跳過思考區塊)。請保持此 content 陣列完整無缺:下一步會將其原封不動地傳回。
若要看到像此輸出一樣的思考文字,請在請求中加入 display: "summarized"。在 display 預設為省略的模型上(包括 claude-opus-4-8),thinking 欄位否則會以空字串回傳,僅填入 signature。無論哪種情況,都要將 content 陣列原封不動地回傳;請參閱控制思考顯示。
{
"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?"}],
)
# 擷取工具使用區塊以取得其 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 以文字完成該輪次。由於交錯思考在自適應模式下是自動的,接續的回應也可能在最終文字之前以新的思考區塊開頭:
{
"content": [
{
"type": "text",
"text": "Currently in Paris, the temperature is 88°F (31°C)"
}
]
}交錯思考讓 Claude 能在工具呼叫之間進行思考,在採取行動之前先對每個工具結果進行推理。其概念和各模型的可用性在思考頁面的交錯思考中有所說明;交錯思考改變的是思考區塊出現的位置,而非工具呼叫是否可以串連。以下比較展示了交錯思考在雙工具工作流程中所帶來的變化:
總覽:開啟思考功能、讀取思考輸出,並查看工具使用、快取和串流的完整規則。
透過努力程度和基於提示的引導,控制 Claude 思考的頻率和深度。
舊版模型上的手動思考預算:budget_tokens 機制以及遷移至自適應思考。
Was this page helpful?