Skip to Content
模型用法Claude扩展思考

Claude 扩展思考

本页说明如何开启 Claude 的扩展思考并读取思考内容。模型 ID 与接口地址见 Claude 系列概览。

要点原生接口传 thinking,OpenAI 兼容接口传 reasoning 对象。开启思考时不要设置 temperature。

扩展思考让模型在回答前先进行推理,适合数学、代码与多步骤分析。实测以下模型在默认供应商下可稳定返回思考内容:Opus 5.5、Opus 5、Opus 4.8、Opus 4.6、Sonnet 4.6、Haiku 5.5、Haiku 4.5、Fable 5。

Anthropic 原生接口

两种模式:

模式写法说明
固定预算"thinking": {"type": "enabled", "budget_tokens": 2000}budget_tokens 至少为 1024,且必须小于 max_tokens
自适应"thinking": {"type": "adaptive"}由模型决定思考深度;可配合 "output_config": {"effort": "low"} 等调节
claude_thinking.py
import os import anthropic client = anthropic.Anthropic(base_url="https://api.ofox.ai/anthropic", api_key=os.environ["OFOX_API_KEY"]) message = client.messages.create( model="anthropic/claude-opus-5.5", max_tokens=4000, thinking={"type": "enabled", "budget_tokens": 2000}, messages=[{"role": "user", "content": "球拍和球共 1.10 美元,球拍比球贵 1 美元,球多少钱?"}], ) for block in message.content: if block.type == "thinking": print("[思考]", block.thinking) elif block.type == "text": print("[回答]", block.text)
  • 响应的 content 中先出现 thinking 块,再出现 text 块。thinking 块带有 signature,在多轮工具调用中需原样传回。
  • 流式输出时,思考内容通过 thinking_delta 事件返回,随后是一个 signature_delta 事件,再开始 text_delta。
  • 开启思考时不要设置 temperature(或设为 1),也不要设置 top_k。
  • 思考消耗的 token 计入 output_tokens,按输出单价计费。

OpenAI 兼容接口

通过 reasoning 对象开启,max_tokens 为思考预算:

claude_thinking_openai.py
import os from openai import OpenAI client = OpenAI(base_url="https://api.ofox.ai/v1", api_key=os.environ["OFOX_API_KEY"]) resp = client.chat.completions.create( model="anthropic/claude-sonnet-4.6", max_tokens=4000, extra_body={"reasoning": {"max_tokens": 2000}}, messages=[{"role": "user", "content": "球拍和球共 1.10 美元,球拍比球贵 1 美元,球多少钱?"}], ) msg = resp.choices[0].message details = getattr(msg, "reasoning_details", None) or [] print("[思考]", "".join(d.get("text", "") for d in details)) print("[回答]", msg.content)
  • 非流式响应中,思考内容位于 message.reasoning_details,请将各项的 text 依次拼接。
  • 流式响应中,思考内容位于 delta.reasoning_content。

其他报错见常见报错。

最后更新于