推理模型有何不同
内部思维链与标准模型的区别:提示词编写者需要做出哪些调整。
推理模型有何不同 是 CoddyKit 上的免费 AI Prompt Engineering 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Prompt Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Prompt Engineering 课程共包含 4 节课。
什么是推理模型
推理模型是经过专门训练和配置、会在生成响应前进行较长时间内部推理的 LLM。示例包括 OpenAI 的 o1/o3/o4 系列,以及启用了扩展思考功能的 Anthropic 的 Claude。
标准模型会根据您的提示直接逐个令牌生成文本,而推理模型会先生成一段较长的内部思维链,再将其总结为最终答案。
标准模型与推理模型:您能看到什么
从 API 用户的角度来看,两者的区别是:
- 标准模型:输入 → 输出(快速、直接)
- 推理模型:输入 → [内部思考,隐藏或流式传输] → 输出(较慢,但处理困难问题时更准确)
思考过程是模型的私有草稿区,其中可能包含错误尝试、自我纠正和中间计算,这些内容都不会出现在最终答案中。
OpenAI o 系列:API 行为
OpenAI 的 o1/o3/o4 模型会在内部处理思考——默认情况下,您看不到推理令牌。您可以在使用情况元数据中查看思考令牌数量,但看不到其内容。
import openai
client = openai.OpenAI(api_key='sk-...')
# o3 — reasoning happens internally
response = client.chat.completions.create(
model='o3',
messages=[
{'role': 'user', 'content': 'Solve: A train leaves Chicago at 9am going 60mph. Another leaves NYC at 10am going 80mph. If the distance is 790 miles, when do they meet?'}
],
# reasoning_effort='high' # Optional: 'low', 'medium', 'high'
)
print(response.choices[0].message.content)
# Check how many tokens were used for thinking:
print('Input tokens:', response.usage.prompt_tokens)
print('Output tokens:', response.usage.completion_tokens)Claude 扩展思考:API 行为
Anthropic 的 Claude 会通过 API 暴露扩展思考令牌。您可以流式传输并实时观察模型的推理过程。思考内容会出现在最终响应之前。
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
# Enable extended thinking
response = client.messages.create(
model='claude-opus-4-5',
max_tokens=16000,
thinking={
'type': 'enabled',
'budget_tokens': 10000 # Max tokens for thinking
},
messages=[{
'role': 'user',
'content': 'What is the 100th prime number?'
}]
)
# Response contains both thinking blocks and text blocks
for block in response.content:
if block.type == 'thinking':
print('THINKING:', block.thinking[:200], '...')
elif block.type == 'text':
print('ANSWER:', block.text)流式传输思考令牌
您可以实时流式传输扩展思考,观察模型逐步展开的推理过程。这对于用户体验很有用——您可以显示“思考中”动画,或让高级用户观察推理过程。
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
def stream_with_thinking(question):
with client.messages.stream(
model='claude-opus-4-5',
max_tokens=16000,
thinking={'type': 'enabled', 'budget_tokens': 8000},
messages=[{'role': 'user', 'content': question}]
) as stream:
current_block_type = None
for event in stream:
# Track which block type we're in
if hasattr(event, 'type'):
if 'thinking' in str(event.type):
current_block_type = 'thinking'
elif 'text' in str(event.type):
current_block_type = 'text'
# Print text delta
if hasattr(event, 'delta') and hasattr(event.delta, 'text'):
prefix = '[THINK] ' if current_block_type == 'thinking' else '[ANS] '
print(prefix + event.delta.text, end='', flush=True)
stream_with_thinking('Explain why P != NP is unproven.')内部发生了什么:草稿区
模型的内部思考是一个草稿区,它可以在其中:
- 在做出决定前探索多种方法
- 进行计算并验证结果
- 发现自身错误并回溯
- 考虑边界情况
- 规划多步骤解决方案
正是这种内部推理,使推理模型在困难的数学、编程和战略规划任务上的表现远胜标准模型——它们本质上是在写答案前先进行文献综述。
budget_tokens:控制思考深度
budget_tokens(Claude)或 reasoning_effort(OpenAI)用于控制模型的思考量。思考越多,在困难问题上的准确率越高,但成本和延迟也会增加。
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
def ask_with_budget(question, budget):
r = client.messages.create(
model='claude-opus-4-5',
max_tokens=budget + 2048, # must exceed budget_tokens
thinking={'type': 'enabled', 'budget_tokens': budget},
messages=[{'role': 'user', 'content': question}]
)
thinking_tokens = sum(
len(b.thinking.split()) * 1.3 # rough estimate
for b in r.content if b.type == 'thinking'
)
answer = next(b.text for b in r.content if b.type == 'text')
return answer, int(thinking_tokens)
# Same hard question with different budgets
q = 'Prove that the square root of 2 is irrational.'
ans_small, tok_small = ask_with_budget(q, 1024)
ans_large, tok_large = ask_with_budget(q, 8000)
print(f'Small budget ({tok_small} thinking tokens): {ans_small[:100]}')
print(f'Large budget ({tok_large} thinking tokens): {ans_large[:100]}')温度与推理模型
推理模型对温度设置的处理方式不同:
- 对于 OpenAI o 系列:温度默认为 1,而且不一定能够更改
- 对于 Claude 的扩展思考:思考期间通常将温度设为 1
不要尝试使用温度来控制推理模型的行为——请改用 budget_tokens 或 reasoning_effort。
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
# For Claude with extended thinking, temperature=1 is the default
# and best practice
response = client.messages.create(
model='claude-opus-4-5',
max_tokens=10000,
temperature=1, # Required to be 1 when extended thinking is enabled
thinking={
'type': 'enabled',
'budget_tokens': 5000
},
messages=[{
'role': 'user',
'content': 'Write a Python function to find all prime numbers up to N.'
}]
)
print(response.content[-1].text[:300])性能基准:推理模型的优势领域
推理模型在以下任务上的表现明显优于标准模型:
- 竞赛数学:AIME、AMC(o3 接近人类专家水平)
- 复杂编程:竞赛编程(Codeforces)
- 科学推理:GPQA(研究生级科学问题)
- 多步逻辑:需要按顺序进行推导的问题
对于简单任务(语法、摘要、事实性问答),标准模型的表现同样出色,但成本低 10—100 倍。
延迟的现实情况
推理模型速度较慢。对于困难问题,如果推理力度较高,可能需要 30—60 秒。因此,请相应规划用户体验:
- 显示“正在思考……”等进度指示器
- 使用流式传输,在部分结果可用时立即显示
- 当延迟很重要时,不要将推理模型用于实时聊天
- 针对已知的困难问题,预先计算推理模型的输出
import time
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
def timed_reasoning_call(question, budget):
start = time.time()
response = client.messages.create(
model='claude-opus-4-5',
max_tokens=budget + 2000,
thinking={'type': 'enabled', 'budget_tokens': budget},
messages=[{'role': 'user', 'content': question}]
)
elapsed = time.time() - start
answer = next(b.text for b in response.content if b.type == 'text')
print(f'Latency: {elapsed:.1f}s | Answer: {answer[:100]}')
return answer
# Hard problem — expect 20-45 seconds
timed_reasoning_call(
'Design a database schema for a multi-tenant SaaS billing system.',
budget=8000
)推理模型与系统提示词
推理模型对系统提示词的响应方式不同于标准模型。由于它们会在回答前进行内部审慎推理,因此能够更可靠地遵循系统提示词中的复杂多步指令。
但是,过长且限制过多的系统提示词可能会与内部推理产生冲突。最佳实践是:为推理模型保持系统提示词简洁——定义角色和输出格式,然后让模型的内部推理处理策略。
知识检查:推理模型的思考方式
提示词编写者提供的内容,与推理模型内部实际发生的过程之间,关键区别是什么?
回顾:推理模型的不同之处
o1/o3 以及启用扩展思考的 Claude 等推理模型,会在响应前运行内部思维链。提示词编写者提供问题;模型在内部进行审慎推理,探索不同方法并自我纠正,然后生成最终答案。您可以使用 budget_tokens(Claude)或 reasoning_effort(OpenAI)控制思考深度。推理模型擅长复杂数学、编程和多步逻辑,但成本比标准模型高 10—100 倍,速度也慢 10—100 倍。请使用流式传输和进度指示器,在用户体验中管理延迟。
常见问题解答
「推理模型有何不同」课时是免费的吗?
是的 — 「推理模型有何不同」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Prompt Engineering 课程的其余内容,请升级到 CoddyKit PRO。 AI Prompt Engineering 课程共包含 4 节课。
「推理模型有何不同」这节课中我会学到什么?
内部思维链与标准模型的区别:提示词编写者需要做出哪些调整。 你通过在浏览器中直接运行的动手代码来练习 AI Prompt Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Prompt Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Prompt Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「推理模型有何不同」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Prompt Engineering 课中编写并运行代码吗?
能。每节 AI Prompt Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。