Prompts2026年3月29日·1 分钟阅读

Anthropic Prompt Engineering Guide — Official Best Practices

Official prompting guide from Anthropic for Claude. System prompts, chain-of-thought, few-shot, XML tags, tool use, and advanced techniques.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Prompt
安装
Single
信任
信任等级:Community
入口
Anthropic Prompt Engineering Guide
先审查命令
npx -y tokrepo@latest install 8fdb8111-8d8a-4dfd-aea2-797eb1db6cea --target codex

先 dry-run,确认写入项后再运行此命令。

TL;DR
Anthropic's official guide to writing effective prompts for Claude with proven techniques.
§01

What it is

This is Anthropic's official prompt engineering guide for Claude models. It covers system prompts, chain-of-thought reasoning, few-shot examples, XML tag structuring, tool use, and advanced techniques for getting reliable outputs from Claude. The guide represents Anthropic's own recommendations based on internal research and user feedback.

This resource is essential for developers building applications with Claude, prompt engineers optimizing performance, and anyone who wants to get better results from Claude models.

§02

How it saves time or tokens

Good prompts reduce iteration cycles. Instead of trial-and-error, this guide teaches proven patterns that work on the first try. Techniques like XML tag structuring reduce ambiguity, which means fewer retries and lower token costs. Chain-of-thought prompting improves accuracy on complex tasks, reducing the need for human correction. The estimated token cost to study and apply the guide is around 500 tokens per session.

§03

How to use

  1. Read the guide sections relevant to your use case.
  2. Apply the recommended prompt structure to your system prompts.
  3. Use XML tags to organize complex instructions.
  4. Test and iterate with the techniques described.
<!-- Recommended Claude prompt structure -->
<system>
You are a senior code reviewer. Analyze the provided code
for bugs, security issues, and performance problems.

<rules>
- Focus on logic errors, not style
- Rate severity as HIGH, MEDIUM, or LOW
- Provide a fix for each issue found
</rules>

<output_format>
Return a JSON array of issues:
[{"line": N, "severity": "...", "issue": "...", "fix": "..."}]
</output_format>
</system>
§04

Example

Applying chain-of-thought with XML tags:

<instructions>
Analyze this SQL query for performance issues.

Think step by step:
1. Identify the tables and joins involved
2. Check for missing indexes based on WHERE clauses
3. Look for N+1 query patterns
4. Suggest specific optimizations

Put your reasoning in <thinking> tags before the final answer.
</instructions>

<query>
SELECT u.name, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE o.created_at > '2026-01-01'
GROUP BY u.name
ORDER BY order_count DESC
</query>
§05

Related on TokRepo

§06

Common pitfalls

  • Overly long system prompts waste tokens and can dilute important instructions. Keep system prompts focused.
  • Few-shot examples that do not match the target format cause inconsistent outputs. Ensure examples closely mirror expected input.
  • Chain-of-thought prompting increases output tokens. Use it for complex reasoning tasks, not simple lookups.
  • XML tags help Claude parse structure but are not required for simple tasks. Do not over-structure simple prompts.
  • The guide evolves as Claude models improve. Techniques optimal for Claude 3 may need adjustment for newer models.

常见问题

What are XML tags in Claude prompts?+

XML tags like <instructions>, <context>, and <output_format> help Claude parse different sections of a prompt. They reduce ambiguity and improve instruction following, especially in complex prompts with multiple sections.

When should I use chain-of-thought prompting?+

Use chain-of-thought for tasks requiring multi-step reasoning: math problems, code analysis, logical deduction, and complex decision-making. For simple tasks like translation or formatting, direct prompting is more efficient.

How do few-shot examples improve results?+

Few-shot examples show Claude the exact input-output format you expect. They are especially useful for structured outputs, specific formatting requirements, or domain-specific terminology. Two to three examples usually suffice.

Does the guide apply to all Claude models?+

The core techniques apply across Claude models. However, newer models like Claude Sonnet 4 handle ambiguity better than older versions, so some techniques may be more or less critical depending on the model.

How do I structure tool use prompts?+

Define tools with clear descriptions, parameter types, and expected behavior. Claude uses tool descriptions to decide when and how to call tools. Specific, unambiguous tool descriptions lead to more accurate tool selection.

引用来源 (3)
🙏

来源与感谢

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产