Skip to main content

Structuring Prompts with XML Tags

Intermediate
What you'll learn
  • Why XML-style tags give Claude crisp boundaries between prompt parts
  • How to wrap instructions, documents, examples, and format rules in named tags
  • How to ask Claude for tagged output you can parse reliably
  • When to tag — and when not to over-tag

When a prompt mixes instructions, a document, examples, and a question, the model can blur them together. XML-style tags are a clean way to label each part — and Claude responds especially well to them.

The idea

Wrap each section in a named tag so it's unambiguous what's what:

Tagged prompt structure

<instructions>
Summarize the document for a busy executive. Use only the document; if a fact
isn't there, say so.
</instructions>

<document>
{paste the long document here}
</document>

<format>
3 bullet points, then a one-line "decision needed".
</format>

The tags are just text you invent — <document>, <example>, <context>, <rules> — but they give the model crisp boundaries.

Why it helps

Pro tip
  • Separates data from instructions — the model is less likely to obey stray text inside a pasted document (a mild defense against prompt injection — see /docs/security/prompt-injection).
  • Reduces "it ignored part of my prompt." Each part is clearly delimited.
  • Makes outputs easier to parse — you can ask Claude to put its answer in <answer> tags and extract it reliably.
  • Composes with few-shot (/docs/prompting/few-shot) — wrap each example in <example>.

The four payoffs in detail:

  • Separates data from instructions — the model is less likely to obey stray text inside a pasted document (a mild defense against prompt injection).
  • Reduces "it ignored part of my prompt." Each part is clearly delimited.
  • Makes outputs easier to parse — you can ask Claude to put its answer in <answer> tags and extract it reliably.
  • Composes with few-shot — wrap each example in <example>.

Asking for tagged output

Guided walkthrough1 of 3
  1. Tell the model exactly which tags to use for reasoning vs. the final answer.

Request tagged output

Put your reasoning in <thinking> tags and your final answer in <answer> tags.

Then your code can grab just the <answer> content. Pairs well with structured output when you need machine-readable results.

Tips

Key takeaways
  • Be consistent — open and close every tag; reuse the same names.
  • Name tags meaningfully (<contract>, not <x>).
  • Don't over-tag trivial prompts — use this when there are genuinely multiple distinct parts.
  • Be consistent — open and close every tag; reuse the same names.
  • Name tags meaningfully (<contract>, not <x>).
  • Don't over-tag trivial prompts — use this when there are genuinely multiple distinct parts.

Check yourself

Check yourself

0/5
  1. What is the main reason XML-style tags help when a prompt mixes instructions, a document, and a question?
  2. Why do tags act as a mild defense against prompt injection?
  3. How can you reliably extract just Claude's final answer from a tagged response?
  4. Which is the best practice for naming tags?
  5. When should you NOT reach for XML tags?

Next