Structuring with XML Tags
Why Claude Loves Structure
Claude (and other LLMs) perform better with structured input because:
- Clear boundaries - Knows where information starts and ends
- Semantic labels - Understands what information represents
- Easy reference - Can refer back to specific sections
- Consistent parsing - Reliable pattern matching
XML Tags vs. Other Formats
| Format | Best For | Example |
|---|---|---|
| XML Tags | Structured prompts | |
| Markdown | Documentation, headers | ## Instructions |
| JSON | Data structures | {"instructions": "..."} |
| Plain text | Simple prompts | Instructions: ... |
Basic XML Tag Usage
Wrapping Content
<user_input>
The text that the user provided goes here.
It can span multiple lines.
</user_input>
Labeling Sections
<task>Summarize the following article</task>
<article>
Article content goes here...
</article>
<requirements>
- Keep summary under 100 words
- Include main thesis
- List key points
</requirements>
Nesting for Hierarchy
<context>
<domain>Healthcare</domain>
<audience>Medical professionals</audience>
<purpose>Treatment recommendation</purpose>
</context>
Naming Conventions
Use clear, descriptive tag names:
<!-- Good -->
<customer_feedback>...</customer_feedback>
<product_description>...</product_description>
<analysis_output>...</analysis_output>
<!-- Bad -->
<data>...</data>
<text>...</text>
<output>...</output>
Best practices:
- Use snake_case for multi-word tags
- Be specific about content
- Avoid generic names
- Match output tags to input tags
Real-World Examples
Example 1: Document Analysis
You are a document analyst. Analyze the following document.
<document type="contract">
SERVICE AGREEMENT
This agreement is entered into between Company A ("Provider")
and Company B ("Client") on January 15, 2025...
</document>
<analysis_requirements>
<extract>
- Parties involved
- Effective date
- Key obligations
- Termination clauses
</extract>
<format>
Provide a structured summary with sections for each item
</format>
</analysis_requirements>
Output your analysis in <analysis> tags.
Example 2: Code Review
Review the following code for issues:
<code language="python">
def calculate_total(items):
total = 0
for item in items:
total = total + item.price
return total
</code>
<review_criteria>
- Performance issues
- Code style
- Potential bugs
- Best practices
</review_criteria>
Provide feedback in <code_review> tags.
Example 3: Multi-Input Analysis
Compare these two product descriptions:
<product_a>
<name>Widget Pro X</name>
<price>$299</price>
<description>
Professional-grade widget with advanced features...
</description>
</product_a>
<product_b>
<name>Widget Basic</name>
<price>$99</price>
<description>
Entry-level widget for beginners...
</description>
</product_b>
<comparison_criteria>
- Value for money
- Feature set
- Target audience
- Marketing clarity
</comparison_criteria>
Using Tags for Output
Requesting Tagged Output
Analyze the sentiment of this review:
<review>
I loved this product! The quality exceeded my expectations,
though shipping was a bit slow.
</review>
Respond with:
<sentiment_analysis>
<overall>positive/negative/mixed</overall>
<score>1-10</score>
<positive_aspects>...</positive_aspects>
<negative_aspects>...</negative_aspects>
<summary>...</summary>
</sentiment_analysis>
Benefits of Tagged Output
- Easy parsing - Extract specific fields programmatically
- Consistency - Same structure every time
- Validation - Check for required elements
- Integration - Feed into downstream systems
Parsing XML Output
In your application:
// Example: Extracting tagged content
function extractTag(response, tagName) {
const regex = new RegExp(`<${tagName}>([\\s\\S]*?)</${tagName}>`);
const match = response.match(regex);
return match ? match[1].trim() : null;
}
// Usage
const sentiment = extractTag(response, 'overall');
const score = extractTag(response, 'score');
Common Patterns
Input-Output Mirroring
<!-- Input -->
<user_query>How do I reset my password?</user_query>
<!-- Request output in matching structure -->
Please respond with:
<response>
<answer>...</answer>
<steps>...</steps>
<related_topics>...</related_topics>
</response>
Conditional Sections
<instructions>
If the query is a question, include <answer> tags.
If the query requires steps, include <steps> tags.
If uncertain, include <clarification_needed> tags.
</instructions>
Metadata Tags
<output>
<metadata>
<confidence>high/medium/low</confidence>
<sources_used>...</sources_used>
<processing_notes>...</processing_notes>
</metadata>
<content>
The actual response content...
</content>
</output>
Key Takeaways
- XML tags add structure - Claude understands and respects boundaries
- Use descriptive names - Clear labels help the model understand context
- Be consistent - Use the same tags throughout your prompt
- Request tagged output - Makes parsing reliable
- Nest for hierarchy - Group related information
Next Lesson
Next, we'll explore Few-Shot Learning with Examples - how to demonstrate expected behavior through input/output pairs.