XML Tags Basics: Structuring Prompts for Claude
If you've used GPT, Gemini, or other models extensively, you may have heard the general advice to "be clear and specific." With Claude, there's a more precise technique that consistently outperforms plain natural language: XML tags. This isn't a preference quirk — it's rooted in how Claude was trained.
Why Claude Responds So Well to XML Structure
Claude was trained on a substantial corpus of text that included XML-formatted data: documentation, structured datasets, markup, configuration files, and Anthropic's own internal prompt formats. As a result, Claude has internalized XML as a structural language, not just as text to read through.
When you wrap content in XML tags, Claude does several things automatically:
- Identifies semantic roles:
<context>signals background information,<instructions>signals what to do,<input>signals the actual content to act on - Applies appropriate attention: Instructions inside
<instructions>are weighted differently than background text in<context> - Maintains separation: Multiple pieces of content in different tags don't bleed into each other in Claude's processing
This is fundamentally different from using markdown headers or plain prose to separate concerns. A ## Context header tells Claude to interpret what follows as context. An <context> tag tells Claude this content is context, at a deeper representational level.
Anthropic's own prompt engineering documentation recommends XML tags for any prompt with multiple distinct components. It's not stylistic — it's architectural.
The Core Tag Vocabulary
You don't need a large vocabulary to get most of the benefit. These six tags cover the vast majority of use cases:
<context> — Background information Claude needs but shouldn't act on directly. Company information, user history, domain knowledge, current date, situational constraints.
<instructions> — What you want Claude to do. The action directives. Can include numbered steps, constraints, tone requirements, and output format specifications.
<examples> — Few-shot examples showing input/output pairs. Wrapping examples in this tag signals to Claude that these are demonstrations, not new tasks to complete.
<input> — The content Claude should process. A document to summarize, a question to answer, code to review, text to translate.
<output> — (In input prompts) A specification of what the output should look like. Sometimes used as a prefill to guide Claude's response format.
<thinking> — Often used when you want Claude to reason step-by-step before giving its final answer. You can ask Claude to use <thinking> tags in its response for transparency.
Before and After: The Difference XML Makes
Here's the same prompt, first written as plain prose, then restructured with XML tags.
Plain prose version:
You are a helpful assistant working for Acme Corp, a B2B SaaS company that sells project management software to mid-market companies. You should respond professionally. The user has been a customer for 2 years and is on the Professional plan. They've opened 3 support tickets this month. Analyze the following message and decide if it should be escalated to the account management team or handled by tier-1 support, then draft a response. Message: "Your latest update broke our API integration and now our entire workflow is down. This is completely unacceptable."
XML-structured version:
<context>
Company: Acme Corp — B2B SaaS project management software for mid-market clients
Customer: 2-year customer, Professional plan, 3 support tickets opened this month
</context>
<instructions>
1. Classify this message: should it be escalated to account management or handled by tier-1 support?
2. List the key factors that drove your classification decision
3. Draft a professional response (2-3 paragraphs) that acknowledges urgency and sets clear next steps
</instructions>
<input>
"Your latest update broke our API integration and now our entire workflow is down. This is completely unacceptable."
</input>
The second version gives Claude the same information, but the structural separation lets Claude process each component appropriately. The context doesn't compete with the instructions. The instructions are unambiguous. The input is cleanly isolated.
Try both versions in the playground and compare the responses:
When to Use XML vs Plain Markdown
XML isn't always necessary. Understanding when it adds value versus when it adds noise is a key skill.
Use XML tags when:
- Your prompt has 3 or more distinct semantic components (context + instructions + input is the classic triple)
- You're passing long documents or multiple pieces of content that need to stay logically separate
- You're building reusable prompt templates where variables will be substituted
- The prompt will be used programmatically (in a pipeline, API call, or app)
- Instructions are complex or multi-step
- You need Claude to treat different sections with different levels of authority (e.g., system instructions vs. user-provided content)
Stick with plain text or markdown when:
- You're asking a simple, single-purpose question
- The prompt is conversational and context is minimal
- You're iterating rapidly in a chat interface and the overhead isn't worth it
- The task is self-contained with no separation of concerns needed
A rule of thumb: if you'd naturally use headers or bullet points to organize the prompt, those sections are probably better expressed as XML tags.
Common Structural Patterns
The instruction-input pattern (most common):
<instructions>
[What to do]
</instructions>
<input>
[Content to act on]
</input>
The context-instruction-input pattern (add background):
<context>
[Background information]
</context>
<instructions>
[What to do]
</instructions>
<input>
[Content to act on]
</input>
The few-shot pattern (add examples):
<instructions>
[Task description]
</instructions>
<examples>
<example>
<input>[Example input]</input>
<output>[Expected output]</output>
</example>
</examples>
<input>
[Actual content to process]
</input>
Tag names are not reserved words — Claude responds to any meaningful XML tag name. <document>, <user_message>, <article>, <code>, <query> all work. The key is using consistent, semantically meaningful names and staying consistent within a prompt.
Exercise: Restructure a Messy Prompt
Key Takeaways
- Claude processes XML tags at a structural level, not just as formatted text — this is why XML outperforms markdown headers for prompt organization
- The core tag set covers most scenarios:
<context>,<instructions>,<examples>,<input>,<output> - XML is most valuable for prompts with 3+ distinct components, complex instructions, or reusable templates
- For simple single-purpose questions, plain text works fine — use XML when it adds structural clarity
- Tag names are flexible: use descriptive names that match your domain (
<document>,<user_query>,<article>) - The consistent pattern is: context first, then instructions, then the input to process
Discussion
Sign in to join the discussion.

