aiwithgrant
about me
aiwithgrantGuidesAnthropic
Anthropic
Anthropic
Official Docs
Beginner

Use XML Tags to Structure Prompts

Structure your prompts with XML tags for clarity, accuracy, and easy parsing of Claude's output.

Official Anthropic docs →
Content sourced from official Anthropic documentation
1

Why use XML tags?

When prompts involve multiple components (context, instructions, examples), XML tags are a game-changer. Benefits: Clarity, Accuracy, Flexibility, and Parseability. There are no canonical 'best' XML tags — just use names that make sense for the content they surround.

💡Use tags like <instructions>, <example>, and <formatting> to clearly separate different parts of your prompt. This prevents Claude from mixing up instructions with examples or context.
2

Tagging best practices

Be consistent: use the same tag names throughout and refer to them by name. Nest tags for hierarchical content.

💡Combine XML tags with other techniques like multishot prompting (<examples>) or chain of thought (<thinking>, <answer>) for super-structured, high-performance prompts.
3

Financial report generation

Without XML tags, Claude produces a verbose letter-style report. With tags, it generates a concise, properly formatted report.

Q2 financial report
You're a financial analyst at AcmeCorp. Generate a Q2 financial report for our investors.

AcmeCorp is a B2B SaaS company. Our investors value transparency and actionable insights.

Use this data for your report:
<data>{{SPREADSHEET_DATA}}</data>

<instructions>
1. Include sections: Revenue Growth, Profit Margins, Cash Flow.
2. Highlight strengths and areas for improvement.
</instructions>

Make your tone concise and professional. Follow this structure:
<formatting_example>{{Q1_REPORT}}</formatting_example>
The tags prevent Claude from confusing the Q1 reference report with the actual data. Output goes from a verbose letter to clean bullet points with arrows for trends.
4

Legal contract analysis

Without XML tags, Claude's analysis is disorganized. With tags, it produces structured, actionable findings.

Contract risk analysis
Analyze this software licensing agreement for legal risks and liabilities.

We're a multinational enterprise considering this agreement for our core data infrastructure.

<agreement>{{CONTRACT}}</agreement>

This is our standard contract for reference:
<standard_contract>{{STANDARD_CONTRACT}}</standard_contract>

<instructions>
1. Analyze these clauses:
   - Indemnification
   - Limitation of liability
   - IP ownership
2. Note unusual or concerning terms.
3. Compare to our standard contract.
4. Summarize findings in <findings> tags.
5. List actionable recommendations in <recommendations> tags.
</instructions>
With tags, Claude outputs structured <findings> with clause-by-clause analysis and <recommendations> with specific counter-proposals the legal team can act on immediately.
5

Power user patterns

Common tag patterns to build your personal prompting framework: <document>/<source>/<document_content> for multi-document inputs, <instructions> for task steps, <examples> wrapping individual <example> tags, <thinking>/<answer> for chain of thought, <data> for input data, <output_format> for format specification.

Key topics covered

XML tags
Prompt structure
Output parsing
Nested tags
Document organization
Formatting
Read the full guide
View the complete Anthropic documentation
Official docs →

More guides