Back to glossary

Term

XML tags in the prompt

XML tags are named brackets like <context>...</context> used to clearly separate sections of a prompt. They structure the input, split instruction from data and make the model's output more predictable.

XML tags in the prompt — explained in detail

XML tags in the prompt describe the technique of framing parts of an instruction to a language model with named markers — for example <instruction>...</instruction>, <context>...</context> or <example>...</example>. The notation borrows from XML/HTML: an opening tag <name>, the content, a closing tag </name>.

The point is not the XML format itself, but unambiguous separation. A longer prompt often mixes several parts: the actual task, background context, examples and the user data to be processed. Without clear separation, the model cannot reliably tell what is instruction and what is mere material. Tags draw visible boundaries and reduce that ambiguity.

XML tags are a form of delimiter. Other common delimiters are Markdown headings, triple backticks or lines of dashes. Tags have the advantage of being named and nestable — so within the same prompt you can refer clearly to one specific section (for example: “Only refer to the text inside <document>”).

Typical benefits

  • Clear structure: Instruction, context and data are visibly separated.
  • Robustness against mixing: Inserted user content is more likely treated as data rather than as a new instruction — which reduces the risk of prompt injection, though it does not fully prevent it.
  • Referenceability: Later in the prompt you can point to a specific tag name.
  • Structured output: If you ask the model to return its answer in tags as well (e.g. <answer>...</answer>), the result is easier to process programmatically.

Note: tag names are freely chosen and need not form valid XML. What matters is that they are opened and closed consistently and that their naming fits the task.

Example / Practical use

A summarisation task might be structured like this:

<instruction>
Summarise the following text in three bullet points.
Use only information from <document>.
</instruction>

<document>
[the long source text goes here]
</document>

This way the model “knows” exactly which part is the task and which part is the source to be processed. The approach can be combined with few-shot prompting by placing examples in tags such as <example>. XML tags are a frequently recommended pattern in prompt engineering — especially with models that were explicitly trained to attend to such structural markers.

  • XML tags vs. system prompt: The system prompt sets the overarching role/behaviour; XML tags structure the content within a prompt (whether system or user prompt).
  • XML tags vs. prompt template: A template is the reusable blueprint with placeholders; XML tags are one of the means by which a template organises its sections.
  • XML tags vs. structured output (JSON): Tags usually structure the input; if you need a machine-readable output, JSON is often the better choice. The two can be combined.
  • XML tags vs. real XML: The notation is only borrowed — it need not be schema-conformant or parseable. It is solely about visual and semantic separation for the model.

Related terms: prompt engineering, few-shot prompting, chain of thought, context engineering.

Entdecke mehr

Themenuebersicht