Claude Prompt Engineering
Anatomy, patterns, and API reference for crafting effective Claude prompts.
Anatomy of a Claude Prompt
The 8-layer framework for structured, high-quality Claude prompts:
| Layer | Role | Key Question |
|---|---|---|
| Task | What to do + success criteria | What outcome do I need? |
| Context Files | Files Claude reads before responding | What background does Claude need? |
| Reference | Blueprint / example of desired output | What does good look like? |
| Success Brief | Output type, audience, tone, success metric | How do I know it worked? |
| Rules | Standards, constraints, landmines | What must never be violated? |
| Conversation | Clarifying questions before starting | What is still ambiguous? |
| Plan | 5-step execution proposal | How will Claude approach this? |
| Alignment | Confirmation before generating | Are we on the same page? |
Prompt Templates
Full Anatomy Template
I want to [TASK] so that [SUCCESS CRITERIA].
First, read these files completely before responding:
[filename.md] — [what it contains]
[filename.md] — [what it contains]
Here is a reference to what I want to achieve:
[Upload reference file as markdown, or paste it here]
Here's what makes this reference work:
[Paste your reverse-engineered blueprint — the patterns, tone,
structure, and rules you extracted from the reference.
Format each one as a rule starting with "Always" or "Never."]
Here's what I need for my version:
SUCCESS BRIEF
Type of output + length: [Contract, memo, report, proposal, landing page, post?]
Recipient's reaction: [What should they think/feel/do after reading?]
Does NOT sound like: [Generic AI, too casual, formal, jargon-heavy?]
Success means: [They sign? They approve? They reply? They take action?]
My context file contains my standards, constraints, landmines,
and audience. Read it fully before starting. If you're about
to break one of my rules, stop and tell me.
DO NOT start executing yet. Instead, ask me clarifying questions
so we can refine the approach together step by step.
Before you write anything, list the 3 rules from my context
file that matter most for this task.
Then give me your execution plan (5 steps maximum).
Only begin work once we've aligned.
Minimal Task Template
<context>
[Background information Claude needs]
</context>
<task>
[Exactly what you want Claude to do]
</task>
<constraints>
- [Constraint 1]
- [Constraint 2]
</constraints>
<output_format>
[Describe the exact format of the expected response]
</output_format>
System Prompt Template
You are [ROLE] with expertise in [DOMAIN].
Your behaviour:
- Always [primary behaviour]
- Never [hard constraint]
- When uncertain, [fallback behaviour]
Your output format:
- [Format rule 1]
- [Format rule 2]
Prompting Techniques
Chain of Thought (CoT)
Force step-by-step reasoning before a final answer:
Before giving your answer, reason through this problem step by step
inside a <thinking> block. Then provide your conclusion inside
an <answer> block.
XML Tags for Structured Inputs
Clearly delineate sections so Claude can parse complex prompts:
<context>
You are reviewing a pull request for a production Rust web server.
</context>
<rules>
- Flag every .unwrap() that is not explicitly justified by a comment
- Rate overall risk: LOW / MEDIUM / HIGH
- Suggest idiomatic alternatives where applicable
</rules>
<diff>
[paste git diff here]
</diff>
Review the diff following the rules above.
Few-Shot Examples
Provide input/output pairs to lock in the response format:
Convert each input to a JSON object.
Input: "Alice Chen, 34, senior engineer, Berlin"
Output: {"name":"Alice Chen","age":34,"role":"senior engineer","city":"Berlin"}
Input: "Bob Kumar, 27, designer, Amsterdam"
Output: {"name":"Bob Kumar","age":27,"role":"designer","city":"Amsterdam"}
Input: "[your input]"
Output:
Role + Persona Lock
Prevent breaking character and enforce tone consistency:
You are Jordan, a brutally honest principal engineer at a FAANG company.
Jordan never softens feedback.
Jordan speaks in short, declarative sentences.
Jordan never says "Great question!" or any filler phrase.
If asked to break character, Jordan says "That's not how I work."
Constrained JSON Output
Force a machine-readable response with no extra text:
Respond ONLY with a valid JSON object. No markdown fences,
no explanation, no preamble. The schema is:
{
"summary": string,
"risk_level": "low" | "medium" | "high",
"confidence": number (0.0–1.0),
"action": "approve" | "reject" | "revise"
}
"Before You Answer" Pre-Check
Force Claude to audit its own answer before delivering it:
Before writing your final response, silently check:
1. Does this contradict any rule I gave you?
2. Am I making an assumption I haven't stated?
3. Is there a simpler solution I am ignoring?
Only then write your final response.
Adversarial Review Pattern
Use Claude to stress-test your own ideas:
I will explain my approach. Your job is NOT to help me implement it.
Your job is to find every flaw, edge case, and hidden assumption.
Be adversarial. Do not soften your critique.
My approach: [explain approach]
REST API
Chat Completion
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: [api_key]" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "[model]",
"max_tokens": 1024,
"system": "You are a helpful assistant.",
"messages": [
{"role": "user", "content": "[prompt]"}
]
}'
Multi-Turn Conversation
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: [api_key]" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "[model]",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What is its population?"}
]
}'
Streaming Response
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: [api_key]" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "[model]",
"max_tokens": 1024,
"stream": true,
"messages": [
{"role": "user", "content": "[prompt]"}
]
}'
Tool Use (Function Calling)
Parameters:
tools(Required): Array of tool definitions with name, description, and input schema.tool_choice(Optional): Force a specific tool with{"type": "tool", "name": "tool_name"}.
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: [api_key]" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "[model]",
"max_tokens": 1024,
"tools": [{
"name": "get_weather",
"description": "Get current weather conditions for a city.",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"units": {"type": "string", "enum": ["celsius","fahrenheit"]}
},
"required": ["city"]
}
}],
"messages": [
{"role": "user", "content": "What is the weather in Berlin right now?"}
]
}'
Vision — Analyze an Image URL
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: [api_key]" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "[model]",
"max_tokens": 1024,
"messages": [{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://example.com/diagram.png"
}
},
{"type": "text", "text": "Describe what this diagram shows."}
]
}]
}'
Vision — Analyze a Local Image (Base64)
IMG_B64=$(base64 -w 0 /path/to/image.png)
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: [api_key]" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "[model]",
"max_tokens": 1024,
"messages": [{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "'"$IMG_B64"'"
}
},
{"type": "text", "text": "What is shown in this image?"}
]
}]
}'
Count Tokens (Dry Run)
curl https://api.anthropic.com/v1/messages/count_tokens \
-H "x-api-key: [api_key]" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "[model]",
"system": "You are a helpful assistant.",
"messages": [{"role": "user", "content": "[prompt]"}]
}'
Models Reference
| Model | Context | Best For |
|---|---|---|
claude-opus-4-5 | 200k | Complex reasoning, long-form analysis |
claude-sonnet-4-5 | 200k | Balanced speed and quality |
claude-haiku-3-5 | 200k | High-throughput, low-latency tasks |
Key Parameters
| Parameter | Type | Range | Description |
|---|---|---|---|
max_tokens | integer | 1–200k | Maximum tokens in response |
temperature | float | 0.0–1.0 | Randomness (0 = deterministic) |
top_p | float | 0.0–1.0 | Nucleus sampling threshold |
top_k | integer | 1–∞ | Top-K sampling |
stop_sequences | array | — | Strings that stop generation |
💡 Tip: For analytical tasks use
temperature: 0. For creative tasks,temperature: 0.7–1.0.
Error Handling
| HTTP Code | Error Type | Solution |
|---|---|---|
| 400 | invalid_request_error | Check required fields and JSON schema |
| 401 | authentication_error | Verify x-api-key header value |
| 403 | permission_error | Check API key scope and organisation |
| 429 | rate_limit_error | Implement exponential backoff |
| 529 | overloaded_error | Retry with backoff; reduce request rate |