Skip to main content

Structuring Prompts with XML Tags

Intermediate

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:

<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

  • 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

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

  • 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.

Next