aiwithgrant
about me
aiwithgrant›Guides›Anthropic
Anthropic
Anthropic
Official Docs
Beginner

The Complete Prompt Engineering Guide

Everything from Anthropic's prompt engineering docs in one progressive guide. Clear instructions, multishot examples, chain of thought, XML tags, roles, chaining, long context, extended thinking, and Claude 4 best practices.

Official Anthropic docs →
Content sourced from official Anthropic documentation
1

Before you start

Before diving into techniques, you need three things: a clear definition of what success looks like for your use case, a way to test against those criteria, and a first draft prompt to improve. Without these, you're optimizing blind. Prompt engineering is far faster and cheaper than fine-tuning. It preserves the model's general knowledge, keeps everything human-readable, and works across model updates without retraining.

šŸ’”Think of prompt engineering like giving instructions to a brilliant new hire who has zero context on your project. The clearer you are, the better they perform.
2

Be clear and direct

This is the single most impactful technique. Show your prompt to a colleague with no context and ask them to follow the instructions. If they're confused, Claude will be too. Be specific about format, length, style, and structure. Provide context about what the output is for, who the audience is, and what success looks like. Use numbered steps for multi-part tasks so nothing gets skipped.

šŸ’”Instead of 'Write a marketing email', say exactly what audience, tone, features to highlight, CTA, and structure you want. Eliminate every point of ambiguity.
Vague vs. specific prompt
Your task is to craft a targeted marketing email for our Q3 AcmeCloud feature release.

Instructions:
1. Write for this target audience: Mid-size tech companies (100-500 employees) upgrading from on-prem to cloud.
2. Highlight 3 key new features: advanced data encryption, cross-platform sync, and real-time collaboration.
3. Tone: Professional yet approachable. Emphasize security, efficiency, and teamwork.
4. Include a clear CTA: Free 30-day trial with priority onboarding.
5. Subject line: Under 50 chars, mention "security" and "collaboration".
6. Personalization: Use {{COMPANY_NAME}} and {{CONTACT_NAME}} variables.

Structure:
1. Subject line
2. Email body (150-200 words)
3. CTA button text
The vague prompt makes Claude guess everything. The specific prompt eliminates all guesswork and gets exactly what you need first try.
3

Use examples (multishot prompting)

Examples are your secret weapon. By providing 2-5 well-crafted input/output examples, you dramatically improve accuracy and consistency, especially for structured outputs. Wrap examples in <example> tags. Make them relevant to your actual use case, diverse enough to cover edge cases, and clear in the format you expect. Without examples, Claude often adds unwanted explanations and uses inconsistent formatting.

šŸ’”Don't have good examples? Ask Claude to generate some based on your description, then edit them to match your ideal output.
Feedback categorization
Your task is to analyze feedback and categorize issues. Use these categories: UI/UX, Performance, Feature Request, Integration, Pricing, and Other. Rate sentiment (Positive/Neutral/Negative) and priority (High/Medium/Low).

<example>
Input: The new dashboard is a mess! It takes forever to load, and I can't find the export button. Fix this ASAP!
Category: UI/UX, Performance
Sentiment: Negative
Priority: High
</example>

Now analyze this feedback: {{FEEDBACK}}
Without the example, Claude outputs single categories with long paragraphs. With the example, it outputs clean multi-category labels in the exact format shown.
4

Let Claude think (chain of thought)

For complex tasks, giving Claude space to reason step-by-step dramatically improves accuracy. There are three levels: Basic (just add 'Think step-by-step'), Guided (outline specific reasoning steps), and Structured (use XML tags like <thinking> and <answer> to separate reasoning from output). Structured is the best because you can programmatically extract just the answer while keeping the full reasoning for debugging.

šŸ’”Use CoT for tasks a human would need to think through: complex math, multi-step analysis, decisions with many factors. Skip it for simple lookups or straightforward formatting.
Investment analysis with CoT
A client wants to invest $10,000. Option A: stock returning 12% annually but volatile. Option B: bond guaranteeing 6%. They need the money in 5 years for a house down payment.

Think step-by-step in <thinking> tags, then give your recommendation in <answer> tags.
With CoT, Claude calculates exact figures ($17,623 best case vs $13,382 guaranteed), considers historical volatility, and analyzes risk tolerance, producing a fundamentally better recommendation.
5

Use XML tags to structure prompts

When prompts have multiple components (context, instructions, examples, data), XML tags prevent Claude from mixing things up. Use <instructions> for task steps, <data> for inputs, <examples> for examples, and <thinking>/<answer> for chain of thought. There are no 'correct' tag names, just use ones that make sense. Claude was specifically trained to recognize XML structure, making it particularly effective.

šŸ’”Combine XML tags with other techniques for maximum impact: wrap multishot examples in <examples>, separate reasoning with <thinking>/<answer>, and tag data inputs with <data>.
Financial report with XML
Generate a Q2 financial report for our investors. AcmeCorp is a B2B SaaS company. Our investors value transparency and actionable insights.

Use this data:
<data>{{SPREADSHEET_DATA}}</data>

<instructions>
1. Include sections: Revenue Growth, Profit Margins, Cash Flow.
2. Highlight strengths and areas for improvement.
</instructions>

Follow this structure:
<formatting_example>{{Q1_REPORT}}</formatting_example>
Tags prevent Claude from confusing the Q1 reference with the actual data. Output goes from verbose paragraphs to clean bullet points with trend indicators.
6

Give Claude a role (system prompts)

The right role turns Claude from a general assistant into a virtual domain expert. Use the system parameter in the API to set roles. A 'General Counsel' catches contract risks worth millions that a generic prompt misses. A 'CFO' delivers board-ready analysis instead of generic summaries. The role should match the expertise level your task requires.

šŸ’”Experiment with different roles on the same data. A 'data scientist' finds different insights than a 'marketing strategist' analyzing the same dataset.
Contract review
[System] You are the General Counsel of a Fortune 500 tech company.

[User] We're considering this software licensing agreement for our core data infrastructure:
<contract>{{CONTRACT}}</contract>

Analyze it for potential risks, focusing on indemnification, liability, and IP ownership. Give your professional opinion.
Without role: 'the agreement seems standard.' With role: catches that indemnification could hold you liable for vendor's negligence, $500 liability cap is 'grossly inadequate,' and joint IP ownership could let vendor sell your proprietary algorithms.
7

Chain complex prompts

When a single prompt handles everything, Claude can drop steps. Break complex tasks into focused subtasks connected in a chain. Each link gets Claude's full attention. Common chains: Research > Outline > Draft > Edit > Format, or Generate > Review > Improve. Self-correction chains (generate, then review, then improve) catch errors that humans would find in editing.

šŸ’”Run independent subtasks in parallel for speed. Only chain sequentially when outputs depend on previous steps.
Self-correction chain
Prompt 1 (Generate):
"Summarize this medical research paper. Focus on methodology, findings, and clinical implications."

Prompt 2 (Review):
"Review this summary for accuracy, clarity, and completeness on an A-F scale."
> Catches: undefined acronyms, missing baseline stats, no secondary endpoints

Prompt 3 (Improve):
"Update the summary based on the feedback."
> Final version: defines all acronyms, adds group matching, notes secondary endpoints
The review step consistently identifies issues like missing definitions, incomplete data, and gaps in analysis. Each step has one focused objective, so nothing gets dropped.
8

Long context tips

Claude handles up to 200K tokens. Place long documents at the top of your prompt, above your query and instructions. Queries at the end can improve response quality by up to 30%. Wrap multiple documents in <document> tags with index attributes so Claude knows which source is which. For large document tasks, ask Claude to quote relevant sections first before answering. This forces it to find evidence before reasoning, dramatically reducing hallucination.

šŸ’”Number your documents with index attributes and ask Claude to cite which document its information comes from.
9

Extended thinking

For the most complex problems, extended thinking lets Claude reason internally before responding. Start with general instructions rather than prescriptive step-by-step guidance. Claude's creativity may exceed your ability to prescribe the optimal thinking process. Include multishot examples with <thinking> tags to show reasoning patterns. Extended thinking excels on heavily constrained problems, STEM tasks, and strategic analysis. Always ask Claude to verify its work before declaring a task complete.

šŸ’”Start with the minimum thinking budget (1024 tokens) and increase only if needed. For workloads above 32K thinking tokens, use batch processing.
10

Claude 4 best practices

Claude's latest models (Opus 4.6, Sonnet 4.5) follow instructions more precisely than ever. Be explicit about what you want, including 'above and beyond' behavior. Provide context for your constraints ('never use ellipses because this will be read by text-to-speech'). Claude excels at parallel tool execution and long-horizon reasoning across extended sessions. For agentic workflows, use the first context window to set up a framework (write tests, create scripts), then iterate in future windows.

šŸ’”If you want Claude to take action rather than just suggest changes, say so explicitly: 'Make these edits' instead of 'Can you suggest some changes?'
11

The prompt improver

Anthropic's built-in prompt improver (in the Workbench) automatically enhances your prompts in 4 steps: identifies examples, adds XML structure, refines chain of thought, and enhances examples with step-by-step reasoning. Submit your prompt, add feedback about current issues, include example inputs and ideal outputs, and review the improved version. Best for complex tasks where accuracy matters more than speed.

šŸ’”Don't have examples? Use the Test Case Generator to generate sample inputs, get Claude's responses, edit them to your ideal, then feed them back into the improver.

Key topics covered

Clear prompting
Multishot examples
Chain of thought
XML tags
System prompts
Prompt chaining
Long context
Extended thinking
Claude 4
Read the full guide
View the complete Anthropic documentation
Official docs →

More guides