Claude XML cheatsheet
One-screen condensed reference. Bookmark this.
Core tags
| Tag | Purpose | Sibling of |
|---|---|---|
<instructions> | What the model should do | context, examples, documents, question |
<context> | Background facts (not commands) | instructions, documents, question |
<examples> | Container for few-shot demos | instructions, question |
<example> | One demo (with <input>/<output>) | nested in examples |
<documents> | Container for RAG passages | instructions, question |
<document> | One passage (with <source>) | nested in documents |
<source> | Citation ID for a document | nested in document |
<thinking> | CoT scratchpad | answer |
<answer> | Extractable final answer | thinking |
<format> | Output schema / shape | instructions, question |
<role> | Inline persona | standalone |
Recommended order
For prompts with long reference material:
<documents>...</documents>
<examples>...</examples>
<instructions>...</instructions>
<question>...</question>
For prompts without long material, instructions can come first.
Rules of thumb
- Long documents → put them first.
- Want to parse the output → wrap it in a tag and tell Claude "return only inside the tag."
- Want citations → always include
<source>. - Want reliability → 2–5 examples, not 0, not 20.
- Want safety → sanitize user input before splicing into prompts.
- Want structured output → use API tool use first; XML
<format>second.
Extraction regex
To pull content from a tag:
<answer>([\s\S]*?)</answer>
Use the multi-line, non-greedy form in all languages. [\s\S] beats . because it matches newlines.
Cite this page
Claude XML Cheatsheet. claudexml.com. https://claudexml.com/cheatsheet/