claude-prompting

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Claude Prompt Engineering

Claude提示词工程

Claude is Anthropic's AI assistant designed to be helpful, harmless, and honest. It excels at long-context tasks, follows complex instructions precisely, and works best with well-structured prompts using XML-style tags.
Claude是Anthropic推出的AI助手,旨在提供有用、无害且诚实的服务。它擅长处理长上下文任务,能精准遵循复杂指令,且在使用XML风格标签的结构化提示词时表现最佳。

When to Invoke This Skill

何时调用此技能

Use this skill when:
  • Crafting prompts specifically for Claude/Anthropic models (default: Claude Sonnet 4.5)
  • Working with long documents or large context (up to 1M tokens with Sonnet 4.5 beta)
  • Using structured prompts with XML-style tags
  • Implementing extended thinking for complex reasoning
  • Requiring precise instruction following
  • Building agentic workflows with parallel tool use
在以下场景使用此技能:
  • 专为Claude/Anthropic模型编写提示词(默认模型:Claude Sonnet 4.5)
  • 处理长文档或大上下文内容(Sonnet 4.5 beta版本支持最高100万token)
  • 使用XML风格标签构建结构化提示词
  • 为复杂推理任务启用扩展思考功能
  • 需要模型精准遵循指令
  • 构建支持并行工具调用的Agent工作流

Claude's Identity & Characteristics

Claude的特性与标识

AttributeDescription
PersonalityHelpful, harmless, honest
Constitutional AIBuilt-in safety and ethical guidelines
Context WindowUp to 1M tokens (Sonnet 4.5 beta), 200K standard
StrengthsLong-context analysis, instruction following, document understanding, agentic tasks
Prompt StyleStructured, clear, XML-style formatting
Extended ThinkingOptional reasoning trace feature with tool use
属性描述
个性有用、无害、诚实
** Constitutional AI**内置安全与伦理准则
上下文窗口最高100万token(Sonnet 4.5 beta版本),标准版为20万token
核心优势长上下文分析、指令遵循、文档理解、Agent类任务处理
提示词风格结构化、清晰、XML风格格式
扩展思考支持结合工具调用的可选推理追踪功能

Universal Prompting Techniques (Claude-Adapted)

适配Claude的通用提示词技巧

1. Zero-Shot Prompting

1. 零样本提示词

Claude responds well to clear, direct zero-shot prompts.
Good Example:
Extract the key dates and events from the following text:

<text>
[paste text]
</text>

Output format: JSON with keys "date", "event", "description"
Less Effective:
Can you tell me what dates are in this text?
Claude对清晰、直接的零样本提示词响应良好。
优秀示例:
从以下文本中提取关键日期与事件:

<text>
[粘贴文本]
</text>

输出格式:包含"date"、"event"、"description"键的JSON
效果较差示例:
你能告诉我这段文本里有哪些日期吗?

2. Few-Shot Prompting (Multishot)

2. 少样本提示词(多轮示例)

Use well-formatted examples with XML structure.
<examples>
<example>
<input>
The conference is scheduled for March 15, 2025 in San Francisco.
</input>
<output>
{
  "date": "2025-03-15",
  "event": "conference",
  "location": "San Francisco"
}
</output>
</example>

<example>
<input>
Our next board meeting is on June 22nd.
</input>
<output>
{
  "date": "2025-06-22",
  "event": "board meeting"
}
</output>
</examples>

<input>
The product launches on September 1st in New York.
</input>

<output>
使用XML结构的格式化示例。
<examples>
<example>
<input>
会议定于2025年3月15日在旧金山举行。
</input>
<output>
{
  "date": "2025-03-15",
  "event": "conference",
  "location": "San Francisco"
}
</output>
</example>

<example>
<input>
我们的下一次董事会会议在6月22日。
</input>
<output>
{
  "date": "2025-06-22",
  "event": "board meeting"
}
</output>
</examples>

<input>
产品将于9月1日在纽约发布。
</input>

<output>

3. Chain-of-Thought Prompting

3. 思维链提示词

Claude has an Extended Thinking feature that shows reasoning (enabled via API, output in response):
I need to decide between these two job offers. Let me think through this step by step.

<job_offer_a>
[details]
</job_offer_a>

<job_offer_b>
[details]
</job_offer_b>

Please analyze both offers, show your reasoning, and provide a recommendation.
API enables extended thinking; response includes:
<thinking>
First, let me analyze the compensation...
Then consider the growth potential...
The work-life balance factors are...
The company stability differs by...
</thinking>

<answer>
[conclusion]
</answer>
Claude具备扩展思考功能,可展示推理过程(通过API启用,在响应中输出):
我需要在这两份工作offer中做选择,请一步步帮我分析。

<job_offer_a>
[详情]
</job_offer_a>

<job_offer_b>
[详情]
</job_offer_b>

请分析这两份offer,展示你的推理过程,并给出推荐建议。
API启用扩展思考后,响应将包含:
<thinking>
首先,我来分析薪酬待遇...
然后考虑发展潜力...
工作与生活平衡的因素包括...
公司稳定性的差异在于...
</thinking>

<answer>
[结论]
</answer>

4. Zero-Shot CoT

4. 零样本思维链

Simply add "Let's think step by step" or similar:
What's the most efficient route to visit all these cities?

Let's think step by step.
只需添加“让我们一步步思考”或类似表述:
游览所有这些城市的最高效路线是什么?

让我们一步步思考。

5. Prompt Chaining with XML Tags

5. 结合XML标签的提示词链

Break complex tasks using XML delimiters:
Chain Step 1:
<task>
Extract relevant quotes from this document related to [topic].
</task>

<document>
[paste document]
</document>

<output_format>
<quotes>
<quote>[relevant quote 1]</quote>
<quote>[relevant quote 2]</quote>
</quotes>
</output_format>
Chain Step 2:
<task>
Summarize the extracted quotes and synthesize key insights.
</task>

<quotes>
[from previous response]
</quotes>

<output_format>
<summary>
[executive summary]
</summary>

<key_insights>
<insight>[insight 1]</insight>
<insight>[insight 2]</insight>
</key_insights>
</output_format>
使用XML分隔符拆分复杂任务:
链步骤1:
<task>
从本文档中提取与[主题]相关的重要引述。
</task>

<document>
[粘贴文档]
</document>

<output_format>
<quotes>
<quote>[相关引述1]</quote>
<quote>[相关引述2]</quote>
</quotes>
</output_format>
链步骤2:
<task>
总结提取的引述并提炼核心见解。
</task>

<quotes>
[来自上一步的响应]
</quotes>

<output_format>
<summary>
[执行摘要]
</summary>

<key_insights>
<insight>[见解1]</insight>
<insight>[见解2]</insight>
</key_insights>
</output_format>

6. ReAct Prompting

6. ReAct提示词

Use structured thought-action-observation cycles:
<question>
[research question]
</question>

<thought_1>
[what needs to be done first]
</thought_1>

<action_1>
[tool use or information gathering]
</action_1>

<observation_1>
[result from action]
</observation_1>

<thought_2>
[next step based on observation]
</thought_2>

<final_answer>
[conclusion]
</final_answer>
使用结构化的思考-行动-观察循环:
<question>
[研究问题]
</question>

<thought_1>
[首先需要完成的事项]
</thought_1>

<action_1>
[工具调用或信息收集]
</action_1>

<observation_1>
[行动结果]
</observation_1>

<thought_2>
[基于观察的下一步计划]
</thought_2>

<final_answer>
[结论]
</final_answer>

7. Tree of Thoughts

7. 思维树

Use multiple reasoning paths with XML structure:
<problem>
[complex problem]
</problem>

<thought_paths>
<path_1>
<assumption>[approach 1]</assumption>
<reasoning>[step-by-step]</reasoning>
<conclusion>[result]</conclusion>
</path_1>

<path_2>
<assumption>[approach 2]</assumption>
<reasoning>[step-by-step]</reasoning>
<conclusion>[result]</conclusion>
</path_2>

<path_3>
<assumption>[approach 3]</assumption>
<reasoning>[step-by-step]</reasoning>
<conclusion>[result]</conclusion>
</path_3>
</thought_paths>

<synthesis>
[best path and why]
</synthesis>
使用XML结构构建多条推理路径:
<problem>
[复杂问题]
</problem>

<thought_paths>
<path_1>
<assumption>[方法1]</assumption>
<reasoning>[步骤分解]</reasoning>
<conclusion>[结果]</conclusion>
</path_1>

<path_2>
<assumption>[方法2]</assumption>
<reasoning>[步骤分解]</reasoning>
<conclusion>[结果]</conclusion>
</path_2>

<path_3>
<assumption>[方法3]</assumption>
<reasoning>[步骤分解]</reasoning>
<conclusion>[结果]</conclusion>
</path_3>
</thought_paths>

<synthesis>
[最优路径及原因]
</synthesis>

Claude-Specific Best Practices

Claude专属最佳实践

1. Use XML-Style Tags for Structure

1. 使用XML风格标签构建结构

Claude's official courses extensively use XML tags:
xml
<context>
[background information]
</context>

<task>
[what needs to be done]
</task>

<examples>
[example inputs and outputs]
</examples>

<input>
[the actual input to process]
</input>

<output_format>
[expected format]
</output_format>
Claude的官方教程广泛使用XML标签:
xml
<context>
[背景信息]
</context>

<task>
[需要完成的任务]
</task>

<examples>
[示例输入与输出]
</examples>

<input>
[待处理的实际输入]
</input>

<output_format>
[预期格式]
</output_format>

2. Structure Long Prompts Hierarchically

2. 分层构建长提示词

From Anthropic's official courses:
[TASK_CONTEXT]
Setting the stage and overall context

[TONE_CONTEXT]
How Claude should approach the task

[INPUT_DATA]
The actual data to work with

[EXAMPLES]
Few-shot examples

[TASK_DESCRIPTION]
Specific task details

[IMMEDIATE_TASK]
The immediate action to take

[OUTPUT_FORMATTING]
Expected output structure
来自Anthropic官方教程:
[TASK_CONTEXT]
设定任务背景与整体上下文

[TONE_CONTEXT]
Claude处理任务的语气要求

[INPUT_DATA]
待处理的实际数据

[EXAMPLES]
少样本示例

[TASK_DESCRIPTION]
具体任务细节

[IMMEDIATE_TASK]
需立即执行的动作

[OUTPUT_FORMATTING]
预期输出结构

3. Leverage Extended Thinking

3. 利用扩展思考功能

For complex reasoning, enable Claude's extended thinking via the API:
API Syntax (Python SDK):
python
response = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=4096,
    thinking={
        "type": "enabled",
        "budget_tokens": 8192
    },
    messages=[{
        "role": "user",
        "content": "Analyze this complex problem..."
    }]
)
Prompt-Side (XML structure for expected output):
<task>
[complex reasoning task]
</task>

<thinking>
[Claude will show its reasoning here]
</thinking>

<answer>
[final answer]
</answer>
Key Points:
  • budget_tokens
    sets max tokens for reasoning (must be <
    max_tokens
    )
  • Claude 4.5 returns summarized thinking by default
  • First few lines are more verbose (useful for prompt engineering)
  • You're billed for full thinking tokens, not summary tokens
对于复杂推理任务,通过API启用Claude的扩展思考功能:
API语法(Python SDK):
python
response = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=4096,
    thinking={
        "type": "enabled",
        "budget_tokens": 8192
    },
    messages=[{
        "role": "user",
        "content": "分析这个复杂问题..."
    }]
)
提示词侧(用于指定输出格式的XML结构):
<task>
[复杂推理任务]
</task>

<thinking>
[Claude将在此展示推理过程]
</thinking>

<answer>
[最终答案]
</answer>
核心要点:
  • budget_tokens
    设置推理过程的最大token数(必须小于
    max_tokens
  • Claude 4.5默认返回精简后的推理内容
  • 开头几行内容更详细(对提示词工程很有帮助)
  • 计费按照完整推理token数计算,而非精简后的token数

4. Use System Prompts Effectively

4. 有效使用系统提示词

System prompts set Claude's behavior:
System: You are a technical writer specializing in API documentation. Your responses are always:
- Clear and concise
- Technically accurate
- Formatted with Markdown
- Focused on developer needs

User: [your actual query]
系统提示词可设定Claude的行为模式:
System: 你是一名专注于API文档的技术作家。你的响应需始终满足:
- 清晰简洁
- 技术准确
- 使用Markdown格式
- 聚焦开发者需求

User: [你的实际查询]

5. Prefill Claude's Response

5. 预填充Claude的响应内容

Guide the format by starting Claude's response:
<task>
Analyze this document and extract key findings.
</task>

<document>
[paste document]
</document>

<response>
<summary>
[Claude continues from here]
通过预写响应开头来引导输出格式:
<task>
分析本文档并提取关键发现。
</task>

<document>
[粘贴文档]
</document>

<response>
<summary>
[Claude将在此处继续输出]

6. Cache Control for Long Prompts

6. 长提示词的缓存控制

Optimize for repeated prompts:
<cached_content cache_control="{\"type\":\"ephemeral\"}">
[large context that doesn't change]
</cached_content>

<task>
[specific task that varies]
</task>
针对重复使用的提示词进行优化:
<cached_content cache_control="{\"type\":\"ephemeral\"}">
[不发生变化的大上下文内容]
</cached_content>

<task>
[会变化的具体任务]
</task>

7. Claude 4.5 Agent Features

7. Claude 4.5的Agent功能

Claude 4.5 introduces powerful agent capabilities:
Parallel Tool Use - Claude can use multiple tools simultaneously:
xml
<task>
Analyze this data and create a visualization.
</task>

<tools>
- Web search for market data
- Code execution for analysis
- File write for chart output
</tools>

Claude will execute these in parallel when possible.
Memory Files - Claude can maintain knowledge across sessions:
xml
<task>
When working on ongoing projects, create a memory file to track:
- Key decisions and rationale
- Project context and constraints
- Preferences and patterns
</task>

Claude will automatically update and reference memory files when given local file access.
Extended Thinking with Tools - Reasoning can pause to use tools:
python
undefined
Claude 4.5引入了强大的Agent能力:
并行工具调用 - Claude可同时使用多个工具:
xml
<task>
分析这些数据并创建可视化图表。
</task>

<tools>
- 网络搜索获取市场数据
- 代码执行进行分析
- 文件写入生成图表输出
</tools>

Claude会在可能的情况下并行执行这些操作。
记忆文件 - Claude可在多个会话间保留知识:
xml
<task>
处理持续进行的项目时,创建记忆文件以追踪:
- 关键决策及理由
- 项目背景与约束条件
- 偏好与模式
</task>

当获得本地文件访问权限时,Claude会自动更新并引用记忆文件。
结合工具的扩展思考 - 推理过程可暂停以调用工具:
python
undefined

API: Enable extended thinking with tool use

API:启用结合工具的扩展思考

response = client.messages.create( model="claude-sonnet-4-5-20250929", max_tokens=4096, thinking={ "type": "enabled", "budget_tokens": 8192 }, tools=[web_search_tool], messages=[{ "role": "user", "content": "Research [topic] and provide a comprehensive analysis." }] )
response = client.messages.create( model="claude-sonnet-4-5-20250929", max_tokens=4096, thinking={ "type": "enabled", "budget_tokens": 8192 }, tools=[web_search_tool], messages=[{ "role": "user", "content": "研究[主题]并提供全面分析。" }] )

Claude can now use web search DURING extended thinking,

Claude现在可在扩展思考过程中调用网络搜索,

alternating between reasoning and information gathering.

在推理与信息收集之间交替进行。

undefined
undefined

Anti-Patterns to Avoid

需避免的反模式

Anti-PatternWhy It FailsBetter Approach
Ambiguous instructionsClaude follows literallyBe explicit about requirements
Missing output formatUnpredictable formattingAlways specify format
No structure in long promptsClaude may lose trackUse XML tags and sections
Ignoring context window limitsTruncation issuesBe mindful of 200K/1M limits
Over-constraining creativityReduces Claude's helpfulnessBalance structure with flexibility
反模式失败原因优化方案
模糊的指令Claude会严格按字面意思执行明确说明需求
未指定输出格式输出格式不可预测始终指定输出格式
长提示词无结构Claude可能遗漏关键信息使用XML标签与分段结构
忽略上下文窗口限制出现内容截断问题注意20万/100万token的限制
过度限制创造力降低Claude的实用性在结构与灵活性间取得平衡

Quick Reference Templates

快速参考模板

Document Analysis

文档分析

xml
<task>
[specific analysis task]
</task>

<document>
[paste document]
</document>

<output_format>
[expected structure]
</output_format>
xml
<task>
[具体分析任务]
</task>

<document>
[粘贴文档]
</document>

<output_format>
[预期结构]
</output_format>

Code Generation with Examples

带示例的代码生成

xml
<task>
Write a function that [description]
</task>

<requirements>
[specific requirements]
</requirements>

<examples>
<example>
<input>[input example]</input>
<output>[expected output]</output>
</example>
</examples>

<output_format>
[code in specified language]
</output_format>
xml
<task>
编写一个实现[功能描述]的函数
</task>

<requirements>
[具体要求]
</requirements>

<examples>
<example>
<input>[输入示例]</input>
<output>[预期输出]</output>
</example>
</examples>

<output_format>
[指定语言的代码]
</output_format>

Data Extraction

数据提取

xml
<task>
Extract [specific fields] from the following text
</task>

<input_text>
[paste text]
</input_text>

<output_format>
JSON with keys: [list keys]
</output_format>
xml
<task>
从以下文本中提取[具体字段]
</task>

<input_text>
[粘贴文本]
</input_text>

<output_format>
包含以下键的JSON:[键列表]
</output_format>

Extended Thinking

扩展思考

python
undefined
python
undefined

API: Enable extended thinking

API:启用扩展思考

response = client.messages.create( model="claude-sonnet-4-5-20250929", max_tokens=4096, thinking={ "type": "enabled", "budget_tokens": 8192 }, messages=[{ "role": "user", "content": """ <task> [complex reasoning task] </task>
Please show your reasoning step by step, then provide the final answer. """ }] )
response = client.messages.create( model="claude-sonnet-4-5-20250929", max_tokens=4096, thinking={ "type": "enabled", "budget_tokens": 8192 }, messages=[{ "role": "user", "content": """ <task> [复杂推理任务] </task>
请逐步展示你的推理过程,然后给出最终答案。 """ }] )

Response will include <thinking> block followed by <answer>

响应将包含<thinking>块及后续的<answer>

undefined
undefined

Model Capabilities Reference

模型能力参考

FeatureClaude Sonnet 4.5Claude Haiku 4.5Claude Opus 4.5Notes
Context Window200K / 1M (beta)200K200KSonnet 4.5: 1M with beta header
Extended Thinking✅ Yes✅ Yes✅ YesWith tool use support
Max Output64K tokens64K tokens64K tokensUnified across 4.5
Vision✅ Yes✅ Yes✅ YesImage analysis
Parallel Tool Use✅ Yes✅ Yes✅ YesClaude 4.5 feature
Memory Files✅ Yes✅ Yes✅ BestLocal file knowledge
Code✅ Excellent✅ Good✅ BestOpus 4.5: SOTA coding
SpeedFastFastestModerateDefault: Sonnet 4.5
Recommendation: Start with Claude Sonnet 4.5 - best balance of intelligence, speed, and cost for most use cases. Use Opus 4.5 for complex coding, Haiku 4.5 for speed-critical tasks.
特性Claude Sonnet 4.5Claude Haiku 4.5Claude Opus 4.5说明
上下文窗口20万 / 100万(beta)20万20万Sonnet 4.5:添加beta请求头可启用100万token
扩展思考✅ 支持✅ 支持✅ 支持支持结合工具调用
最大输出64000 token64000 token64000 token4.5系列统一支持
视觉能力✅ 支持✅ 支持✅ 支持图片分析
并行工具调用✅ 支持✅ 支持✅ 支持Claude 4.5新增特性
记忆文件✅ 支持✅ 支持✅ 最优本地文件知识存储
代码能力✅ 优秀✅ 良好✅ 最优Opus 4.5:达到SOTA编码水平
速度最快中等默认推荐:Sonnet 4.5
推荐:从Claude Sonnet 4.5开始使用——它在智能性、速度与成本间达到最佳平衡,适用于大多数场景。复杂编码任务使用Opus 4.5,对速度要求极高的任务使用Haiku 4.5。

Migration Notes (Claude 3 → 4.5)

迁移说明(Claude 3 → 4.5)

If you're migrating from Claude 3.x to Claude 4.5:
ChangeImpactAction
Default modelSonnet 3.5 → Sonnet 4.5Update model IDs in code
Context window200K → 1M (beta) availableRequires beta header for 1M
Parallel toolsNew capabilityUpdate prompts to leverage parallel execution
Memory filesNew capabilityGrant file access for persistent knowledge
Extended thinking + toolsNew capabilityCan now use tools during reasoning
Max output8K → 64K tokensAdjust output expectations
API Migration:
python
undefined
如果你正从Claude 3.x迁移到Claude 4.5:
变更影响操作建议
默认模型Sonnet 3.5 → Sonnet 4.5更新代码中的模型ID
上下文窗口20万 → 100万(beta)可用需要添加beta请求头以启用100万token
并行工具调用新增能力更新提示词以利用并行执行特性
记忆文件新增能力授予文件访问权限以实现持久化知识存储
扩展思考+工具新增能力现在可在推理过程中调用工具
最大输出8000 → 64000 token调整对输出长度的预期
API迁移示例:
python
undefined

Old (Claude 3.5)

旧版本(Claude 3.5)

model="claude-sonnet-3-5-20240620"
model="claude-sonnet-3-5-20240620"

New (Claude 4.5)

新版本(Claude 4.5)

model="claude-sonnet-4-5-20250929" # or use alias "claude-sonnet-4-5"

Most prompting techniques remain unchanged—XML tags, system prompts, and structured outputs work identically.
model="claude-sonnet-4-5-20250929" # 或使用别名"claude-sonnet-4-5"

大多数提示词技巧保持不变——XML标签、系统提示词及结构化输出的用法完全一致。

Prompt Element Checklist

提示词元素检查清单

When creating Claude prompts, consider including:
  • Task Context: Overall purpose and setting
  • Tone Context: How Claude should approach it
  • Input Data: The actual content to process
  • Examples: Few-shot demonstrations (if needed)
  • Task Description: Specific instructions
  • Immediate Task: What to do right now
  • Output Format: Expected structure
  • Prefill: Start of Claude's response (optional)
创建Claude提示词时,建议包含以下元素:
  • 任务上下文:整体目标与背景
  • 语气要求:Claude处理任务的语气风格
  • 输入数据:待处理的实际内容
  • 示例:少样本演示(如有需要)
  • 任务描述:具体指令
  • 即时任务:当前需执行的动作
  • 输出格式:预期的输出结构
  • 预填充:Claude响应的开头内容(可选)

See Also

另请参阅

  • references/basics.md
    - Foundational Claude prompting concepts
  • references/techniques.md
    - Detailed technique explanations
  • references/xml-formatting.md
    - XML tag patterns and usage
  • references/patterns.md
    - Reusable Claude prompt patterns
  • references/examples.md
    - Concrete examples from Anthropic courses
  • grok-prompting
    skill - For Grok/xAI-specific guidance
  • gemini-prompting
    skill - For Google Gemini-specific guidance
  • references/basics.md
    - Claude提示词基础概念
  • references/techniques.md
    - 详细技巧说明
  • references/xml-formatting.md
    - XML标签模式与用法
  • references/patterns.md
    - 可复用的Claude提示词模板
  • references/examples.md
    - 来自Anthropic教程的具体示例
  • grok-prompting
    技能 - 针对Grok/xAI模型的专属指南
  • gemini-prompting
    技能 - 针对Google Gemini模型的专属指南