Mastering Claude Prompt Engineering: 9 Proven Strategies to Boost LLM Performance
This guide systematically breaks down Anthropic's official prompt‑engineering recommendations—clear instructions, multishot examples, chain‑of‑thought prompting, XML structuring, response pre‑filling, prompt chaining, long‑context handling, extended thinking, and practical code snippets—showing how to unlock Claude's full potential across complex tasks.
Anthropic Prompt Optimization Strategies
Anthropic officially recommends seven key strategies to get the most out of Claude and other advanced large language models:
Be clear and direct
Use examples (multishot)
Let Claude think (chain of thought)
Use XML tags
Prefill Claude’s response
Chain complex prompts
Long‑context tips
Key Takeaways from Personal Experience
Structure prompts with XML tags to clearly separate context, instructions, and examples.
Trigger Claude’s thinking and planning by asking it to reason step‑by‑step before answering.
Provide contextual information such as task purpose, audience, workflow position, and success criteria.
Specify exactly what Claude should do , e.g., output only code or JSON.
Order instructions using numbered or bulleted lists to ensure correct execution.
XML‑Based Prompt Structure
When prompts contain multiple types of information (context, examples, instructions), wrap each part in custom XML tags. Example:
<example>…</example>
<examples>…</examples>Maintain consistency and nesting, e.g., <outer><inner>…</inner></outer>.
Multishot Prompting
Multishot prompting is a concrete implementation of few‑shot learning for LLMs; the two are not opposites.
Provide well‑designed examples to improve accuracy, consistency, and quality, especially for structured output.
<example>…</example>
<example>…</example>Chain‑of‑Thought (CoT)
For complex tasks, ask Claude to decompose the problem step‑by‑step, which yields more precise and detailed responses.
Prefilling Claude’s Response
Start the assistant message with a partial answer so Claude continues from there, allowing you to skip long introductions, enforce specific output formats (e.g., JSON), or maintain role consistency.
{
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "请帮我写一封感谢信…"},
{"role": "assistant", "content": "Dear [Name],
我希望这封邮件能找到你。"}
]
}Prompt Chaining
Break a complex task into smaller sub‑tasks, feed the output of one step as the input to the next, and iterate for optimization.
1. Analyze the existing code and identify refactoring points.
2. Design a new modular structure with function signatures.
3. Implement each function step‑by‑step.Example XML‑structured output for a refactoring analysis:
<refactoring_points>
<point>
<description>函数职责单一原则违反</description>
<reason>函数承担过多职责,难以测试</reason>
<improvement>拆分为 validate、communicate、record 等独立函数</improvement>
</point>
</refactoring_points>Extended Thinking
Enable Claude’s extended thinking mode via the API to obtain a transparent reasoning trace before the final answer.
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=16000,
thinking={"type": "enabled", "budget_tokens": 10000},
messages=[{"role": "user", "content": "Are there an infinite number of prime numbers such that n mod 4 == 3?"}]
)The response includes a thinking block followed by the final text block.
High‑Level Instructions
Claude often performs better when given a concise high‑level goal rather than overly prescriptive step‑by‑step directions, allowing it to apply its own creativity.
Practical Coding Example
Ask Claude to write a Python function with test cases, then have it run the tests in its extended thinking phase.
编写一个计算数字阶乘的函数。
在完成之前,请使用以下测试用例验证你的解决方案:
- n=0
- n=1
- n=5
- n=10
并修复你发现的任何问题。Reference Links
Prompt Engineering Overview
Be Clear and Direct
Multishot Prompting
Chain of Thought
Use XML Tags
Prefill Claude’s Response
Chain Prompts
Long Context Tips
Extended Thinking
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
