适用于扩展思考的有效提示词
保持提示词简单,避免分步指令,相信模型自行推理。
适用于扩展思考的有效提示词 是 CoddyKit 上的免费 AI Prompt Engineering 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Prompt Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Prompt Engineering 课程共包含 4 节课。
提示推理模型的方法不同
您为标准模型建立的提示词经验——思维链、分步指令、少样本示例——在使用推理模型时往往会反而不利于您。
推理模型已经在进行复杂的内部推理。明确告诉它应该如何思考,可能会干扰这一过程。适用于推理模型的最佳提示词,比适用于标准模型的提示词更简单、更直接。
不要规定分步思考方式
对于标准模型,您可能会这样写:“请逐步思考。首先考虑 X,然后考虑 Y,最后得出结论 Z。”这种辅助结构很有帮助,因为标准模型不会自动执行这一过程。
对于推理模型,这种辅助结构可能会将模型的内部推理限制在次优路径上。相反,请清晰地说明问题,让模型自行决定如何进行推理。
# Standard model: needs scaffolding
STANDARD_PROMPT = (
'Let us think step by step.\n'
'First, identify the variables.\n'
'Then, set up the equation.\n'
'Then, solve for x.\n'
'Finally, verify your answer.\n\n'
'Problem: If 3x + 7 = 22, what is x?'
)
# Reasoning model: just state the problem clearly
REASONING_PROMPT = (
'Solve: If 3x + 7 = 22, what is x?'
# The model handles the step-by-step internally
)
# Both produce correct answers; the reasoning model prompt is simpler
print('Reasoning model prefers the cleaner prompt')清晰完整地说明问题
虽然您应当简化指导推理模型的方式,但对于您要询问的内容应当做到全面。请预先提供所有背景信息、约束条件和要求——模型会在内部推理过程中使用这些信息。
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
# Poor: vague problem statement
BAD_PROMPT = 'Write me a good sorting algorithm.'
# Good: clear, complete problem specification
GOOD_PROMPT = (
'Write a Python sorting algorithm with these requirements:\n'
'- Must sort a list of integers in ascending order\n'
'- Must work correctly on empty lists, single-element lists, and lists with duplicates\n'
'- Target time complexity: O(n log n) average case\n'
'- Must not use Python built-in sort() or sorted()\n'
'- Include a brief docstring and 3 test cases\n\n'
'Return only the code, no explanation.'
)
response = client.messages.create(
model='claude-opus-4-5',
max_tokens=8000,
thinking={'type': 'enabled', 'budget_tokens': 5000},
messages=[{'role': 'user', 'content': GOOD_PROMPT}]
)
print(next(b.text for b in response.content if b.type == 'text')[:300])恰当设置 budget_tokens
budget_tokens控制最大思考令牌数。正确设置它是调整推理模型的主要手段:
- 1,000—2,000:简单问题、快速计算
- 5,000—10,000:中等复杂度的编程、分析
- 16,000+:最困难的数学问题、复杂系统设计、研究级问题
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
def reasoning_call(prompt, budget_tokens=5000):
response = client.messages.create(
model='claude-opus-4-5',
max_tokens=budget_tokens + 2048, # max_tokens must exceed budget_tokens
thinking={
'type': 'enabled',
'budget_tokens': budget_tokens
},
messages=[{'role': 'user', 'content': prompt}]
)
answer = next((b.text for b in response.content if b.type == 'text'), '')
thinking_blocks = [b for b in response.content if b.type == 'thinking']
print(f'Thinking blocks: {len(thinking_blocks)}')
return answer
# Simple problem: small budget
reasoning_call('What is 17 * 23?', budget_tokens=1000)
# Complex problem: larger budget
reasoning_call(
'Design a distributed rate limiter that handles 100k requests/second.',
budget_tokens=10000
)精简的系统提示词
对于推理模型,请保持系统提示词精简。模型的内部推理是其主要能力——不要用冗长的行为指令过度限制它。
适用于推理模型的良好系统提示词应当:设定角色、定义输出格式、明确约束条件。仅此而已。
# Over-engineered system prompt (hurts reasoning models)
BAD_SYSTEM = (
'You are an expert Python developer. '
'Always think step by step. '
'First understand the problem. '
'Then plan your approach. '
'Then implement step by step. '
'Check each step before proceeding. '
'Finally review your solution. '
'Format all code with comments. '
'Add error handling to every function. '
'...'
)
# Minimal system prompt (helps reasoning models)
GOOD_SYSTEM = (
'You are an expert Python developer. '
'Return only code unless explanation is explicitly requested. '
'Use type hints and docstrings.'
)
# The model's internal reasoning handles the rest输出格式指令仍然重要
虽然您不应当指示模型如何推理,但应当清晰地指定所需的输出格式。这与推理指令不同——它告诉模型要返回什么,而不是要如何思考。
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
# Clear output format instructions are still important
prompt = (
'Analyze the time and space complexity of this Python function:\n\n'
'def bubble_sort(arr):\n'
' n = len(arr)\n'
' for i in range(n):\n'
' for j in range(0, n-i-1):\n'
' if arr[j] > arr[j+1]:\n'
' arr[j], arr[j+1] = arr[j+1], arr[j]\n\n'
'Return your answer as JSON with keys: '
'time_complexity, space_complexity, explanation (2 sentences max).'
)
response = client.messages.create(
model='claude-opus-4-5',
max_tokens=5000,
thinking={'type': 'enabled', 'budget_tokens': 3000},
messages=[{'role': 'user', 'content': prompt}]
)
print(next(b.text for b in response.content if b.type == 'text'))减少少样本示例
标准模型会从 3—5 个少样本示例中大获益。推理模型受益较少,而示例过多甚至可能产生负面影响,因为示例会用干扰内部推理的内容填满上下文窗口。
对于推理模型,使用 0—1 个示例通常最为理想。只有当输出格式不常见或存在歧义时,才使用示例。
# Standard model: 3 few-shot examples improve performance significantly
STANDARD_FEW_SHOT = (
'Q: 2 + 2 = ?\nA: 4\n\n'
'Q: 5 * 6 = ?\nA: 30\n\n'
'Q: 100 / 4 = ?\nA: 25\n\n'
'Q: 17 + 38 = ?\nA:'
)
# Reasoning model: 0 examples is fine; 1 is enough if format is unclear
REASONING_DIRECT = 'What is 17 + 38?'
# The reasoning model already knows math — examples are overhead, not signal
# Only use 1 example when the output format needs clarification:
REASONING_FORMAT_EXAMPLE = (
'Answer math questions returning only the number.\n'
'Example: Q: 2 + 2 A: 4\n\n'
'Q: 17 + 38'
)处理推理模型输出中的不确定性
推理模型比标准模型更可能表达真实的不确定性(因为它们确实进行过思考)。请构建应用,以便妥善处理措辞谨慎的回答。
import anthropic
import re
client = anthropic.Anthropic(api_key='sk-ant-...')
def reasoning_with_confidence(question):
prompt = (
f'{question}\n\n'
f'At the end of your answer, include a confidence statement: '
f'Confidence: [HIGH/MEDIUM/LOW] — [one sentence why]'
)
response = client.messages.create(
model='claude-opus-4-5',
max_tokens=8000,
thinking={'type': 'enabled', 'budget_tokens': 5000},
messages=[{'role': 'user', 'content': prompt}]
)
text = next(b.text for b in response.content if b.type == 'text')
# Parse confidence
match = re.search(r'Confidence: (HIGH|MEDIUM|LOW)', text)
confidence = match.group(1) if match else 'UNKNOWN'
print(f'Confidence: {confidence}')
return text, confidence
answer, conf = reasoning_with_confidence(
'What will AI capabilities look like in 2030?'
)缓存推理模型的输出
推理模型调用成本高且速度慢。对于重复或可预测的查询,请缓存结果。由于思考令牌可能非常长,缓存可以避免重复调用时再次承担延迟和成本。
import hashlib
import json
import os
cache_dir = '/tmp/reasoning_cache'
os.makedirs(cache_dir, exist_ok=True)
def cached_reasoning_call(prompt, budget_tokens=5000):
# Create cache key from prompt
key = hashlib.sha256(f'{prompt}:{budget_tokens}'.encode()).hexdigest()
cache_file = os.path.join(cache_dir, f'{key}.json')
if os.path.exists(cache_file):
with open(cache_file) as f:
cached = json.load(f)
print('Cache hit!')
return cached['answer']
# Cache miss: call the model
answer = reasoning_call(prompt, budget_tokens)
with open(cache_file, 'w') as f:
json.dump({'prompt': prompt, 'answer': answer}, f)
return answer验证推理模型的输出
推理模型出错的次数较少,但并非万无一失——尤其是在特定领域的事实或前沿主题方面。对于将在高风险场景中据此采取行动的输出,请始终进行验证。
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
def reasoning_with_verification(question):
# Step 1: Get reasoning model answer
r1 = client.messages.create(
model='claude-opus-4-5',
max_tokens=10000,
thinking={'type': 'enabled', 'budget_tokens': 8000},
messages=[{'role': 'user', 'content': question}]
)
answer = next(b.text for b in r1.content if b.type == 'text')
# Step 2: Independent verification call
verify_prompt = (
f'Question: {question}\n\n'
f'Proposed answer: {answer}\n\n'
f'Is this answer correct? Respond with CORRECT, INCORRECT, or UNCERTAIN, '
f'followed by a brief explanation.'
)
r2 = client.messages.create(
model='claude-opus-4-5',
max_tokens=256,
messages=[{'role': 'user', 'content': verify_prompt}]
)
verification = r2.content[0].text
print(f'Verification: {verification[:100]}')
return answer, verification推理模型提示词的实用检查清单
编写推理模型提示词时,请遵循以下检查清单:
- 清晰完整地说明问题
- 请勿包含逐步推理指令(NOT)
- 保持系统提示词简短(仅包含角色、格式和约束条件)
- 最多使用 0—1 个少样本示例
- 明确指定输出格式
- 根据问题复杂度按比例设置
budget_tokens - 为 10—60 秒的响应延迟做好准备
知识检查:推理模型提示词
与标准模型相比,为什么建议为推理模型使用更简单的提示词?
回顾:适用于扩展思考的有效提示词
推理模型需要比标准模型更简单、更直接的提示词。不要指示模型如何推理——请清晰完整地说明问题,然后让模型的内部审慎推理处理策略。保持系统提示词精简:仅包含角色、输出格式和约束条件。使用 0—1 个少样本示例。根据问题复杂度设置 budget_tokens(简单问题使用 1K,困难问题使用 10K+)。请为显著的延迟做好准备,并在可能时缓存结果。明确指定输出格式——这是仍然适合使用详细指令的部分。
常见问题解答
「适用于扩展思考的有效提示词」课时是免费的吗?
是的 — 「适用于扩展思考的有效提示词」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Prompt Engineering 课程的其余内容,请升级到 CoddyKit PRO。 AI Prompt Engineering 课程共包含 4 节课。
「适用于扩展思考的有效提示词」这节课中我会学到什么?
保持提示词简单,避免分步指令,相信模型自行推理。 你通过在浏览器中直接运行的动手代码来练习 AI Prompt Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Prompt Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Prompt Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「适用于扩展思考的有效提示词」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Prompt Engineering 课中编写并运行代码吗?
能。每节 AI Prompt Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 推理模型有何不同
- 适用于扩展思考的有效提示词
- 何时使用推理模型与标准模型
- 成本与延迟之间的权衡