Building with extended thinking
使用 Extended Thinking 建構
Extended thinking gives Claude enhanced reasoning capabilities for complex tasks, while providing varying levels of transparency into its step-by-step thought process.
Extended thinking 為 Claude 提供增強的推理能力來處理複雜任務,同時對其逐步思考過程提供不同程度的透明度。
Supported models
支援的模型
- Claude Sonnet 4.5 (claude-sonnet-4-5-20250929)
- Claude Sonnet 4 (claude-sonnet-4-20250514)
- Claude Haiku 4.5 (claude-haiku-4-5-20251001)
- Claude Opus 4.5 (claude-opus-4-5-20251101)
- Claude Opus 4.1 (claude-opus-4-1-20250805)
- Claude Opus 4 (claude-opus-4-20250514)
- Claude Sonnet 4.5 (claude-sonnet-4-5-20250929)
- Claude Sonnet 4 (claude-sonnet-4-20250514)
- Claude Haiku 4.5 (claude-haiku-4-5-20251001)
- Claude Opus 4.5 (claude-opus-4-5-20251101)
- Claude Opus 4.1 (claude-opus-4-1-20250805)
- Claude Opus 4 (claude-opus-4-20250514)
How extended thinking works
Extended thinking 如何運作
When extended thinking is turned on, Claude creates thinking content blocks where it outputs its internal reasoning. Claude incorporates insights from this reasoning before crafting a final response.
當啟用 extended thinking 時,Claude 會建立 thinking 內容區塊來輸出其內部推理。Claude 會在生成最終回應之前納入這些推理的見解。
How to use extended thinking
如何使用 extended thinking
Add a thinking object with type set to enabled and budget_tokens to a specified token budget. The budget_tokens parameter determines the maximum number of tokens Claude is allowed to use for its internal reasoning.
新增一個 thinking 物件,將 type 設為 enabled,並將 budget_tokens 設為指定的 token 預算。budget_tokens 參數決定 Claude 可以用於內部推理的最大 token 數。
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=16000,
thinking={
"type": "enabled",
"budget_tokens": 10000
},
messages=[{
"role": "user",
"content": "Are there an infinite number of prime numbers such that n mod 4 == 3?"
}]
)
Summarized thinking
摘要思考
With extended thinking enabled, the Messages API for Claude 4 models returns a summary of Claude's full thinking process. You're charged for the full thinking tokens, not the summary tokens.
啟用 extended thinking 後,Claude 4 模型的 Messages API 會回傳 Claude 完整思考過程的摘要。你需要支付完整思考 token 的費用,而不是摘要 token。
Extended thinking with tool use
Extended thinking 與工具使用
Extended thinking can be used alongside tool use. During tool use, you must pass thinking blocks back to the API. Include the complete unmodified block to maintain reasoning continuity.
Extended thinking 可以與工具使用一起使用。在工具使用期間,你必須將 thinking 區塊傳回 API。包含完整未修改的區塊以維持推理連續性。
Best practices
最佳實踐
- Start with larger thinking budgets (16k+ tokens) for complex tasks.
- Use batch processing for thinking budgets above 32k to avoid networking issues.
- Monitor thinking token usage to optimize costs and performance.
- Use extended thinking for complex tasks like math, coding, and analysis.
- 對於複雜任務,從較大的思考預算(16k+ token)開始。
- 對於超過 32k 的思考預算,使用批次處理以避免網路問題。
- 監控思考 token 使用量以優化成本和效能。
- 對於數學、程式設計和分析等複雜任務使用 extended thinking。