CCAO-F Domain 2: Prompt Structuring with XML Tags
How to organize complex prompts, reference documents, few-shot examples, and output targets using Anthropic's recommended XML tag conventions.
1. Why Anthropic Recommends XML Tags
Claude models are specifically fine-tuned to recognize and parse XML-style tags (such as <context>, <instructions>, <rules>). Using XML tags offers three major advantages:
- Clean Separation: Distinguishes system directives from user-provided source data.
- Prompt Injection Mitigation: Isolates untrusted user input inside dedicated data tags (e.g.,
<user_input>), preventing malicious text from hijacking system instructions. - Targeted Extraction: Enables referencing specific sections in your instructions (e.g., "Summarize only the text found inside <financial_report> tags").
2. Standard Anthropic XML Tag Conventions
| XML Tag | Standard Usage |
|---|---|
<context>...</context> |
Background knowledge, company policies, domain documentation. |
<instructions>...</instructions> |
Step-by-step procedures, behavioral rules, calculation steps. |
<examples>...</examples> |
Demonstration pairs showing desired formatting and tone. |
<documents>...</documents> |
Raw reference materials or multiple numbered documents (<document id="1">). |
<thinking>...</thinking> |
Scratchpad space for Claude to reason prior to generating output. |
3. Proper Tag Nesting & Multi-Document Organization
When passing multiple documents or complex data schemas, maintain strict tag nesting:
<documents>
<document id="1" title="Q3 Earnings">
Revenue grew by 14% year-over-year...
</document>
<document id="2" title="Product Roadmap">
Beta launch scheduled for October...
</document>
</documents>
4. Formatting Output Targets
You can also instruct Claude to wrap its final output in custom XML tags (e.g., <summary> or <json>), making downstream programmatic extraction straightforward via standard regex or DOM parsers.