Advanced XML Tag Patterns
Once you're comfortable with the basics of <context>, <instructions>, and <input>, the real power of XML in prompts becomes available: hierarchical nesting, multi-document prompts, variable substitution patterns, and knowing precisely when XML is earning its complexity overhead.
Nested Hierarchical Tags
Nesting XML tags is the most direct way to represent complex, multi-layered information. Claude handles nested structures well because XML hierarchies are natural representations of parent-child relationships.
The key principle: nest when content has a natural containment relationship. A <documents> container holds multiple <document> elements. A <conversation> container holds <message> elements. Don't nest just for visual organization — nest when the parent-child relationship conveys real semantic meaning.
Example: Nested conversation history
<context>
<user_profile>
Name: Sarah Chen
Account tier: Enterprise
Industry: Healthcare
Previous interactions: 3 escalations in 90 days
</user_profile>
<conversation_history>
<message role="user" timestamp="2026-03-01T09:15:00">
We're having trouble with the bulk export feature.
</message>
<message role="assistant" timestamp="2026-03-01T09:16:00">
I'm sorry to hear that. Can you describe what happens when you try to export?
</message>
<message role="user" timestamp="2026-03-01T09:18:00">
The export starts but always fails at around 60% with no error message.
</message>
</conversation_history>
</context>
<instructions>
Based on the user's history and the current conversation, suggest the 3 most likely causes and a diagnostic step for each.
</instructions>
The nesting here communicates that user_profile and conversation_history are both part of the broader context, while message elements are components of the conversation_history. Claude can reference these relationships explicitly in its reasoning.
Example: Nested task breakdown
<task>
<objective>
Evaluate this business proposal for investment viability.
</objective>
<evaluation_criteria>
<criterion weight="high">Market size and growth rate</criterion>
<criterion weight="high">Team background and domain expertise</criterion>
<criterion weight="medium">Revenue model and unit economics</criterion>
<criterion weight="medium">Competitive differentiation</criterion>
<criterion weight="low">Current traction metrics</criterion>
</evaluation_criteria>
<output_format>
For each criterion, provide: rating (1-5), one-sentence evidence, one key risk.
End with an overall recommendation: PASS / CONSIDER / STRONG PASS
</output_format>
</task>
<proposal>
[proposal text here]
</proposal>
XML attributes like weight="high" are processed by Claude as additional metadata. You don't need to explain what weight means — the semantic is clear from context.
Multi-Document Prompts
When you need Claude to work across multiple source documents, the <documents> pattern is the most reliable approach. Without structure, Claude may lose track of which information came from which source.
The standard pattern from Anthropic's prompt engineering guidance:
<documents>
<document index="1">
<source>Q4 2025 Earnings Report</source>
<content>
[document text]
</content>
</document>
<document index="2">
<source>Q4 2024 Earnings Report</source>
<content>
[document text]
</content>
</document>
<document index="3">
<source>Industry Analyst Report — January 2026</source>
<content>
[document text]
</content>
</document>
</documents>
<instructions>
Compare performance across the two earnings reports and evaluate whether the analyst's projections from document 3 were accurate. Cite sources by document index when making specific claims.
</instructions>
The index attribute and <source> tag give Claude the ability to cite sources precisely: "According to document 2..." This is critical for any research or analysis workflow where traceability matters.
Try this pattern in the playground:
Combining XML with Markdown Inside Tags
XML tags and markdown are complementary, not competing. Markdown inside XML tags structures the content of a section; XML structures the semantic role of that section.
This combination works especially well for instructions that have their own internal structure:
<instructions>
## Primary Task
Analyze the provided code for security vulnerabilities.
## Output Requirements
For each vulnerability found:
- **Severity**: Critical / High / Medium / Low
- **Location**: File and line number
- **Description**: What the vulnerability is
- **Remediation**: Concrete fix with code snippet
## Constraints
- Focus only on OWASP Top 10 categories
- Do not flag style issues or non-security bugs
- If no vulnerabilities found, state that explicitly
</instructions>
The XML tag carries the semantic label (<instructions>); the markdown inside organizes the instructions for readability. Claude processes both layers correctly.
Variable Substitution Pattern
For reusable prompt templates — whether in code, an admin panel, or a prompt library — the double-brace variable pattern is the standard:
<context>
You are a customer success manager at {{company_name}}.
Current date: {{current_date}}
</context>
<instructions>
Write a check-in email to {{customer_name}} at {{customer_company}}.
Their subscription renews on {{renewal_date}}.
Tone: {{tone}}
Include: {{include_items}}
</instructions>
Variables in double braces ({{variable_name}}) are a convention, not a built-in syntax. They work because:
- Claude recognizes them as placeholders from its training data
- They visually distinguish dynamic content from static prompt structure
- Code that processes the template can replace them reliably with a simple string replacement
In a Python application, this looks like:
template = """
<context>
You are a customer success manager at {{company_name}}.
</context>
<instructions>
Write a check-in email to {{customer_name}}.
</instructions>
"""
filled = template.replace("{{company_name}}", company_name) \
.replace("{{customer_name}}", customer_name)
For more complex cases, use a templating library. The pattern is the same regardless of the mechanism.
When XML Adds Overhead vs When It's Essential
XML isn't free. It adds characters to your prompt (which means tokens and cost), it adds visual complexity, and it can make prompts harder to read in some contexts. Understanding when to use it is as important as knowing how.
XML is essential when:
| Scenario | Reason |
|---|---|
| Multiple documents are being analyzed | Prevents source confusion |
| Instructions are multi-step or conditional | Tags clarify what is instruction vs. context |
| Prompt is part of a production pipeline | Machine-readable structure improves reliability |
| You have user-provided content alongside system instructions | Tags enforce the authority hierarchy |
| The prompt will be reused as a template | Variables need structural anchoring |
XML adds overhead without benefit when:
- The prompt is a single question with no context separation
- You're in a rapid back-and-forth conversation where adding tags slows iteration
- The task is inherently unstructured (open-ended creative writing with minimal constraints)
- The "structure" you'd add is just one tag wrapping the entire prompt (pointless nesting)
A practical test: would a new developer reading this prompt in 6 months understand which parts are context, which are instructions, and what the input is? If not, XML tags would help. If the prompt is already self-evident, they're overhead.
Exercise: Build a Multi-Document Analysis Prompt
Key Takeaways
- Nest XML tags when content has a genuine parent-child relationship —
<documents>containing<document>elements is the canonical example - The multi-document pattern (
<documents><document index="1">...</document></documents>) enables source-cited analysis across multiple inputs - XML and markdown are complementary: XML carries semantic labels, markdown organizes content within those sections
- Variable substitution with
{{variable_name}}is the standard pattern for reusable prompt templates - XML adds cost and complexity — use it when you have multi-component prompts, production pipelines, or content that needs structural separation; skip it for simple conversational prompts
Discussion
Sign in to join the discussion.

