Prompt Engineering is a core technique for improving the quality and control of large-scale language model (LLM) output by optimizing the input text (i.e., "prompts"). This comprehensive guide provides a systematic introduction to the techniques used in conjunction with Claude
A series of best practices for each of the model interactions, covering the entire spectrum from basic preparation to advanced application, to help developers and researchers maximize the Claude
potential.
Before you start: the basis for success
Before delving into specific tips engineering techniques, a solid foundation must be laid. This includes:
- Clear success criteria: Clearly define what the success criteria are for the use case.
- Effective testing methods: Establish a methodology that can objectively and quantitatively assess whether model outputs meet these criteria.
- Initial draft prompts: There is an initial version hint that you want to improve.
If these preparations have not been completed, it is highly recommended to invest the time to set them up first.
When to choose a cue project? (contrast fine-tuning)
Not all problems are best solved by cue engineering. For example, latency and cost issues can sometimes be improved more easily by choosing a different model. Whereas cue engineering and fine-tuning are the two main tools when it comes to controlling model behavior, here are some reasons why cue engineering is better than fine-tuning in a variety of scenarios:
- Resource efficiency: Fine-tuning requires a lot of computational resources (e.g., high-end GPUs) and memory, whereas cue engineering requires only text input and is much more resource-friendly.
- cost-effectiveness: For scenarios using cloud-based AI services, fine-tuning incurs significant training costs. Tip engineering utilizes the base model directly, which is usually less costly.
- Model Update Compatibility: When the model provider updates the base model, the fine-tuned version may need to be retrained. Whereas well-designed hints usually continue to work across versions.
- time efficiency: The fine-tuning process can take hours or even days. In contrast, cue engineering provides near-instant feedback, dramatically accelerating the speed of iteration and problem solving.
- Very low data requirements: Fine-tuning requires large task-specific, labeled datasets that may be difficult or costly to obtain. Cue engineering works effectively in few-shot or even zero-shot scenarios.
- Flexibility and Rapid Iteration: Developers can quickly try multiple approaches, adjust cues and observe results immediately. This agility for rapid experimentation is difficult to achieve with fine-tuning.
- Domain Adaptation: By providing domain-specific context in the hints, models can be easily adapted to new domains without retraining.
- Comprehension Improvement: Cue engineering is far more effective than fine-tuning in helping models better understand and utilize external content (e.g., documents retrieved via RAG).
- Retention of generic knowledge: Fine-tuning carries the risk of "catastrophic forgetting", i.e., the model may lose its original generalized capabilities as it learns new knowledge. Cue engineering preserves the model's broad knowledge base.
- Transparency and interpretability: Hints are human-readable and clearly show the exact information received by the model. This transparency helps in understanding and debugging the model's behavior.
Claude 4
Tip Engineering Core Principles
Claude 4
Series models (including Opus 4.1, Opus 4, and Sonnet 4) are optimized to follow instructions more precisely. For best results, follow these core principles.
1. Clear and direct instructions
Models respond best to clear, specific instructions. Explicitly stating the desired output can significantly improve the quality of results. If the model is expected to behave like the old Claude
As with "over-performing" behaviors, these behaviors now need to be more explicitly requested.
Example: Creating an Analytics Dashboard
- Less effective tips::
创建一个分析仪表盘。
- Tips for better results::
创建一个分析仪表盘。请包含尽可能多的相关功能和交互细节,超越基础功能,实现一个功能完备的设计。
2. Supplementing context to improve performance
Provide context or motivation behind the directive, such as to the Claude
Explaining why a particular behavior is important can help the model better understand the target and provide a more targeted response.
Example: Formatting Preferences
- Less effective tips::
绝不使用省略号。
- Tips for better results::
你的回答将被文本转语音引擎朗读,所以绝不使用省略号,因为引擎不知道如何发音。
The model is intelligent enough to generalize from such explanations.
3. Careful use of examples and details
Claude 4
The model takes every detail and example in the instructions seriously. Please make sure that the examples you provide are consistent with the behaviors you expect to encourage and minimize the behaviors you want to avoid.
Core Tips Engineering Techniques
The following techniques are organized in order from most general to more specialized. It is recommended to try them in this order when solving performance problems.
- clear and direct instructions (as mentioned above)
- Example of use (multi-sample tip)
- Guided Model Thinking (Chain of Thought)
- Using XML Tags
- Empowering characters through system prompts
- Pre-populated model responses
- Chained Complex Hints
- long context cue technique
- Using the Tip Generator
The most critical of these techniques are described in detail below.
Using XML tags to build structured prompts
When the prompt contains multiple components (such as background information, instructions, examples, and input data), using XML tags is a great way to enhance the Claude
Parsing Accuracy Sharpshooters.
- Benefits of using XML tags::
- clarity: Clearly separate the different parts of the cue to ensure good structure.
- accuracy: Reduce model errors caused by confusing instructions with context.
- dexterity: Easy to find, add, remove or modify specific parts of the prompt.
- resolvability: Requiring models to also use XML tags in their output can make it easier for programs to extract specific parts of their responses.
- best practice::
- Maintaining consistency: Always use the same tag names in prompts and refer to them in commands (e.g., "Use the
<contract>
Contracts in the tab..."). - nested label: For hierarchical content, you can use nested tags such as
<outer><inner></inner></outer>
The
- Maintaining consistency: Always use the same tag names in prompts and refer to them in commands (e.g., "Use the
Example: Legal Contract Analysis
Tips for not using XML tags::
分析这份软件许可协议的潜在风险和责任:{{CONTRACT}}。关注赔偿、责任限制和知识产权所有权条款。同时,注意任何不寻常或令人担忧的条款。这是我们的标准合同以供参考:{{STANDARD_CONTRACT}}。给出调查结果摘要和给我们法律团队的建议。
in the end: The analysis can be disorganized and key points can easily be missed.
Tips for using XML tags::
分析这份软件许可协议的法律风险和责任。
我们是一家跨国企业,正在考虑将此协议用于我们的核心数据基础设施。
<agreement>
{{CONTRACT}}
</agreement>
这是我们的标准合同以供参考:
<standard_contract>
{{STANDARD_CONTRACT}}
</standard_contract>
<instructions>
1. 分析以下条款:
- 赔偿 (Indemnification)
- 责任限制 (Limitation of liability)
- 知识产权所有权 (IP ownership)
2. 指出不寻常或令人担忧的条款。
3. 与我们的标准合同进行比较。
4. 在 <findings> 标签中总结发现。
5. 在 <recommendations> 标签中列出可行的建议。
</instructions>
in the end: The model outputs a well-structured <findings>
cap (a poem) <recommendations>
It is detailed and logical, making it easy for the legal team to adopt it directly.
Role Prompting
utilization system
parameters are Claude
Setting up a character is one of the most powerful ways to boost its performance. A proper character can turn the Claude
Transform from a generalist assistant to a virtual domain expert.
- Benefits of using system prompts to set up your character::
- Enhanced accuracy: In complex scenarios such as legal analysis or financial modeling, role prompts can significantly improve
Claude
The performance. - Customized tone of voiceRole Cues: Whether you need a CFO to be succinct or a copywriter to be lively, Role Cues can be adjusted
Claude
communication style. - Enhancing Focus: After setting the roles
Claude
would be better able to limit themselves to the specific requirements of the task.
- Enhanced accuracy: In complex scenarios such as legal analysis or financial modeling, role prompts can significantly improve
- How to realize::
In the Messages API call, put the role description into thesystem
Parameters. Reserve specific task instructions in theuser
Message.import anthropic client = anthropic.Anthropic() response = client.messages.create( model="claude-3-opus-20240229", max_tokens=2048, system="你是一家高增长B2B SaaS公司的CFO。我们的投资者希望实现积极增长,但对我们的烧钱率保持警惕。", # <-- 赋予角色 messages=[ {"role": "user", "content": "在董事会议上,请分析我们Q2的财务数据,指出关键趋势,标记关注点,并推荐战略行动:\n\n{{FINANCIALS}}"} ] ) print(response.content)
Expert Tips: Try different roles! For the same data.数据科学家
,市场营销策略师
maybe 专为财富500强公司提供客户洞察分析的数据科学家
A very different insight may be reached.
Guided Model Thinking (Chain of Thought, Chain of Thought)
When faced with complex tasks such as researching, analyzing, or solving problems, giving Claude
space to think can dramatically improve their performance. This technique is known as Chain of Thought (CoT) prompting, and it encourages the Claude
Step-by-step disassembly of the problem results in more accurate and refined output.
- When do you use thought chains?
- Tasks to think about: Used for tasks that require humans to think as well, such as complex mathematical calculations, multi-step analysis, writing complex documents, or decisions that require weighing multiple factors.
- Improve Accuracy: Step-by-step problem solving reduces errors in logic, math, and analytical tasks.
- adjust components during testing: Observations
Claude
The thought process helps you to spot areas of the prompt that may not be clear.
- How do you guide thinking?
There are multiple ways to navigate from simple to complexClaude
Thinking. A core skill is:Always let Claude export his thought process., otherwise thinking doesn't happen.- Basic Tips: Add "think step by step" to the prompts.
- Introductory Tip: for
Claude
The thought process outlines specific steps. - Structured tips: Use
<thinking>
cap (a poem)<answer>
and other XML tags separate the reasoning process from the final answer.
Example: Financial Analysis
Analysis of unused chains of thought::
用户
: A client wants to invest $10,000, with option A being a 12% annualized but volatile stock, and option B being a 6% annualized fixed income bond. The client will need this money for a down payment in 5 years. Which one is recommended?模型
: Recommendation B. Reasons are high certainty, risk aversion, and short time horizon. Conclusion seems reasonable but lacks depth.
Analysis using the chain of thought::
用户
: (ibid.), with the addition of the words "Please think step by step" at the end.模型
::<thinking> 1. 理解客户目标和时间线:目标是房屋首付,时间线5年。结论:这是一个短期的、高优先级的财务目标,客户可能无法承受重大风险。 2. 分析选项A(股票): - 优点:历史回报率高(12%)。 - 缺点:波动性大。 - 计算潜在的5年回报: - 最佳情况(年化12%):$10,000 * (1.12)^5 ≈ $17,623.42 - 最差情况(市场崩盘):可能损失惨重,例如50%的崩盘 = $5,000 3. 分析选项B(债券): - 优点:保证回报率(6%)。 - 缺点:回报率低于股票的历史表现。 - 计算5年回报: - 保证:$10,000 * (1.06)^5 ≈ $13,382.25 4. 考虑风险承受能力:目标是房屋首付(必需品,非可有可无),时间短,无法承受市场低迷。结论:客户风险承受能力可能较低。 ... </thinking> <answer> 我推荐选项B,即保证年回报率为6%的债券。虽然股票的历史回报率很诱人,但债券能确保您在5年后拥有$13,382.25用于房屋首付。考虑到您目标的重要性和短暂的时间线,股票的波动性构成了不可接受的风险。 </answer>
This analysis provides quantitative data and more rigorous logic, and the conclusions are more compelling.
Prefilling Claude's response (Prefilling)
Pre-population is a unique and powerful technique that allows you to add an API call to the assistant
The initial text is provided in the message.Claude
will continue to be generated from the end of that text. This lets you enforce a specific format (such as JSON), skip lengthy openings, or reinforce character consistency in roleplaying.
take note of: Prefilled content cannot end with trailing spaces. This feature is currently not supported in Extended Thinking mode.
- How to pre-fill::
import anthropic client = anthropic.Anthropic() response = client.messages.create( model="claude-3-opus-20240229", max_tokens=1024, messages=[ {"role": "user", "content": "你最喜欢的颜色是什么?"}, {"role": "assistant", "content": "作为一个AI助手,我没有最喜欢的颜色,但如果我必须选一个,那会是绿色,因为"} # 在此预填充 ] )
Example: controlling the output format and skipping the preamble
Prefilling not used: When asked to extract JSON, the model may say "Here is the information you want in JSON format:" before giving the code block.
Using pre-fill::
用户
: Extract name, size, price and color as JSON objects from product descriptions...助手 (预填充)
::{
Claude 的回应
::"name": "SmartHome Mini", "size": "5 inches wide", "price": "$49.99", "colors": ["black", "white"] }
This will force
Claude
Direct output of JSON content is clean and easy for the program to parse.
Scenario-specific guidance
- Control Response Format::
- Positive directives are superior to negative directives: Instead of saying "don't use markdown in your response", say "your response should consist of flowing prose paragraphs".
- Using XML Formatting Indicators: Try: "Write the prose portion of your response on the
<smoothly_flowing_prose_paragraphs>
Tagged in." - Matching cue style to desired output: The formatting style used in your prompts may affect the
Claude
The response style.
- Optimizing Parallel Tool Calls::
Claude 4
Models excel at executing tools in parallel. While they have a high success rate for parallel tool invocations without hints, a few minor hints can boost the success rate to near 1001 TP3T.This hint proved most effective: "For maximum efficiency, whenever multiple independent operations need to be performed, all relevant tools should be invoked simultaneously, rather than sequentially. " - Reduced creation of files in code generation::
Claude 4
Models sometimes create new files for testing and iteration. If you wish to minimize the creation of new files, you can instruct theClaude
Clean up on your own: "If you created any temporary new files, scripts, or helper files for the iteration, clean them up by deleting them at the end of the task." - Enhanced visualization and front-end code generation: For front-end code generation, this can be guided by explicit encouragement of the
Claude 4
Models create complex, detailed, and interactive designs: "Don't be constrained, go all out."
Automatically generate and manage alerts
Using the Tip Generator
Sometimes the hardest part of using an AI model is figuring out how to cue it effectively.Anthropic
A prompt generation tool is provided to guide the Claude
Generate high-quality reminder templates for your specific tasks. These templates follow many prompt engineering best practices and are especially useful for solving the "blank page problem". You can find these templates in the Anthropic Console
Try the hint generator directly in the
Using prompt templates and variables
When deploying LLM-based applications, API calls typically contain two types of content:
- Fixed content: A static instruction or context that remains unchanged across multiple interactions.
- variable content: dynamic elements that change with each request or conversation, such as user input, content retrieved by the RAG, conversation history, etc.
Tip templates Combine these fixed and variable sections to use placeholders for dynamic content. In the Anthropic Console
in which these placeholders are represented by {{双花括号}}
Indicates.
Benefits of using a prompt template:
- consistency: Ensure that prompts are structured consistently across multiple interactions.
- efficiency: Easily replace variable content without rewriting the entire prompt.
- testability: Quickly test different input and edge cases by changing only the variable part.
- scalability: Simplify tip management as application complexity grows.
- version control: Easily track changes to the prompt structure by separating the core part of the prompt from the dynamic input.
Notes on Migrating from Sonnet 3.7 to Claude 4
- Specify desired behaviors: Consider describing exactly what you want to see in the output.
- Build your instructions with modifiers: Add encouragement
Claude
Modifiers that improve the quality and detail of the output help to better shape theClaude
The performance. - Explicit request for specific functions: If animation and interactive elements are required, they should be explicitly requested.
Next Steps and Learning Resources
For developers looking to dive deeper with examples, explore the following resources:
- GitHub Tips Engineering Tutorial: A tutorial with a wealth of examples covering the engineering concepts of the tips in the documentation.
- Google Sheets Tips Engineering Tutorial: A lightweight version of the Tips Project tutorial presented through an interactive spreadsheet.
By systematically learning and applying the techniques in this guide, you will be able to navigate more precisely and efficiently the Claude
s powerful ability to build smarter and more reliable applications.