Claude 提示工程最佳实践
Claude官方提示工程综合指南,适用于Claude Opus 4.6、Sonnet 4.6和Haiku 4.5模型
概述
本文档是Claude最新模型的提示工程综合指南,涵盖基础技术、输出控制、工具使用、思考能力和智能体系统。
一、通用原则
1.1 清晰直接
Claude善于响应清晰、明确的指令。具体说明期望的输出格式可以提升效果。
黄金法则:把提示给一个对任务几乎无背景的同事看,如果他们会困惑,Claude也会困惑。
要点:
- 具体说明期望的输出格式和约束
- 当步骤顺序或完整性重要时,用编号列表或项目符号提供分步说明
1.2 添加上下文以提升性能
提供指令背后的上下文或动机,帮助Claude更好地理解目标,提供更有针对性的响应。
1.3 有效使用示例
示例是引导Claude输出格式、语气和结构最可靠的方式。精心设计的几个示例(少样本提示)可以显著提高准确性和一致性。
添加示例时,确保它们:
- 相关:紧密模拟实际用例
- 多样:覆盖边缘情况,有足够变化避免Claude产生非预期模式
- 结构化:用<example>标签包装示例(多个示例用<examples>标签)
1.4 使用XML标签构建提示
XML标签帮助Claude无歧义地解析复杂提示,特别是当提示混合了指令、上下文、示例和变量输入时。
最佳实践:
- 在整个提示中使用一致、描述性的标签名
- 当内容有自然层级时嵌套标签(如文档在<documents>内,每个在<document index="n">内)
1.5 给Claude一个角色
在系统提示中设置角色可以聚焦Claude针对用例的行为和语气。
message = client.messages.create(
model="claude-opus-4-6",
max_tokens=1024,
system="You are a helpful coding assistant specializing in Python.",
messages=[{"role": "user", "content": "How do I sort a list of dictionaries by key?"}]
)
1.6 长上下文提示
处理大型文档或数据丰富的输入(20K+ tokens)时:
- 将长格式数据放在顶部:把长文档和输入放在提示的顶部,在查询、指令和示例之上
- 用XML标签结构化文档内容:每个文档用<document>标签,包含<document_content>和<source>子标签
- 先引用相关内容:让Claude先引用文档的相关部分再执行任务
1.7 模型自我认知
如需Claude在应用中正确识别自己:
The assistant is Claude, created by Anthropic. The current model is Claude Opus 4.6.
二、输出与格式化
2.1 沟通风格
最新模型相比之前版本有更简洁自然的沟通风格:
- 更直接:提供基于事实的进度报告,而非自我庆祝
- 更会话化:更流畅和口语化
- 更简洁:为效率可能跳过详细总结
如果需要更多可见性:
After completing a task that involves tool use, provide a quick summary of the work you've done.
2.2 控制响应格式
有效方法:
- 告诉Claude要做什么,而非不要做什么
- ❌ “Do not use markdown in your response”
- ✅ “Your response should be composed of smoothly flowing prose paragraphs.”
- 使用XML格式指示器
- 匹配提示风格与期望输出风格
- 详细提示控制格式
示例 - 最小化markdown:
<avoid_excessive_markdown_and_bullet_points>
When, technical explanations, writing reports, documents analyses, or any long-form content, write in clear, flowing prose using complete paragraphs and sentences. Avoid using **bold** and *italics*. DO NOT use ordered lists or unordered lists unless...
</avoid_excessive_markdown_and_bullet_points>
2.3 LaTeX输出
Claude Opus 4.6默认使用LaTeX处理数学表达式。如需纯文本:
Format your response in plain text only. Do not use LaTeX, MathJax, or any markup notation.
三、工具使用
3.1 工具使用
最新模型经过精确指令遵循训练,受益于明确指示使用特定工具。
让Claude采取行动,要更明确:
Make changes to the files
3.2 主动行动
如需Claude默认更主动:
<default_to_action>
By default, implement changes rather than only suggesting them. If the user's intent is unclear, infer the most useful likely action and proceed.
</default_to_action>
如需更保守:
<do_not_act_before_instructions>
Do not jump into implementation or changes files unless clearly instructed to make changes.
</do_not_act_before_instructions>
3.3 优化并行工具调用
最新模型擅长并行工具执行:
- 运行多个并行搜索
- 同时读取多个文件
- 并行执行bash命令
最大化并行效率:
<use_parallel_tool_calls>
If you intend to call multiple tools and there are no dependencies between the tool calls, make all of the independent tool calls in parallel.
</use_parallel_tool_calls>
四、思考与推理
4.1 过度思考
Claude Opus 4.6比之前的模型进行更多前期探索。如果这不想要:
- 用更针对性的指令替换 blanket defaults
- 移除过度提示
- 使用较低effort设置
When you're deciding how to approach a problem, choose an approach and commit to it. Avoid revisiting decisions unless you encounter new information.
4.2 利用思考能力
- 自适应思考(Claude Opus 4.6):Claude动态决定何时思考及思考多少
- 扩展思考(Claude Sonnet 4.6):支持手动扩展思考和交错模式
引导思考行为:
After receiving tool results, carefully reflect on their quality and determine optimal next steps before proceeding.
减少不必要思考:
Extended thinking adds latency and should only be used when it will meaningfully improve answer quality.
五、智能体系统
5.1 长程推理与状态跟踪
最新模型擅长长程推理任务,具有出色的状态跟踪能力。Claude通过专注于增量进展来保持方向。
上下文感知:Claude 4.6和4.5具有上下文感知能力,可跟踪剩余上下文窗口。
多上下文窗口工作流:
- 第一个上下文窗口设置框架
- 用结构化格式创建测试
- 设置生活质量工具(init.sh等)
5.2 状态管理最佳实践
- 结构化格式:用JSON跟踪结构化信息
- 非结构化文本:自由形式笔记适合跟踪一般进度
- 使用git:Git提供已做工作的日志和可恢复的检查点
- 强调增量进展:明确要求Claude跟踪进度
5.3 平衡自主性与安全性
如需Claude在采取潜在风险行动前确认:
Consider the reversibility and potential impact of your actions. For actions that are hard to reverse or destructive, ask the user before proceeding.
5.4 研究与信息收集
- 提供明确的成功标准
- 鼓励来源验证
- 使用结构化方法
Search for this information in a structured way. As you gather data, develop several competing hypotheses. Track your confidence levels.
5.5 子智能体编排
最新模型展现出显著改进的原生子智能体编排能力。
确保子智能体工具定义清晰:让子智能体工具可用并在工具定义中描述
注意过度使用:Claude Opus 4.6对子智能体有强烈偏好,可能在简单直接方法足够的情况下spawn子智能体
5.6 减少智能体编码中的文件创建
如需最小化净新文件创建:
If you create any temporary new files, scripts, or helper files for iteration, clean up these files by removing them at the end of the task.
5.7 过度热情
Claude Opus 4.5和4.6有过度工程的倾向。如看到这种不良行为:
Avoid over-engineering. Only make changes that are directly requested or clearly necessary. Keep solutions simple and focused.
5.8 避免过度关注测试
Please write a high-quality, general-purpose solution using the standard tools available. Do not create helper scripts or workarounds to accomplish the task more efficiently.
5.9 最小化智能体编码中的幻觉
<investigate_before_answering>
Never speculate about code you have not opened. If the user references a specific file, you MUST read the file before answering.
</investigate_before_answering>
六、能力特定技巧
6.1 改进的视觉能力
Claude Opus 4.5和4.6相比之前的模型有改进的视觉能力。给Claude裁剪工具或skill可以提升性能。
6.2 前端设计
如需创建独特、有创意的前端:
<frontend_aesthetics>
You tend to converge toward generic, "on distribution" outputs. In frontend design, this creates what users call the "AI slop" aesthetic. Avoid this: make creative, distinctive frontends that surprise and delight.
</frontend_aesthetics>
七、迁移注意事项
从早期模型迁移到Claude 4.6
- 具体说明期望行为
- 用修饰语框架指令
- 明确请求特定功能
- 更新思考配置:使用自适应思考(
thinking: {type: "adaptive"}) - 远离预填充响应:4.6不再支持最后助手消息的预填充
- 调整反懒政提示:如果之前鼓励模型更彻底,现在要回拨
推荐effort设置
| 场景 | 设置 |
|---|---|
| 大多数应用 | Medium |
| 高流量或延迟敏感 | Low |
| 最难、最长程问题 | High |
八、总结
核心要点
- 清晰具体 - 明确说明期望,不要依赖模糊提示
- 使用示例 - 少样本提示显著提升效果
- XML结构化 - 标签帮助解析复杂提示
- 给角色 - 系统提示中设置角色
- 引导思考 - 用effort参数控制思考深度
- 平衡自主性 - 明确何时需要确认
翻译自 Claude官方文档
整理时间:2026-03-09