Skip to content
mentoor

Topic 1 of 5 · 4 min read

XML tags and delimiters

Goal: Restructure a long prompt with consistent tags or delimiters so each instruction, document and example is clearly bounded.

Give every kind of content in a long prompt its own labelled box, and use the same labels every time. A model reads your prompt as one stream of text. If instructions, pasted documents and examples run together, it has to guess where one ends and the next begins.

Check yourself · 1 of 3

A 2,000-word prompt mixes rules, three pasted contracts and a question in one block. What is the best first fix?

Choose one answer

Why it matters

A short prompt is easy to read. A 2,000-word prompt with a role, rules, three documents, two examples and a question is not. Models such as Claude, ChatGPT and Gemini sometimes treat a sentence inside a pasted document as an instruction, or copy the style of an example into the wrong place. Clear boundaries remove that guesswork. They also help you: you can see at a glance what the prompt contains and edit one part safely.

How to do it

  1. Pick one delimiter style and keep it. Claude's docs recommend XML-style tags. OpenAI's GPT-4.1 guide finds XML and Markdown both work well and warns that JSON does badly for many documents. The best choice is the one you use consistently.
  2. Name tags for what they hold. Use <instructions>, <document>, <example>, <question>. Do not use <section1>.
  3. Nest when content has parts. Several documents go in a <documents> wrapper, each with its own <document>, and <source> and <content> inside.
  4. Refer to tags by name. Write "Answer using only the text inside <documents>." The name is now a handle.
  5. Close what you open. A missing closing tag is the most common bug.

Before and after

Before, everything is one block:

You are a support analyst. Here is the ticket: My invoice is wrong. Here is the policy: Refunds within 30 days. Summarise the ticket and say if a refund applies. Example: Ticket: late delivery -> Summary: ...

After, each part is bounded:

<role>You are a support analyst.</role>
<policy>Refunds within 30 days.</policy>
<example>
  <ticket>Late delivery</ticket>
  <summary>Customer reports a late delivery. Refund not requested.</summary>
</example>
<ticket>My invoice is wrong.</ticket>
<task>Summarise the ticket, then say if a refund applies under the policy.</task>

What goes wrong

  • Mixed styles. Tags in one place, hashes in another, and bare text elsewhere. The boundaries become unreliable.
  • Vague tag names. <data> holding three different things.
  • Delimiters inside the content. A document that itself contains </document> or a row of dashes breaks your structure. The third topic deals with this.
  • Believing tags are magic. They help the model, but nothing enforces them. Test with real inputs.

Examples

Three documents, one question

<documents>
  <document index="1">
    <source>pricing-2026.md</source>
    <content>Pro plan: 20 USD per seat per month.</content>
  </document>
  <document index="2">
    <source>faq.md</source>
    <content>Annual billing gives two months free.</content>
  </document>
</documents>
<question>What does one Pro seat cost per year with annual billing?</question>

Each document has a source, so the answer can cite it, and the question cannot be mistaken for part of a file.

Markdown headings instead of tags

## Instructions
Return JSON only.

## Input
{{ticket_text}}

## Output format
{"category": "...", "urgent": true}

Headings are fine when the content has no markup of its own. Keep the heading names identical across every prompt in the codebase.

Key points

  • Give each kind of content its own labelled section: instructions, documents, examples and the question.
  • Use one delimiter style throughout; XML-style tags and Markdown headings both work, mixing them does not.
  • Name tags for what they hold and refer to them by name in the instructions.
  • Wrap several documents in an outer tag with one inner tag each, plus source metadata.
  • Delimiters guide the model but are not enforced, so test the prompt with real inputs.

Common mistakes

  • Mixing tags, headings and bare text so the boundaries differ from one part of the prompt to the next.
  • Using vague tag names such as <data> or <section2> that say nothing about the content.
  • Forgetting a closing tag, which leaves the rest of the prompt inside the last section.
  • Putting examples inside the instruction text instead of their own tag, so the model copies their wording.

Check yourself · 2 of 3

Which tag name is the most useful in a prompt that holds a customer email?

Choose one answer

Check yourself · 3 of 3

A prompt wraps some documents in <doc> tags, separates others with --- lines, and lists rules under Markdown headings. The model sometimes merges the end of one document into the next. What is the best fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here