How to Structure Claude Prompts with XML (and Why It Works)
Last updated: June 2026
This guide to the Claude XML prompt format shows you how to use role, context, task, and output_format tags to get more consistent, on-target results. Claude is not GPT — it was trained differently, it responds differently, and it understands XML structure natively. If you are still writing free-form paragraphs and wondering why Claude drifts off-topic or ignores half your instructions, structured XML prompts are the fix.
This guide covers the format, shows real examples, and explains the mechanics behind why it works.
Why XML? The Short Answer
Anthropic trained Claude on data that included a significant amount of XML-tagged content. As a result, Claude parses tagged sections as labeled containers — not just prose. When you wrap instructions in <task> tags, Claude treats that section with more precision than a plain sentence buried in a paragraph.
Anthropic’s own documentation states that structured XML prompts produce more consistent outputs compared to unstructured equivalents — particularly on complex, multi-part instructions. The official docs describe XML as the primary recommended method for structuring complex prompts.
Plain English works for simple questions. For anything with multiple requirements — role, format, constraints, examples — XML is the right tool.
The Core of the Claude XML Prompt Format
A well-built Claude prompt typically has four sections:
<role>
You are a senior backend engineer reviewing a pull request for security issues.
</role>
<context>
The codebase is a Node.js REST API. The team uses TypeScript. PCI-DSS compliance is required.
</context>
<task>
Review the attached diff. Identify any security vulnerabilities or compliance risks.
</task>
<output_format>
Return a numbered list. For each issue: severity (critical / high / medium / low),
file and line reference, explanation, and suggested fix.
</output_format>
You do not have to use exactly these tag names. Anthropic’s docs are explicit: there are no “correct” tag names — use ones that describe what the section contains. What matters is consistency and clarity.
What Each Section Does
<role> — Anchors Claude’s perspective before it reads anything else. A specific role activates relevant knowledge and filters tone. “Senior backend engineer” produces different responses than “helpful assistant”.
<context> — Everything Claude needs to know before acting. Constraints, background, existing decisions. Put it here, not inline with the task.
<task> — One clear action. If you find yourself writing “and also”, split it into two tasks or two prompts.
<output_format> — Exactly how the output should look. Numbered list, JSON, markdown table, bullet points with headers — be specific. This is the section most people skip, and it is the one that causes the most reformatting work downstream.
A Minimal Everyday Example
Not every prompt needs four sections. For a quick rewrite request, two is enough:
<task>
Rewrite the following paragraph to be direct and under 50 words.
</task>
<context>
{{paste_your_paragraph_here}}
</context>
The rule of thumb: use a tag whenever you have a distinct type of information (who, what, how, format). Skip tags for single-sentence inputs.
Nesting for Complex Inputs
When you pass multiple documents or examples, nest tags:
<documents>
<document index="1">
<source>Q3 earnings call transcript</source>
<content>{{transcript}}</content>
</document>
<document index="2">
<source>Analyst report</source>
<content>{{report}}</content>
</document>
</documents>
<task>
Identify the three biggest discrepancies between management's statements and analyst findings.
</task>
This pattern is Anthropic’s official recommendation for multi-document prompts. Claude tracks each document separately instead of blending them into one undifferentiated block.
Common Mistakes
Over-tagging. If a section is one sentence, it does not need a tag. Tags add value when they separate meaningfully different types of content.
Burying the task. Put <task> near the top or clearly after context. A wall of context before the task makes Claude weight the context too heavily.
Vague output format. “Give me a summary” tells Claude nothing about length, structure, or audience. “Return a three-bullet executive summary, each bullet under 20 words” is actionable.
Mixing constraints with the task. Constraints (“do not include PII”, “avoid jargon”) belong in <context> or a dedicated <constraints> section, not inline with <task>.
Doing This by Hand Gets Tedious
Once you understand the format, the next problem is speed. Wrapping every draft in XML tags and filling in four sections every time adds friction — especially when you are writing prompts inside Claude.ai, ChatGPT, VS Code, or Slack.
Prompt Enhancer is a native macOS menu-bar app that does this automatically. Select any draft text in any app, press ⌃⌥⌘P, and it rewrites your draft into a structured <role>/<context>/<task>/<output_format> prompt in under four seconds — in place, in whatever app you are already using.
It is not a browser extension. It works system-wide. Free and trial enhancements run through our managed proxy, which never logs prompt content; on Pro you can enable BYOK — your own Anthropic API key, so prompts go directly to api.anthropic.com with no proxy. Start with 3 free enhancements (no signup) to see if it fits your workflow.
Pricing is $29 one-time. See pricing.
XML Prompts for Code Review
Code review is where structure pays off most: you want Claude focused on one diff, with the stack and standards declared up front so it does not invent rules. Put the constraints in <context>, the single review action in <task>, and pin the severity-graded shape in <output_format>. The result reads like a real reviewer’s notes instead of a vague “looks fine”.
<role>
You are a senior Go engineer reviewing a pull request.
</role>
<context>
Service handles payments. Go 1.22, no external deps without approval.
Errors must be wrapped with %w. Tests are required for new logic.
</context>
<task>
Review the diff below for correctness, error handling, and missing tests.
</task>
<output_format>
Numbered list. Per issue: severity (critical/high/medium/low),
file:line, why it matters, suggested fix.
</output_format>
XML Prompts for Writing
Writing tasks drift when the brief is one run-on sentence. Separate who is speaking (<role>), what the piece is and who reads it (<context>), the actual ask (<task>), and the hard limits — length, tone, format — in <output_format>. That last tag is what stops Claude from handing back twice the word count or the wrong register. Tag the source material too, so Claude rewrites it rather than starting from scratch.
<role>
You are a B2B copywriter who writes plainly, no hype.
</role>
<context>
Audience: technical founders. The product is a developer API.
Voice: confident, specific, zero filler adjectives.
</context>
<task>
Rewrite the draft below as a landing-page intro.
</task>
<output_format>
Two short paragraphs, under 90 words total. No exclamation marks.
</output_format>
XML Prompts for Data Extraction
Extraction is the use case where format precision is everything — you usually feed the output straight into code. Wrap the raw input in its own tag so Claude never confuses source text with instructions, state exactly which fields you want in <task>, and specify the schema in <output_format> down to the key names and null handling. A pinned JSON shape is the difference between a parseable result and a paragraph you have to clean up by hand.
<task>
Extract structured contact data from the email below.
</task>
<input>
{{paste_raw_email_here}}
</input>
<output_format>
Return JSON only, no prose:
{ "name": string, "company": string|null,
"email": string, "intent": "sales"|"support"|"other" }
</output_format>
Summary
| Principle | Why it matters |
|---|---|
| Use XML tags for multi-part prompts | Claude parses tagged sections as distinct containers |
<role> before <task> | Anchors Claude’s perspective before it reads instructions |
Explicit <output_format> | Eliminates reformatting work; Claude follows it precisely |
Nest <document> tags for multi-doc inputs | Prevents Claude from blending separate sources |
| Avoid over-tagging | One-sentence sections do not need a tag |
If you would rather start from proven structures than build each prompt from scratch, see our Claude prompt templates — ready-to-copy XML scaffolds for code review, writing, and data extraction.
Sources: Use XML tags to structure your prompts — Claude Docs · Prompting best practices — Claude API Docs
How they compare
| Feature | Prompt Enhancer | Alternative |
|---|---|---|
| Applies Anthropic’s official XML prompt standard | Yes — role/context/task/output_format structure applied automatically | Most generic prompt improvers use ad-hoc rewrites, not the XML standard |
| Works in any macOS app (system-wide) | Yes — global hotkey, works in VS Code, Claude.ai, Slack, Notion, Figma, etc. | Browser extensions (e.g. AIPRM) work only inside the browser |
| BYOK / privacy — no proxy, no server logs | Yes — API key stored in macOS Keychain, traffic direct to api.anthropic.com | Managed tools like BoltAI/Kerlig route through their infrastructure by default |
| Price model | $29 one-time — no subscription | BoltAI $49, Kerlig $69 — one-time; some tools charge monthly |
| App size / footprint | ~793 KB native Swift, Apple Silicon arm64 | Electron-based alternatives typically 200–400 MB |
| Free tier | 3 free, no signup, then a 7-day free trial (BYOK is a Pro feature) | Varies — most require paid tier or subscription for unlimited use |
Honest caveats
- The macOS DMG is Apple-notarized since June 2026 — installs without Gatekeeper warnings. The Windows installer shows a one-time SmartScreen prompt while code-signing reputation builds.
- The app launched public beta on 2026-05-23 and v0.1.1 on 2026-05-27. It has no track record yet — no independent reviews, no published user counts.
- Linux is not available. A Linux build is on the Q3 2026 waitlist.
- The Windows .msi build exists but the primary focus and polish is on macOS. Windows users should evaluate before buying.
- BoltAI and Kerlig are mature, well-reviewed products with more features (chat UI, model switching, etc.) — Prompt Enhancer does one focused thing and is priced accordingly.
- The claim about XML prompts producing ‘more consistent outputs’ comes from Anthropic’s documentation summary and third-party reporting; Anthropic has not published a specific percentage for the general public endpoint.