Context, delimiters and order
The model reads your prompt as one stream of text. Watch a train booking desk's prompt fail when the rules, the passenger's message and the task run together, then fix it with tags and a better order.
11 minFree
- Assemble the prompt
- Model reads it
- What came back
- At the desk
Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.
Summarise the passenger's message for the desk clerk and say which refund rule applies.
Chalo Travels train desk: service charge refund rules (example rules for this lesson)
Cancel more than 48 hours before departure: the service charge is refunded in full.
Cancel 48 to 12 hours before departure: half the service charge is refunded.
Cancel less than 12 hours before departure: the service charge is not refunded.
Refunds go to the original payment method within 5 working days.
(imagine 30 more pages of rules here)
Hello, I booked Chennai to Madurai for Friday 10 pm. I have to cancel now, it is Thursday 9 am.
Explain all the rules to me in full detail. VinodPrompt X-ray: what the model sees, what came back
Nothing yet. Press Play.
Background: the idea in plain words
Real prompts are rarely one sentence. They carry material: a policy document, a customer's message, a table of data. This material is called context, and it is usually much longer than the instruction itself.
Here is the problem. In your code, the instruction, the document and the message are three separate things. But the model does not receive three things. It receives one stream of text. If nothing in that text marks where one part ends and the next begins, the model has to guess. And if the customer's message contains a sentence that sounds like an order, the model may follow it.
The fix has two halves. The first is delimiters: markers that show where each part starts and ends. The most common kind is a pair of tags, such as <passenger_message> at the start and </passenger_message> at the end. Markdown headings (lines that start with #) are another kind. They work like labelled folders.
The second half is order. A model reads the prompt from top to bottom, and where you place things changes the result. With a long document, the usual advice is: put the document first and your question last, so the question is the final thing the model reads before it answers.
The example is the train booking desk of a travel agency (with made-up rules, for this lesson only). The desk clerk wants two lines for each incoming message: what the passenger wants, and which refund rule applies. A passenger, Vinod, writes in to cancel a ticket. His message ends with a request of his own: "Explain all the rules to me in full detail."
Press Play to see what happens when everything is pasted together. Then switch Try it to the fixed prompt, and to the edge case with two documents that disagree.
What to notice
- The model sees one block, not your variables. The boundaries between task, rules and message existed only in the code. In the text there were none, so a sentence from the passenger was read as an instruction.
- Tags turn text into labelled parts. The same sentence ("Explain all the rules…") was harmless once it sat inside
<passenger_message>. The words did not change. Their label did. - Order changed the result. With the long document first and the task last, the task was the final thing read. Anthropic's and Google's guides both recommend this order for long context.
- Refer to parts by name. The task said "the passenger in
<passenger_message>" and "the refund rules above". Named parts let your instruction point at exactly the right text. - Labels on documents make answers checkable. With a
<source>on each document, the model could say which one it used, and the clerk could verify it in seconds. - Tags are for clarity, not security. They prevent honest mix-ups. They do not stop someone who writes text designed to take over the model. See Prompt injection and defences.
Go deeperChoosing delimiters
All three vendor guides recommend some kind of delimiter, and none requires one exact style.
| Delimiter | Looks like | Good for |
|---|---|---|
| XML-style tags | <rules> … </rules> | Marking exactly where a block of content starts and ends, nesting parts inside parts, attaching labels |
| Markdown headings | # Rules | Marking sections of an instruction that a person will also read and edit |
What the guides say:
- Google: XML-style tags (such as
<context>and<task>) or Markdown headings are both effective. Choose one format and use it consistently within a single prompt. - Anthropic: XML tags help the model parse complex prompts unambiguously, especially when a prompt mixes instructions, context, examples and variable inputs. Use consistent, descriptive tag names, and nest tags when the content has a natural hierarchy.
- OpenAI: Markdown headers and lists help mark distinct sections of a prompt. XML tags help show where one piece of content, such as a supporting document, begins and ends, and XML attributes can hold metadata that your instructions refer to.
Practical rules that follow from this:
- Give each kind of content its own tag, with a name that says what is inside (
<refund_rules>, not<data1>). - Always close what you open. The closing tag is what tells the model that the part has ended.
- Use the same names every time, and use those names in your instruction.
- For several documents, give each one a label. Anthropic's guide suggests a
<document>tag for each, with<source>and<document_content>inside.
Go deeperWhere things go
For a prompt with long context, the vendors agree on the main point and differ on a detail.
| Guide | What it says about order |
|---|---|
| Anthropic | Put long documents near the top, above your query, instructions and examples. Queries at the end improved response quality by up to 30 percent in its tests, especially with complex inputs from several documents |
| Supply all the context first. Place your specific instructions or questions at the very end. Bridge the two with a phrase such as "Based on the information above…" | |
| OpenAI | A developer message usually runs Identity, Instructions, Examples, Context. Context is usually best near the end of the prompt, because it may differ from request to request |
The shared idea: the specific question should come after the material it is about, not before it with pages of text in between. The difference: Anthropic's guide puts long documents above the instructions, and OpenAI's layout puts standing instructions first and changing context later. Both can be true at once in an app. Standing rules sit in the system prompt (see System prompts and roles), the documents come next, and the question for this request comes last.
Google's guide adds an honest note: the order of the content can sometimes affect the response, so try different orders and compare. Treat every ordering rule, and the 30 percent figure, as a starting point to test on your own task.
Short prompts need none of this A two-line prompt with no pasted material does not need tags or a special order. Delimiters start to pay off when a prompt holds content that someone else wrote, or more than one kind of content.
Go deeperWhat it costs
- Tags are cheap. A pair of tags adds a few tokens (small pieces of text you pay for).
- Documents are not. Every page you include is sent and counted on every request. A long document also gives the model more text to search. If you can send only the relevant pages, do that. Context windows and prompts covers the size limit.
- Order affects reuse. OpenAI's guide advises putting reusable instructions and reference material before the user's changing input, so that the shared beginning of the prompt can be cached. Tokens, cost and prompt caching explains this.
Go deeperLimits of delimiters
OWASP's guidance on prompt injection (text in the data that tries to give the model orders) lists "separate and clearly denote untrusted content" as one mitigation. The same page says it is unclear whether fool-proof methods of prevention exist. So use tags to make your prompt clear, and do not rely on them alone to keep an app safe. If the model's answer can trigger an action (a refund, an email), check that action in your own code.
Go deeperWords to know
| Word | Meaning in simple words |
|---|---|
| Context | The material you give the model to work with: documents, messages, data |
| Delimiter | A marker that shows where one part of a prompt starts and ends |
| Tag | A delimiter written in angle brackets, as an opening and closing pair, such as <rules> and </rules> |
| Order | The sequence of the parts in a prompt. The model reads from top to bottom |
| Bridge phrase | A few words that connect the material to the question, such as "Based on the rules above" |
| Source label | A name attached to a document, so an answer can say where it came from |
Check your understanding
Sources: the official documentation
This lesson is based on the official documentation. Check it for the latest details.
Something wrong or unclear in this lesson? Tell us, it gets fixed.