Get better AI results with prompt engineering techniques from the team behind Claude.
- Meet Claude
- PlatformBuild on ClaudeWorks with Claude
- SolutionsCompany size
- Resources
- Login
- Contact sales
- Try Claude
- Contact sales
- Try Claude
Get better AI results with prompt engineering techniques from the team behind Claude.
Context engineering has emerged as an increasingly important part of working with LLMs, with prompt engineering as its essential building block.上下文工程已成为与大型语言模型合作中越来越重要的一部分,而提示工程是其基本构建块。
Prompt engineering is the craft of structuring instructions to get better outputs from AI models. It's how you phrase queries, specify style, provide context, and guide the model's behavior to achieve your goals.提示工程是构建指令以从AI模型获得更好输出的技艺。它涉及如何措辞查询、指定风格、提供上下文以及引导模型行为以实现目标。
The difference between a vague instruction and a well-crafted prompt can mean the gap between generic outputs and exactly what you need. A poorly structured prompt might require multiple back-and-forth exchanges to clarify intent, while a well-engineered prompt gets you there in one shot.模糊指令与精心设计的提示之间的差异,可能意味着通用输出与您确切所需之间的差距。结构不良的提示可能需要多次来回交流才能澄清意图,而精心设计的提示则能一次性达成目标。
To help you get started, we've assembled some of our team's best practices, including practical methods designed to improve your results right away. We'll start with simple habits you can use today, then scale up to advanced methods for complex projects.为了帮助您入门,我们整理了一些团队的最佳实践,包括旨在立即改善结果的实际方法。我们将从今天即可使用的简单习惯开始,然后扩展到适用于复杂项目的高级方法。
At its most basic level, prompt engineering is just modifying the query you pass your LLM. Often it's simply adding information to the query before you make your actual request—but knowing which information is the right information to share is the secret to engineering a great and effective prompt.在最基本的层面上,提示工程只是修改您传递给LLM的查询。通常,它只是在提出实际请求之前向查询添加信息——但知道哪些信息是正确信息,是设计出优秀且有效提示的秘诀。
These prompt engineering techniques form the foundation of effective AI interactions. Use them consistently to see immediate improvements in response quality.这些提示工程技术构成了有效AI交互的基础。持续使用它们,您将立即看到响应质量的提升。
Modern AI models respond exceptionally well to clear, explicit instructions. Don't assume the model will infer what you want—state it directly. Use simple language that states exactly what you want without ambiguity.现代AI模型对清晰、明确的指令反应异常出色。不要假设模型会推断出您想要什么——直接说明。使用简单的语言,准确陈述您的需求,避免歧义。
The key principle: Tell the model exactly what you want to see. If you want comprehensive output, ask for it. If you want specific features, list them. Modern models like Claude benefit especially from explicit direction.关键原则:告诉模型您确切想看到什么。如果您想要全面的输出,就明确要求。如果您想要特定功能,就列出它们。像Claude这样的现代模型尤其受益于明确的指示。
Example: Creating an analytics dashboard示例:创建分析仪表板
Vague: "Create an analytics dashboard"模糊:“创建一个分析仪表板”
Explicit: "Create an analytics dashboard. Include as many relevant features and interactions as possible. Go beyond the basics to create a fully-featured implementation."明确:“创建一个分析仪表板。包含尽可能多的相关功能和交互。超越基础,实现一个功能完备的版本。”
The second version explicitly requests comprehensive features and signals that you want the model to go above and beyond the minimum.第二个版本明确要求全面的功能,并表明您希望模型超越最低限度。
Best practices:最佳实践:
Explaining why something matters helps AI models better understand your goals and deliver more targeted responses. This is particularly effective with newer models that can reason about your underlying objectives.解释某事为何重要,有助于AI模型更好地理解您的目标并提供更有针对性的响应。这对于能够推理您潜在目标的新模型尤其有效。
Example: Formatting preferences示例:格式偏好
Less effective: "NEVER use bullet points"效果较差:“永远不要使用项目符号”
More effective: "I prefer responses in natural paragraph form rather than bullet points because I find flowing prose easier to read and more conversational. Bullet points feel too formal and list-like for my casual learning style."效果更好:“我更喜欢自然段落形式的回复,而不是项目符号,因为我觉得流畅的散文更易读,也更适合对话。项目符号对于我随意的学习风格来说过于正式和列表化。”
The second version helps the model understand the reasoning behind the rule, which allows it to make better decisions about related formatting choices.第二个版本帮助模型理解规则背后的原因,从而使其能够就相关的格式选择做出更好的决策。
When to provide context:何时提供上下文:
Specificity in prompt engineering means structuring your instructions with explicit guidelines and requirements. The more specific you are about what you want, the better the results.提示工程中的具体性意味着用明确的指导方针和要求来构建指令。您对想要的内容越具体,结果就越好。
Example: Meal planning示例:膳食计划
Vague: "Create a meal plan for a Mediterranean diet"模糊:“为地中海饮食创建一个膳食计划”
Specific: "Design a Mediterranean diet meal plan for pre-diabetic management. 1,800 calories daily, emphasis on low glycemic foods. List breakfast, lunch, dinner, and one snack with complete nutritional breakdowns."具体:“设计一个针对糖尿病前期管理的地中海饮食膳食计划。每日1800卡路里,强调低血糖指数食物。列出早餐、午餐、晚餐和一份零食,并附上完整的营养分解。”
What makes a prompt specific enough?什么使提示足够具体?
Include:包括:
Examples aren't always necessary, but they shine when explaining concepts or demonstrating specific formats. Also known as one-shot or few-shot prompting, examples show rather than tell, clarifying subtle requirements that are difficult to express through description alone.示例并非总是必要,但在解释概念或演示特定格式时非常有用。也称为一次性或少量示例提示,示例通过展示而非告知来阐明难以仅通过描述表达的细微要求。
Important note for modern models: Claude 4.x and similar advanced models pay very close attention to details in examples. Ensure your examples align with the behaviors you want to encourage and minimize any patterns you want to avoid.现代模型的重要说明:Claude 4.x及类似的高级模型非常关注示例中的细节。确保您的示例与您希望鼓励的行为一致,并尽量减少您希望避免的模式。
Example: Article summarization示例:文章摘要
Without example: "Summarize this article"无示例:“总结这篇文章”
Here's an example of the summary style I want:
Article: [link to article about AI regulation]
Summary: EU passes comprehensive AI Act targeting high-risk systems. Key provisions include transparency requirements and human oversight mandates. Takes effect 2026.
Now summarize this article in the same style: [link to your new article]When to use examples:何时使用示例:
Pro tip: Start with one example (one-shot). Only add more examples (few-shot) if the output still doesn't match your needs.专业提示:从一个示例开始(一次性)。仅当输出仍不符合需求时,再添加更多示例(少量示例)。
Give the AI explicit permission to express uncertainty rather than guessing. This reduces hallucinations and increases reliability.明确允许AI表达不确定性,而不是猜测。这可以减少幻觉并提高可靠性。
Example: "Analyze this financial data and identify trends. If the data is insufficient to draw conclusions, say so rather than speculating."示例:“分析这些财务数据并识别趋势。如果数据不足以得出结论,请说明,而不是推测。”
This simple addition makes responses more trustworthy by allowing the model to acknowledge limitations.这个简单的添加使响应更值得信赖,因为它允许模型承认局限性。
Try these in Claude.在Claude中尝试这些。
These core habits will get you pretty far, but you may still encounter situations that require more sophisticated approaches. Advanced prompt engineering techniques shine when you're building agentic solutions, working with complex data structures, or need to break down multi-stage problems.这些核心习惯能让您走得很远,但您可能仍会遇到需要更复杂方法的情况。高级提示工程技术在您构建代理解决方案、处理复杂数据结构或需要分解多阶段问题时大放异彩。
Prefilling lets you start the AI's response for it, guiding format, tone, or structure. This technique is particularly powerful for enforcing output formats or skipping preambles.预填充让您为AI启动响应,从而引导格式、语气或结构。此技术对于强制输出格式或跳过开场白特别有效。
When to use prefilling:何时使用预填充:
Example: Enforcing JSON output示例:强制JSON输出
Without prefill, Claude might say: "Here's the JSON you requested: {...}"无预填充时,Claude可能会说:“这是您请求的JSON:{...}”
With prefill (API usage):使用预填充(API用法):
messages=[
{"role": "user", "content": "Extract the name and price from this product description into JSON."},
{"role": "assistant", "content": "{"}
]The AI will continue from the opening brace, outputting only valid JSON.AI将从左大括号继续,仅输出有效的JSON。
Note: In chat interfaces, you can approximate this by being very explicit: "Output only valid JSON with no preamble. Begin your response with an opening brace."注意:在聊天界面中,您可以通过非常明确的方式近似实现:“仅输出有效的JSON,无开场白。以左大括号开始您的响应。”
Chain of thought (CoT) prompting involves requesting step-by-step reasoning before answering. This technique helps with complex analytical tasks that benefit from structured thinking.思维链提示涉及在回答之前请求逐步推理。此技术有助于处理需要结构化思维的复杂分析任务。
Modern approach: Claude offers an extended thinking feature that automates structured reasoning. When available, extended thinking is generally preferable to manual chain of thought prompting. However, understanding manual CoT remains valuable for situations where extended thinking isn't available or when you need transparent reasoning you can review.现代方法:Claude提供了扩展思维功能,可自动进行结构化推理。当可用时,扩展思维通常优于手动思维链提示。然而,理解手动思维链在扩展思维不可用或需要可审查的透明推理时仍然有价值。
When to use chain of thought:何时使用思维链:
There are three common implementations of chain of thought:思维链有三种常见实现方式:
Basic chain of thought基本思维链
Simply add "Think step-by-step" to your instructions.只需在指令中添加“逐步思考”。
Draft personalized emails to donors asking for contributions to this year's Care for Kids program.
Program information:
<program>
{{PROGRAM_DETAILS}}
</program>
Donor information:
<donor>
{{DONOR_DETAILS}}
</donor>
Think step-by-step before you write the email.Guided chain of thought引导式思维链
Structure your prompt to provide specific reasoning stages.构建提示以提供特定的推理阶段。
Think before you write the email. First, think through what messaging might appeal to this donor given their donation history. Then, consider which aspects of the Care for Kids program would resonate with them. Finally, write the personalized donor email using your analysis.Structured chain of thought结构化思维链
Use tags to separate reasoning from the final answer.使用标签将推理与最终答案分开。
Think before you write the email in <thinking> tags. First, analyze what messaging would appeal to this donor. Then, identify relevant program aspects. Finally, write the personalized donor email in <email> tags, using your analysis.Note: Even when extended thinking is available, explicit CoT prompting can still be beneficial for complex tasks. The two approaches are complementary, not mutually exclusive.注意:即使扩展思维可用,明确的思维链提示对于复杂任务仍然有益。这两种方法是互补的,而非互斥的。
For modern AI models, there are several effective ways to control response formatting:对于现代AI模型,有几种有效的方法来控制响应格式:
1. Tell the AI what TO do instead of what NOT to do1. 告诉AI该做什么,而不是不该做什么
Instead of: "Do not use markdown in your response" Try: "Your response should be composed of smoothly flowing prose paragraphs"替代:“不要在响应中使用Markdown”尝试:“您的响应应由流畅的散文段落组成”
2. Match your prompt style to the desired output2. 使提示风格与期望输出匹配
The formatting style used in your prompt may influence the AI's response style. If you want minimal markdown, reduce markdown in your prompt.提示中使用的格式风格可能会影响AI的响应风格。如果您希望尽量减少Markdown,请减少提示中的Markdown。
3. Be explicit about formatting preferences3. 明确说明格式偏好
For detailed control over formatting:对于格式的详细控制:
When writing reports or analyses, write in clear, flowing prose using complete paragraphs. Use standard paragraph breaks for organization. Reserve markdown primarily for inline code, code blocks, and simple headings.
DO NOT use ordered lists or unordered lists unless you're presenting truly discrete items where a list format is the best option, or the user explicitly requests a list.
Instead of listing items with bullets, incorporate them naturally into sentences. Your goal is readable, flowing text that guides the reader naturally through ideas.Unlike the previous techniques, prompt chaining cannot be implemented in a single prompt. Chaining breaks down complex tasks into smaller sequential steps with separate prompts. Each prompt handles one stage, and the output feeds into the next instruction.与之前的技巧不同,提示链无法在单个提示中实现。链式分解将复杂任务分解为多个独立的顺序步骤,每个步骤使用单独的提示。每个提示处理一个阶段,输出馈入下一个指令。
This approach trades latency for higher accuracy by making each individual task easier. Typically this technique would be implemented using workflows or programmatically, but you could manually provide the prompts after receiving responses.这种方法通过使每个单独任务更简单,以延迟换取更高的准确性。通常,此技术将通过工作流或编程方式实现,但您也可以在收到响应后手动提供提示。
Example: Research summary示例:研究摘要
Each stage adds refinement through focused instruction.每个阶段通过聚焦指令增加改进。
When to use prompt chaining:何时使用提示链:
Trade-offs: Chaining increases latency (multiple API calls) but often dramatically improves accuracy and reliability for complex tasks.权衡:链式增加延迟(多次API调用),但通常显著提高复杂任务的准确性和可靠性。
Some prompt engineering techniques that were popular with earlier AI models are less necessary with models like Claude. However, you may still encounter them in older documentation or find them useful in specific situations.一些在早期AI模型中流行的提示工程技术,对于像Claude这样的模型已不那么必要。然而,您可能仍会在旧文档中遇到它们,或在特定情况下发现它们有用。
XML tags were once a recommended way to add structure and clarity to prompts, especially when incorporating large amounts of data. While modern models are better at understanding structure without XML tags, they can still be useful in specific situations.XML标签曾是为提示添加结构和清晰度的推荐方式,尤其是在包含大量数据时。虽然现代模型在没有XML标签的情况下也能更好地理解结构,但在特定情况下它们仍然有用。
Example:示例:
<athlete_information>
- Height: 6'2"
- Weight: 180 lbs
- Goal: Build muscle
- Dietary restrictions: Vegetarian
</athlete_information>
Generate a meal plan based on the athlete information above.When XML tags might still be helpful:XML标签可能仍有帮助的情况:
Modern alternative: For most use cases, clear headings, whitespace, and explicit language ("Using the athlete information below...") work just as well with less overhead.现代替代方案:对于大多数用例,清晰的标题、空白和明确的语言(“使用下面的运动员信息...”)效果同样好,且开销更少。
Role prompting defines expert personas and perspectives in how you phrase your query. While this can be effective, modern models are sophisticated enough that heavy-handed role prompting is often unnecessary.角色提示在您措辞查询时定义专家角色和视角。虽然这可能有效,但现代模型足够复杂,通常不需要过于强硬的角色提示。
Example: "You are a financial advisor. Analyze this investment portfolio..."示例:“您是一名财务顾问。分析这个投资组合...”
Important caveat: Don't over-constrain the role. "You are a helpful assistant" is often better than "You are a world-renowned expert who only speaks in technical jargon and never makes mistakes." Overly specific roles can limit the AI's helpfulness.重要警告:不要过度约束角色。“您是一个有用的助手”通常比“您是一位世界知名的专家,只说技术术语,从不犯错”更好。过于具体的角色可能会限制AI的有用性。
When role prompting might help:角色提示可能有帮助的情况:
Modern alternative: Often, being explicit about what perspective you want is more effective: "Analyze this investment portfolio, focusing on risk tolerance and long-term growth potential" rather than assigning a role.现代替代方案:通常,明确说明您想要的视角更有效:“分析这个投资组合,重点关注风险承受能力和长期增长潜力”,而不是分配角色。
Try in Claude.在Claude中尝试。
You've now seen individual techniques in isolation, but their real power emerges when you combine them strategically. The art of prompt engineering isn't using every technique available—it's selecting the right combination for your specific need.您现在已单独了解了各个技巧,但它们的真正力量在于战略性地组合使用。提示工程的艺术不是使用所有可用技巧,而是为您的特定需求选择正确的组合。
Example combining multiple techniques:组合多种技巧的示例:
xtract key financial metrics from this quarterly report and present them in JSON format.
I need this data for automated processing, so it's critical that your response contains ONLY valid JSON with no preamble or explanation.
Use this structure:
{
"revenue": "value with units",
"profit_margin": "percentage",
"growth_rate": "percentage"
}
If any metric is not clearly stated in the report, use null rather than guessing.
Begin your response with an opening brace: {This prompt combines:此提示结合了:
Not every prompt needs every technique. Here's a decision framework:并非每个提示都需要所有技巧。以下是一个决策框架:
Start here:从这里开始:
Technique selection guide:技巧选择指南:
Even well-intentioned prompts can produce unexpected results. Here are common issues and how to fix them:即使是精心设计的提示也可能产生意外结果。以下是常见问题及解决方法:
Pro tip: Start simple and add complexity only when needed. Test each addition to see if it actually improves results.专业提示:从简单开始,仅在需要时增加复杂性。测试每个添加项,看它是否确实改善了结果。
Learn from these common pitfalls to save time and improve your prompts:从这些常见陷阱中学习,以节省时间并改进您的提示:
One of the challenges of implementing advanced prompt engineering is that it adds context overhead through additional token usage. Examples, multiple prompts, detailed instructions—they all consume tokens, and context management is a skill in its own right.实施高级提示工程的挑战之一是,它通过额外的令牌使用增加了上下文开销。示例、多个提示、详细指令——它们都消耗令牌,而上下文管理本身就是一项技能。
Remember to use prompt engineering techniques when they make sense and justify their usage. For comprehensive guidance on managing context effectively, check out our blog post on context engineering.请记住,在合理且证明其用途的情况下使用提示工程技术。有关有效管理上下文的全面指导,请查看我们关于上下文工程的博客文章。
Context awareness improvements: Modern AI models, including Claude 4.x, have significantly improved context awareness capabilities that help address historical "lost-in-the-middle" issues where models struggled to attend equally to all parts of long contexts.上下文感知改进:现代AI模型,包括Claude 4.x,显著改进了上下文感知能力,有助于解决历史性的“中间迷失”问题,即模型难以平等关注长上下文的所有部分。
Why task-splitting still helps: Even with these improvements, breaking large tasks into smaller, discrete chunks remains a valuable technique—not because of context limitations, but because it helps the model focus on doing its best work within a very specific set of requirements and scope. A focused task with clear boundaries consistently produces higher quality results than trying to accomplish multiple objectives in a single prompt.为什么任务拆分仍然有帮助:即使有这些改进,将大任务分解为更小的离散块仍然是一种有价值的技术——不是因为上下文限制,而是因为它帮助模型专注于在非常具体的需求和范围内做到最好。一个具有明确边界的聚焦任务,始终比试图在单个提示中完成多个目标产生更高质量的结果。
Strategy: When working with long contexts, structure your information clearly with the most critical details at the beginning or end. When working with complex tasks, consider whether breaking them into focused subtasks would improve the quality and reliability of each component.策略:处理长上下文时,清晰组织信息,将最关键细节放在开头或结尾。处理复杂任务时,考虑是否将其分解为聚焦的子任务,以提高每个组件的质量和可靠性。
Prompt engineering is a skill, and it's going to take a few tries before you master it. The only way to know if you're doing it right is to test it and see. The first step is to just try it yourself. You'll see right away the differences between queries with and without the prompting techniques we covered here.提示工程是一项技能,需要几次尝试才能掌握。判断是否做对的唯一方法是测试并观察。第一步是亲自尝试。您会立即看到使用和未使用我们介绍的提示技巧的查询之间的差异。
To really hone your prompt engineering skills, you'll need to objectively measure the effectiveness of your prompts. The good news is that is exactly what is covered in our prompt engineering course at anthropic.skilljar.com.要真正磨练您的提示工程技能,您需要客观衡量提示的有效性。好消息是,这正是我们在anthropic.skilljar.com的提示工程课程中涵盖的内容。
Quick evaluation tips:快速评估技巧:
Prompt engineering is ultimately about communication: speaking the language that helps AI most clearly understand your intent. Start with the core techniques covered early in this guide. Use them consistently until they become second nature. Only layer in advanced techniques when they solve a specific problem.提示工程归根结底是沟通:用最能帮助AI清晰理解您意图的语言说话。从本指南早期介绍的核心技巧开始。持续使用它们,直到它们成为第二天性。仅在解决特定问题时才加入高级技巧。
Remember: the best prompt isn't the longest or most complex. It's the one that achieves your goals reliably with the minimum necessary structure. As you practice, you'll develop an intuition for which techniques suit which situations.请记住:最好的提示不是最长或最复杂的。而是以最少的必要结构可靠地实现目标的提示。随着练习,您将培养出对哪些技巧适合哪些情况的直觉。
The shift toward context engineering doesn't diminish prompt engineering's importance. In fact, prompt engineering is a fundamental building block within context engineering. Every well-crafted prompt becomes part of the larger context that shapes AI behavior, working alongside conversation history, attached files, and system instructions to create better outcomes.向上下文工程的转变并不会削弱提示工程的重要性。事实上,提示工程是上下文工程中的基本构建块。每个精心设计的提示都成为塑造AI行为的更大上下文的一部分,与对话历史、附加文件和系统指令一起,创造更好的结果。
Start prompting in Claude today.立即在Claude中开始提示。
Get the developer newsletter
Product updates, how-tos, community spotlights, and more. Delivered monthly to your inbox.