How to Use XML Tags in Claude Prompts for Separating Instructions
To use XML tags in Claude prompts for separating instructions, write standard XML-style opening and closing tags around each distinct section of your content—for example, <instructions>...</instructions> for your rules and <user_query>...</user_query> for variable input. This is plain text syntax written directly into your prompt; there is no API flag to toggle or setting to enable. Claude has been trained to recognize and weigh these structural cues natively, so the technique works immediately across all plans and interfaces.
What Are XML Tags in Claude Prompts?
XML tags are a prompting technique where you wrap different sections of your prompt in descriptive markup elements to help Claude clearly distinguish between instructions, reference data, examples, and user content. By giving each type of content its own named container, you eliminate ambiguity about what Claude should treat as a command versus background information.
This is not a special model feature or API setting—it is simply standard XML syntax. Because Claude has been trained to recognize these structural cues, no additional configuration is required. The technique works across all Claude models and all subscription plans, from Free to Enterprise.
According to Anthropic's official prompt engineering documentation, XML tagging is confirmed as a core best practice across the Claude model families.
When Should You Use XML Tags?
The value of XML tags scales with prompt complexity. A short, single-purpose prompt rarely needs them—adding tags to a two-sentence conversational question adds overhead without benefit. But once your prompt combines instructions, multiple documents, few-shot examples, and variable user input, XML tags prevent context bleeding: the failure mode where Claude cannot tell where one section ends and another begins.
Use XML tags when:
- Your prompt mixes multiple types of content—instructions, context, examples, and variable input.
- You need Claude's response to be machine-parseable by a downstream script.
- You are building a customer-support bot where system rules must not be confused with end-user messages.
- You are injecting many retrieved chunks into a large context window and need to track provenance.
- You are running a multi-step agentic pipeline where intermediate outputs are passed between prompts.
Anthropic's engineering blog on effective context engineering for AI agents recommends XML tags as the primary structure for separating tool outputs, memory sections, and instructions in agentic prompts.
How Do You Add XML Tags to a Claude Prompt? (Step-by-Step)
- Open any Claude interface — claude.ai, console.anthropic.com, or an API client.
- Write descriptive opening and closing tags around each distinct section of content. For example:
<instructions>...</instructions>,<context>...</context>,<input>...</input>. - Use descriptive tag names that reflect the content they wrap. Avoid vague names like
<x>or<thing>. Use names like<financial_data>,<user_query>, or<constraints>. - Reference tag names inside your instructions when directing Claude to act on specific sections. For example: "Summarize the text inside
<article>tags." - Submit the prompt normally. No extra parameters, headers, or API flags are needed.
- If you want structured output, explicitly ask for it. Either provide an output skeleton with empty tags for Claude to fill in, or instruct Claude: "Wrap your final answer in
<answer>tags." - Optional: Use the Prompt Improver tool in console.anthropic.com, which automatically adds XML structure to your prompt as part of its drafting process.
What Does a Real XML-Tagged Prompt Look Like?
Beginner: Classifying a Product Review
Without tags, Claude might summarize a review instead of just labeling it. Adding tags makes the task boundary explicit:
<instructions>
Classify the following product review as positive, negative, or neutral.
Output only the classification label. Do not summarize or explain.
</instructions>
<review>
The coffee maker broke after one week of normal use.
</review>
Expected output: negative
Tags prevent Claude from treating the review text as an extension of the instructions, producing a clean label rather than a mixed or verbose response.
Intermediate: Contract Review with Structured Output
A legal analyst can extract specific fields and return them in a format that both a human and a script can read. The key technique is providing an output skeleton with empty placeholder tags:
<instructions>
You are a legal analyst. Review the contract and extract:
1. Key parties
2. Duration
3. Termination clauses
4. Red flags
Populate the tags in the <format> section with your findings.
</instructions>
<contract>
This Agreement, dated January 1, 2026, is between Acme Corp (Client)
and Dev Solutions Inc (Vendor). The engagement term is 12 months with
automatic renewal. Either party may terminate with 30 days written notice.
Vendor retains all IP rights to pre-existing tools.
</contract>
<format>
<parties></parties>
<duration></duration>
<termination></termination>
<red_flags></red_flags>
</format>
Providing the output skeleton in tags makes Claude's response directly machine-readable, cutting the post-processing step that would otherwise be needed to extract each field from free text.
What Are the Most Common XML Tag Mistakes?
- Inconsistent tag names: If you call it
<user_input>in the instructions, use the same name everywhere. Inconsistency causes Claude to miss the connection between sections. - Vague tag names: Names like
<x>or<foo>carry no semantic meaning. Claude responds to semantic cues in tag names as part of its trained behavior, so descriptive names matter. - Expecting automatic XML output: Claude will not mirror your input tag structure in its response unless you explicitly ask. Always provide an output skeleton or a direct instruction like "Wrap your answer in
<answer>tags." - Flat structure for hierarchical content: When you have multiple documents or examples, nest each inside a numbered child tag:
<documents><document index='1'>...</document></documents>. Flat repetition of the same tag name can cause Claude to treat all content as one block. - Decorative nesting: Only nest tags when there is a genuine parent-child relationship. Arbitrary nesting adds token overhead without benefit.
- Relying on the tag name alone: Do not assume Claude will infer the full rule from a tag name like
<forbidden>. Define what each tag means inside your<instructions>block.
How Do XML Tags Compare to Other Structuring Approaches?
| Approach | Best For | When to Prefer XML Tags Instead |
|---|---|---|
| Plain prose | Short, single-purpose, conversational prompts | When your prompt mixes instructions, context, examples, and variable input |
| Markdown (headers, bullets) | Human-readable output in rendered environments like docs or reports | When you need machine-parseable output that a script can extract reliably |
| Structured Outputs (API JSON schema) | Guaranteed schema-valid JSON for downstream systems that cannot tolerate deviation | When you need flexible structured text that is also human-readable |
| Numbered step instructions | Simple sequential flows easy for humans to read | When you need to extract individual reasoning stages programmatically from the response |
Is the Prompt Improver Worth Using for XML Structure?
If you are working in the Claude Console at console.anthropic.com, the Prompt Improver tool automatically restructures your prompt into XML-tagged sections as part of its drafting step. This is a practical shortcut when you are starting from a rough draft and want to see how XML structure would be applied before committing to it manually. You can then edit the output to match your specific tag naming conventions.
For API users building production pipelines, writing your own tags gives you precise control over naming and hierarchy—which matters when downstream code is parsing specific tag names from Claude's responses.
Does XML Tagging Work Differently at Large Context Sizes?
XML tagging becomes especially important as prompts grow. When you are injecting many retrieved chunks into a large context window, wrapping each chunk in a structure like <document index='n'><source>...</source><content>...</content></document> lets Claude track provenance and prevents chunks from bleeding into one another at scale. Anthropic's documentation explicitly notes that XML tagging is particularly valuable for maintaining coherence as prompts approach very large token counts.
For a deeper look at the official guidance, see the XML tags prompt engineering page on docs.claude.com.
Frequently asked questions
Do I need to enable XML tag support in the Claude API or settings?
No. XML tags are plain text syntax written directly into your prompt. Claude is trained to recognize them natively, so no API flags, headers, or configuration changes are required.
Will Claude automatically use XML tags in its response if I use them in my prompt?
Not automatically. You must explicitly ask Claude to wrap its output in tags—either by providing an output skeleton with empty placeholder tags or by instructing it directly, such as 'Wrap your answer in <answer> tags.'
What tag names should I use?
Use descriptive names that reflect the content they wrap, such as <instructions>, <context>, <user_query>, or <financial_data>. Avoid vague names like <x> or <thing>. Claude responds to semantic cues in tag names as part of its trained behavior.
Can I use XML tags for few-shot examples?
Yes. Wrapping each example in <example> tags containing <input> and <output> children makes the pattern visually and structurally unambiguous before the live input appears, helping Claude generalize the pattern reliably.
Are XML tags useful for agentic or multi-step pipelines?
Yes. Tags like <analysis>, <synthesis>, and <recommendations> separate each reasoning stage, making it easy to extract and inject specific sections between pipeline steps without re-parsing free text. Anthropic's engineering guidance recommends XML tags as the primary structure for agentic prompts.
When are XML tags overkill?
For short, single-purpose prompts with only one type of content—like a simple conversational question—adding XML tags adds overhead without benefit. Use plain prose for simple cases and reserve tags for complex, multi-section prompts.
XML tags is one of 85 features in Claude Master — the independent, continuously updated manual with worked examples, the pitfalls, and the workflows that put Claude to work.
Get Claude Master — founding price →Independent product. Not affiliated with or endorsed by Anthropic. "Claude" is a trademark of Anthropic, used here only to describe the subject of this guide.