XML Tags: Structuring Long Prompts So the Model Doesn't Get Lost
When you paste a prompt into a chat, the visual formatting is lost. XML tags encode structure in the text itself and make long prompts and examples clearer and more consistent.
XML tags turn a flat block of instructions into clearly delimited sections that an LLM can interpret more precisely. They're most useful for long prompts and prompts with multiple examples (few-shot prompting), because they reduce ambiguity and make results more consistent.
The problem: visual formatting is not enough
In Google Docs or Word, headings, lists and indentation make a prompt easy for you to read. But when you paste it into an AI chat, most of that formatting is lost: the model receives a flat block of text, not the hierarchy you saw.
In class, a few-shot prompt with high-engagement and low-engagement tweets lost its line breaks and structure when pasted into Claude.
What is an XML tag?
A marker that delimits where a block of information starts and ends, so the model knows what belongs to what. XML is the ancestor of HTML, which was built to structure information on the internet. Each tag works like a container with an opening and a closing marker.
The syntax takes three steps: open with < + a descriptive word + >, insert the content, and close with </ + the same word + >.
<examples>
...content...
</examples>Rules and best practices
- Choose descriptive names: the word inside the tag becomes part of the prompt. <tweet> is clear; <birdie> can confuse the model.
- Use hyphens, not spaces, in multi-word tags: <high-engagement-tweet>.
- Opening and closing tags must be identical (except for the /).
- Nesting is allowed, but you must close tags in the correct order: the innermost tag closes first.
- Close each tag exactly where the block ends, not before and not after.
What if I don't close a tag?
There's no error message, because you are not programming. But the model may assume everything that follows belongs to that tag, which increases the chance that it misses the patterns in your examples or starts to hallucinate.
XML applied to few-shot
Restructure your prompt with three levels of nested tags: a general container (<examples>), a category container per example (<high-engagement-tweet> / <low-engagement-tweet>), and the actual content of each tweet inside it.
<examples>
<high-engagement-tweet>
...tweet content...
</high-engagement-tweet>
<low-engagement-tweet>
...tweet content...
</low-engagement-tweet>
<high-engagement-tweet>
...tweet content...
</high-engagement-tweet>
</examples>Why shuffle high- and low-engagement examples? Alternating them randomly makes the model more likely to learn what actually defines each category, instead of associating a category with its position in the list. Since every example carries its own label, you can reorder them freely without losing information.
Keep the prompt readable for humans
A prompt full of tags can be hard to read for you and your team. Add visual formatting in your working document: it doesn't affect the model.
- Indent nested containers to see where each block starts and ends.
- Color-code each tag type for quick differentiation.
- Keep line breaks inside examples when the format of the content matters (e.g., a tweet).
Pasting a colored and indented prompt into Claude gives the same interpretation as the plain version: the visual formatting is only for humans.
When to write prompts outside the AI chat
For short queries, typing directly in the chat box is fine. For long or structured prompts (examples, multiple sections), write them first in a document. Pressing Enter by accident sends an incomplete message and you lose your work; use Shift + Enter for a line break without sending.
- Draft the prompt in a document.
- Ask someone with no context to read it and tell you what they understood.
- Refine until it is clear.
- Copy, paste and run it in your preferred LLM.
This way you keep your train of thought, collaborate more easily with your team and make fewer errors as prompts grow.
Key takeaways
- XML tags preserve the structure that visual formatting loses on paste.
- Descriptive names, hyphenated, identical when opening and closing.
- Nesting is allowed; always close in reverse order.
- An unclosed tag gives no error, but the model may misread or hallucinate.
- In few-shot, label each example with its category, then shuffle the order.
- Indentation and colors are for you; the model ignores them.