Back to Prompts
🟠AnthropicBeginner

Anthropic Claude Prompt Engineering Guide

Official best practices for prompting Claude models effectively, including Claude 4 Sonnet and Haiku

View Official Documentation

TL;DR

  • Be clear and direct - treat Claude like a brilliant new employee
  • Use XML tags to structure your prompts
  • Provide context and motivation for instructions
  • Give Claude time to think step by step
  • Use examples for complex formatting requirements
  • Prefill Claude's response to guide output format

Key Principles

1State your goal clearly and directly at the start
2Use XML tags (<context>, <instructions>, <example>) for structure
3Assign a role or persona when domain expertise helps
4Include "Think step by step" for complex reasoning
5Break complex tasks into smaller prompt chains
6Avoid over-engineering - keep solutions simple

Be Clear and Direct

Claude responds well to clear, explicit instructions. Think of Claude as a brilliant new employee who needs specific guidance. The more precise your instructions, the better the results.

Examples

Give specific parameters
Bad:
Tell me about climate change
Good:
Explain climate change to a high school student. Include:
1. What causes it
2. Three main effects
3. Two things individuals can do

Keep it under 200 words.

Specific parameters (audience, structure, length) ensure Claude delivers exactly what you need.

Use XML Tags for Structure

Claude was trained with XML tags in its training data. Using tags like <context>, <instructions>, and <example> helps Claude understand the structure of your prompt and parse different components.

Examples

Structure complex prompts with XML
Good:
<context>
You are reviewing a customer support conversation.
</context>

<transcript>
{{TRANSCRIPT}}
</transcript>

<instructions>
Summarize the main issue and how it was resolved in 2-3 sentences.
</instructions>

XML tags make the prompt structure explicit and help Claude understand where each piece of information belongs.

Prefill Claude's Response

You can guide Claude's response format by starting its response with specific text. This is done by adding text to the Assistant turn that Claude will continue from.

Examples

Force a specific output format
Good:
Extract all names from this text: "Meeting attendees: John Smith, Jane Doe, and Bob Wilson."

Assistant: Here are the names:
1.

By starting with "1." you ensure Claude continues with a numbered list format.

Force JSON output
Good:
Extract the person's name and age from: "My name is Sarah and I'm 28."

Assistant: {

Starting with "{" ensures Claude outputs valid JSON.

Give Claude Time to Think

For complex reasoning tasks, asking Claude to think through the problem step by step before giving a final answer significantly improves accuracy. The simplest approach is to include "Think step by step" in your prompt.

Examples

Chain of thought for math problems
Good:
Think through this problem step by step, showing your work, then give the final answer.

Problem: If a train travels 120 miles in 2 hours, stops for 30 minutes, then travels 60 miles in 1 hour, what is the average speed for the whole journey?

Asking for step-by-step thinking helps Claude avoid calculation errors and shows its reasoning.

Use Examples (Few-Shot Prompting)

For complex formatting or specific output styles, providing examples of desired input-output pairs helps Claude understand exactly what you want.

Examples

Show the format with examples
Good:
<instructions>
Convert the description into a product listing.
</instructions>

<example>
Input: Soft cotton t-shirt, blue, sizes S-XL, $25
Output:
- Product: Cotton T-Shirt
- Color: Blue
- Sizes: S, M, L, XL
- Price: $25.00
</example>

Now convert: Leather wallet, brown, fits in pocket, $45

The example shows Claude exactly how to format the output.

Prompt Chaining

Break complex tasks into smaller, manageable steps. Use the output from one prompt as input to the next. This improves accuracy and makes debugging easier.

Examples

Multi-step document analysis
Good:
Step 1: Extract all the key claims from the document.
Step 2: For each claim, identify the supporting evidence.
Step 3: Rate each claim's strength (strong, moderate, weak) based on the evidence.

Let's start with Step 1:

Breaking into explicit steps ensures each sub-task is completed thoroughly before moving on.

#prompt-engineering#claude#anthropic#xml-tags#chain-of-thought#prefill#few-shot
Last updated: December 1, 2024