Claude 4 提示词工程最佳实践

docs.anthropic.com

本指南提供了针对 Claude 4 系列模型(Opus 4 与 Sonnet 4)的具体提示词工程技术,帮助你在应用中获得最佳效果。与旧版本相比,这一代模型在精准遵循指令方面表现更佳。

一、通用原则

1. 明确表达你的指令

Claude 4 对清晰、具体的指令反应很好。若想获得"超预期"的输出(类似旧版 Claude 的"惊喜"表现),往往需要更显式地提出要求。

示例:创建分析看板

不够有效 更加有效
Create an analytics dashboard Create an analytics dashboard. Include as many relevant features and interactions as possible. Go beyond the basics to create a fully-featured implementation.

2. 提供上下文以提升效果

在指令中说明动机或背景(即"为什么这么做很重要")能帮助 Claude 更好地理解目标,从而给出更贴合需求的回答。

示例:格式偏好

不够有效 更加有效
NEVER use ellipses Your response will be read aloud by a text-to-speech engine, so never use ellipses since the TTS engine will not know how to pronounce them.

3. 谨慎使用示例与细节

Claude 4 对示例及细节十分敏感。确保示例只展示你想要鼓励的行为,尽量避免包含你不希望出现的内容。

二、特定场景指南

1. 控制输出格式

以下方法对 Claude 4 的格式可控性尤为有效:

  1. 告诉 Claude "要做什么"而非"不要做什么"

    • Do not use markdown in your response
    • Your response should be composed of smoothly flowing prose paragraphs.
  2. 使用 XML 标签显式标注格式

    • 例如:Write the prose sections of your response in <smoothly_flowing_prose_paragraphs> tags.
  3. 在提示词中使用目标格式的写法

    • 你的提示词风格往往会影响模型输出的风格。若仍难以控制格式,可尝试去掉提示词中的 Markdown 以减少输出中的 Markdown 量。

2. 利用"思考"与"交错思考"能力

Claude 4 支持在工具调用后进行反思,对复杂多步骤推理特别有用。你可以引导其在获得工具结果后先思考再行动。

vbnet 复制代码
After receiving tool results, carefully reflect on their quality and determine optimal next steps before proceeding. Use your thinking to plan and iterate based on this new information, and then take the best next action.

3. 优化并行工具调用

Claude 4 在并行调用工具方面表现出色。以下提示可将并行成功率提升至近 100%:

css 复制代码
For maximum efficiency, whenever you need to perform multiple independent operations, invoke all relevant tools simultaneously rather than sequentially.

4. 减少代理式编码中的文件生成

在编码场景中,Claude 可能会创建临时文件做"草稿"。如果希望减少多余文件,可加入清理指令:

sql 复制代码
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. 提升视觉与前端代码生成

若需生成复杂、互动性强的前端设计,可通过鼓励性措辞来"放手"让 Claude 发挥:

vbnet 复制代码
Don't hold back. Give it your all.

并可附加更多聚焦点以进一步提升质量:

  • "Include as many relevant features and interactions as possible"
  • "Add thoughtful details like hover states, transitions, and micro-interactions"
  • "Create an impressive demonstration showcasing web development capabilities"
  • "Apply design principles: hierarchy, contrast, balance, and movement"

6. 避免只关注通过测试 & 硬编码

前沿语言模型有时会为"通过测试"而牺牲通用性。可在提示词中提醒其实现普适解、避免硬编码:

sql 复制代码
Please write a high-quality, general-purpose solution ... Do not hard-code values or create solutions that only work for specific test inputs ...

三、迁移注意事项(Sonnet 3.7 → Claude 4)

  1. 明确描述期望行为:在输出中希望看到什么,就直截了当地说出来。

  2. 使用"质量/细节增强"修饰语:例如

    csharp 复制代码
    Create an analytics dashboard. Include as many relevant features and interactions as possible ...
  3. 显式请求特定特性:如需动画、交互元素,请直接写在提示词里。


测试

将这些prompt放进去之后,看看Trae(国际版)一轮生成的效果。

确实不错。

相关推荐
Tencent_TCB8 小时前
Gemini CLI接入CloudBase-AI-Toolkit(MCP)保姆级教程
人工智能·ai·ai编程·云开发
yaocheng的ai分身11 小时前
开发者正在选择更老的AI模型——数据解释了原因
chatgpt·claude
油炸自行车12 小时前
【Qt】VS Code配置Qt UI插件,vscode打开Qt Designer ,vscode打开Qt*.ui文件
qt·ui·cursor·qt designer·vscode插件·trae·qt ui
腾讯云云开发12 小时前
3小时上线!云开发“零运维”外卖小程序指南
微信小程序·ai编程·小程序·云开发
前端中后台12 小时前
如何写好AI提示词?
ai编程
腾讯云云开发12 小时前
云开发1天极速开店!1人搞定全流程
微信小程序·ai编程·小程序·云开发
腾讯云云开发13 小时前
云开发模板建站,1小时速建官网,企业品牌即刻上线
微信小程序·ai编程·小程序·云开发
腾讯云云开发13 小时前
CodeBuddy+CloudBase 1小时开发微信小游戏
ai编程·小程序·云开发·codebuddy
腾讯云云开发13 小时前
手把手教你用CloudBase完成电商小程序全栈开发
微信小程序·ai编程·小程序·云开发
Jartto18 小时前
AI Coding 新范式:基于 Spec Coding 的新玩法
ai编程