Claude XML Tags: Improve Prompt Accuracy and Structure
XML tags are Claude's native structure. Here's how to use them to write clearer, more reliable prompts, with templates and real examples.
XML tags are Claude's native structure. Here's how to use them to write clearer, more reliable prompts, with templates and real examples.
Key Takeaways
- XML tags create explicit boundaries in complex prompts, preventing Claude from confusing your instructions, data, and context.
- They are a structural layout tool that should be paired with strong prompting frameworks like chain of thought.
- There are no official tag names to memorize; just use descriptive labels consistently and always close your tags.
- XML tags offer clearer section boundaries than Markdown headings and are more natural for writing conversational instructions than JSON.
If you have spent any time writing prompts for Claude, you have probably noticed something odd: the model seems to "get it" much faster when your instructions are wrapped in angle brackets. That is not a coincidence, and it is not a superstition either. It is a documented, trainable behavior.
XML tags, things like <role>, <instructions>, or <context>, are a way of labeling the different parts of your prompt so Claude knows exactly what each chunk of text is for. Instead of writing one long paragraph that mixes background information, task instructions, and formatting requirements into a single wall of text, you separate each piece into its own tagged section.
What it is: A lightweight markup convention, borrowed from XML, used to structure prompts into clearly labeled sections.
How it works: You wrap each functional part of your prompt (role, context, task, examples, output format, constraints) in a matching pair of custom tags. Claude reads the tag names and treats the content inside as belonging to that category.
Why you should use them:
<answer> tags) make it trivial to pull specific data into your codebase.When to use it: Any time your prompt has more than one moving part. A single, simple question like "What is the capital of Peru?" does not need tags. A prompt that combines a data set, task instructions, a tone requirement, and three examples absolutely does.
XML tags are a convention, not a rigid syntax standard. Claude does not require your tags to be valid XML in the strict programming sense. There is no schema to register and no tag list to memorize. What matters is that your tags are descriptive and used consistently throughout the prompt.
This guide walks through why XML tags work so well with Claude, the difference between structure and strategy in prompting, ten tag names you can start using today, practical examples, and how XML tags stack up against Markdown and JSON for prompt structuring.
The short answer is training data. During Claude's training, Anthropic's own documentation explains that wrapping each type of content in its own tag reduces the chance that Claude misinterprets your prompt. Anthropic's engineers used XML-style tags extensively to delineate different parts of training examples, documents, worked examples, and reasoning steps. That pattern became deeply embedded in how the model parses text.
Practically, this means Claude does not just see <document> as five characters of markup. It recognizes the tag as a signal: "everything inside this boundary is reference material, not an instruction to follow." The same goes for <instructions>, <examples>, and dozens of other sensible tag names.
Here is what that buys you, according to Anthropic's guidance on structuring prompts:
<answer> or <summary>), you can extract just that portion programmatically, without regex gymnastics on a wall of unstructured prose.This is not just an academic nicety. It is also becoming a workplace expectation. AI adoption surveys covered in industry reporting show that Claude has become one of the more trusted tools for high stakes knowledge work, in part because instruction following and long document accuracy matter more to enterprise buyers than flashy features. Forbes reported that Anthropic's edge with business buyers has come from reliable, instruction faithful output that holds up in production, not consumer gimmicks. Well structured prompts are a big part of getting that reliability in practice.
If you only remember one thing from this section, remember this: XML tags do not make Claude smarter. They make Claude less confused. Confusion, not capability, is usually the real bottleneck in mediocre prompt outputs.
It helps to separate two ideas that often get mashed together when people talk about "prompt engineering."
Prompting structure is the layout of your prompt: how you physically arrange text on the page. XML tags, Markdown headings, numbered lists, and delimiters all belong here. Structure is about legibility.
Prompting framework is the mental strategy behind what you are asking for: how you break a task into steps, whether you ask for reasoning before an answer, how you handle ambiguity, and how you sequence multi-turn tasks. Chain of thought prompting, role prompting, and few-shot learning all belong here.
Here is the important part: XML tags are a structure tool, not a strategy on their own. You can have a beautifully tagged prompt that still asks a vague, poorly reasoned question. And you can have a great strategic approach buried inside an unstructured paragraph that Claude has to work hard to parse.
The two work best together. A solid framework (clear goal, relevant context, a request for step by step reasoning) becomes far more effective once it is laid out with tags, because Claude can locate each part instantly instead of inferring it from prose.
| Aspect | Prompting Structure | Prompting Framework |
|---|---|---|
| Focus | How the prompt looks | How the task is thought through |
| Tools | XML tags, Markdown, delimiters | Chain of thought, role prompting, few-shot examples |
| Fixes | Misread instructions, mixed up sections | Vague goals, weak reasoning, inconsistent logic |
| Analogy | The folder and label system on a filing cabinet | The actual filing policy inside the folders |
Think of structure as the folder labels and framework as the filing policy. Good labels do not fix a bad filing policy, but a good filing policy is much easier to follow when the folders are labeled clearly.
There is no official, locked-in list of tags Claude recognizes. Anthropic is explicit that there are no "canonical" tags. What matters is that your tag name describes its content and that you use it consistently. That said, certain names have become de facto standards because they map cleanly onto the parts of a prompt people actually write.
<role> – Defines who Claude should act as (a senior analyst, a copy editor, a customer support agent).<context> – Background information Claude needs before it can reason about the task (industry, audience, prior decisions).<instructions> – The actual task steps. This is your "do this" section.<data> – Raw input Claude should analyze, summarize, or transform, kept separate from your instructions.<examples> – Sample input/output pairs for few-shot prompting.<document> – A full reference document, especially useful when combined with <source> for citations.<constraints> – Hard limits: word counts, tone restrictions, things to avoid.<output_format> – The exact shape you want the answer in (bullet list, table, JSON, specific headers).<thinking> – Where Claude works through its reasoning before answering, useful for chain of thought tasks.<answer> – The final, clean response, separated from the reasoning that produced it.You are also free to get specific. Tags like <customer_feedback>, <meeting_transcript>, or <q3_report> work just as well as generic ones. Claude reads tag names semantically, so a descriptive tag will often communicate more than a generic one.
A handful of habits separate prompts that consistently work from ones that occasionally work.
Be consistent with tag names. If you call something <context> at the top of your prompt, refer to it as "the context" later, not "the background info." Consistency helps Claude and helps you when you revisit the prompt weeks later.
Nest tags for hierarchy. If one section logically contains another, nest it: <report><summary>...</summary><data>...</data></report>. This mirrors how Claude was trained to read structured documents.
Match tag density to prompt complexity. A two sentence request does not need five tags. Reserve tagging for prompts with genuinely distinct sections.
Place instructions after long context. When you are feeding Claude a lengthy document, put your actual task instructions after the document, not before. This helps Claude apply the task to material it has just processed rather than trying to hold instructions in mind across thousands of words.
Combine tags with other techniques. Wrap few-shot examples in <examples>, and pair <thinking> with <answer> for chain of thought tasks. Anthropic specifically recommends this combination for "super-structured, high-performance prompts."
Ask Claude to quote before it answers, for long documents. For long document tasks, asking Claude to pull relevant quotes into a tag like <relevant_quotes> before answering helps it focus on what matters and ignore filler.
Do not over-engineer simple asks. If your prompt is one clear sentence, tags add friction, not clarity. Structure is a tool for complexity, not a habit for its own sake.
A good gut check: if you can describe your prompt's sections in a single sentence ("here's the data, here's the task, here's the format"), you have at least three tags. If you cannot describe distinct sections at all, you probably do not need tags yet.
This template is ideal for when you need Claude to analyze raw data and present it in a specific format for stakeholders. By separating the role, context, data, and instructions into distinct tags, you prevent the model from confusing the background information with the actual numbers it needs to process.
<role>
{{role_description}}
</role>
<context>
{{background_information}}
</context>
<data>
{{raw_data_or_report}}
</data>
<instructions>
{{task_steps}}
</instructions>
<output_format>
{{desired_format}}
</output_format>
<role>
You are a senior FP&A analyst who explains numbers in plain English for non-finance executives.
</role>
<context>
This analysis is for a mid-market SaaS company preparing for a board meeting. The board cares most about churn and net revenue retention.
</context>
<data>
Q1 revenue: $2.1M, Q2 revenue: $2.4M, Q3 revenue: $2.3M, Q4 revenue: $2.6M.
Churn: 4.1%, 3.8%, 4.6%, 3.9%.
Net revenue retention: 104%, 106%, 101%, 108%.
</data>
<instructions>
1. Identify the quarter with the weakest performance and explain why it stands out.
2. Summarize the overall trend across the year in two sentences.
3. Flag any single metric the board should ask about.
</instructions>
<output_format>
Use three short sections with bold headers: Weakest Quarter, Yearly Trend, Board Question.
</output_format>
Other ways you might use the <output_format> tag:
When you need Claude to make a complex decision or solve a logic problem, asking it to "think" before it answers drastically improves accuracy. This template forces the model to show its work in a <thinking> tag before outputting the final, clean result in an <answer> tag.
<task>
{{decision_or_question}}
</task>
<constraints>
{{limits_or_requirements}}
</constraints>
Think step-by-step in <thinking> tags, then give your final recommendation in <answer> tags.
<task>
A client has $50,000 to invest and needs the money in five years for a house down payment. Compare a stock portfolio with 8% average historical returns against a bond ladder with a guaranteed 6% return.
</task>
<constraints>
Assume no withdrawals before year five. Account for typical market volatility in equities. Keep the final recommendation under 100 words.
</constraints>
Think step-by-step in <thinking> tags, then give your final recommendation in <answer> tags.
If you are asking Claude to compare multiple files or sources, wrapping each one in its own <document> tag keeps the context perfectly organized. This boundary prevents the model from bleeding details from one document into another, making it perfect for vendor evaluations, legal reviews, or research summaries.
<document>
<source>{{source_name_1}}</source>
<document_content>{{content_1}}</document_content>
</document>
<document>
<source>{{source_name_2}}</source>
<document_content>{{content_2}}</document_content>
</document>
<instructions>
{{comparison_task}}
</instructions>
<document>
<source>vendor-a-proposal.pdf</source>
<document_content>Implementation timeline: 6 weeks. Annual cost: $48,000. Includes onboarding and 24/7 support.</document_content>
</document>
<document>
<source>vendor-b-proposal.pdf</source>
<document_content>Implementation timeline: 10 weeks. Annual cost: $36,000. Onboarding is self-serve, support is business hours only.</document_content>
</document>
<instructions>
Compare the two vendors on cost, speed to launch, and support quality. Recommend one option for a lean 12-person team with limited internal IT resources.
</instructions>
Notice how, in every example, Claude never has to guess where the data ends and the instructions begin. That boundary clarity is the entire point.
XML tags are not the only way to organize a prompt. Markdown headings and JSON are common alternatives, and each has a place.
| Method | Best for | Weakness with Claude |
|---|---|---|
| XML tags | Multi-section prompts, long documents, chain of thought, anything Claude needs to parse precisely | Slightly more verbose to write than a heading |
| Markdown headings | Quick, readable prompts for humans; simple one or two section requests | Headings can visually resemble body text, which occasionally blurs section boundaries in dense prompts |
| JSON | Passing structured data Claude will process programmatically, or when the output itself needs to be machine readable | Less natural for instructions and reasoning; nesting deeply can get visually noisy inside a chat-style prompt |
XML tags vs Markdown headings:
## Context, ## Task) are readable and quick to write, and they work fine for shorter, simpler prompts.XML vs JSON in prompts:
<answer> tag, combining the strengths of both.None of these methods are mutually exclusive. A well-built production prompt often uses XML tags for the skeleton, Markdown for readability inside sections, and JSON for machine-readable output. Choose based on who, or what, reads the prompt and the response.
Even well-intentioned tagging can go wrong. Here is what to check when a tagged prompt is not producing the results you expect.
Claude is ignoring one section entirely. Check for a typo in your closing tag. <instructions>...</instructions> needs to match exactly. A mismatched tag name (opening <instructions> and closing </instruction>) can confuse the parsing.
Claude is blending instructions with your data. This usually means the data was not wrapped at all, or the instructions came before a large data block instead of after it. Move instructions to follow long data, and make sure the data has its own tag.
Output format keeps drifting from what you asked. Put format requirements in a dedicated <output_format> tag near the end of the prompt, close to where Claude starts generating. Conflicting instructions elsewhere in the prompt (for example, "be concise" in one section and "explain in detail" in another) will also cause drift; scan for contradictions.
Tags are not adding value. If your prompt only has one real section, or if you are tagging things like "Hello" and "Please," you are adding overhead without benefit. Strip tags back down to only the sections that actually need boundaries.
Reasoning quality is inconsistent on complex tasks. Pair your tags with a chain of thought instruction: ask Claude to think in <thinking> tags before answering in <answer> tags. This is a framework fix layered on top of a structure fix, and the two often need to be solved together.
Long documents produce shallow analysis. Ask Claude to quote the most relevant passages into a tag first, before doing the main task. Grounding the response in extracted quotes keeps Claude anchored to the material instead of skimming.
When debugging a prompt that is not working, isolate variables one at a time. Remove a tag, rerun the prompt, and see what changes. This tells you whether a specific tag was doing real work or just adding visual noise.
XML tags give Claude explicit boundaries between different parts of your prompt—like the role, context, data, and instructions. Because Claude’s training data heavily relied on tagged content, it treats these markers as hard structural rules rather than just visual formatting.
While short, simple requests don't require tags, they are essential for complex, multi-section prompts. They work best when paired with a strong prompting strategy, like chain-of-thought, serving as the structural foundation for your logic.
The bottom line:
1. Do I need to close every XML tag in a Claude prompt?
Yes, closing tags matter. An unclosed or mismatched tag can cause Claude to misjudge where a section ends, which defeats the purpose of tagging in the first place. Always pair <tag> with </tag>.
2. Are there official tag names Claude expects me to use?
No. Anthropic's own documentation confirms there is no canonical or required tag list. Descriptive, consistent names like <instructions> or <customer_data> work because they make sense to both you and the model, not because they are reserved keywords.
3. Does using XML tags work better than Markdown for every prompt?
Not for every prompt. Short, single-purpose requests do fine with plain text or a Markdown heading or two. XML tags earn their keep once a prompt has multiple distinct sections, like context, data, and formatting instructions, that need to stay clearly separated.
4. Can I combine XML tags with few-shot examples and chain of thought prompting?
Yes, and this combination tends to produce Claude's strongest results. Wrap your sample input/output pairs in <examples>, and separate reasoning from the final response using <thinking> and <answer> tags.
5. Will XML tags help Claude produce output I can parse programmatically?
Yes. If you ask Claude to place its final response inside a specific tag, like <answer> or <summary>, you can extract just that content from the response without complex text parsing, which is especially useful when Claude is part of an automated pipeline.