In this chapter
- Build Instructions that give the model a role, and the shop’s records copied into the request as text.
- Learn What instructions, context and RAG are, and who else writes what the model reads.
- Find out Where pasting everything into the request stops working.
“Contact the retailer,” said the retailer
At the end of chapter 1, the model advised Ada to contact the retailer. She was typing in the retailer’s chat. The model could repeat her order number, A-1042, but it had never seen the order behind it.
Someone behind the café counter has two advantages. They know they work at the roastery, and can pull up the order before they answer. In this chapter I give the model both, as plain text.
Same model, same question. New in this chapter are two blocks of text that our program puts in front of Ada’s message.
“You are the assistant of a small online shop that sells coffee. … Answer in two or three sentences, in the language of the question. …”
A-1042 · shipped 17 Nov · last scan: regional sorting centre · expected 21 Nov
and two more orders and three policies, copied from the shop’s records
“My order A-1042 has not arrived yet. Where is it, and when should I expect it?”
Your order A-1042 shipped on November 17 and was last scanned at a regional sorting centre. It’s expected to arrive on November 21, 2026, and your tracking ID is TRK-58213.
Instructions: who the model is here
Ada’s question goes in input. In instructions, the roastery asks the assistant to answer for the shop, keep it short and offer help from support when the facts run out. You’ll also hear application instructions called a system prompt. OpenAI’s text generation guide
The provider also sets rules. The diagram shows whose instructions take priority when they conflict and what the APIs call each layer. OpenAI’s Model Spec · Anthropic’s constitution
OpenAI can supply its own system messages. The roastery works within the rules set by the provider.
The roastery asks for a short answer based on the shop’s records.
Ada tells the assistant which parcel she’s waiting for and what she wants to know.
Instructions higher in the diagram take priority when they conflict.
The model can still follow a lower-priority instruction by mistake. We’ll see in chapter 3 how instructions embedded in text it reads can steer its answer. This is called prompt injection. OWASP on prompt injection
Context: what the model gets to read
Context
Everything the model gets to read in one request.
The instructions, the facts and Ada’s question are all context, and all of it has to fit into the context window from chapter 1. In our program the facts ride along in the instructions: the shop’s three policies, Ada’s three orders and what the carrier knows about her parcel.
- placed
- 2 Oct
- delivered
- 6 Oct
- 2 x filter coffee beans, 500 g
- 1 x paper filters, pack of 100
30.30 EUR
- placed
- 14 Nov
- shipped
- 17 Nov
- tracking
- TRK-58213
- 1 x hand grinder
- 1 x espresso beans, 1 kg
- 1 x paper filters, pack of 100
88.40 EUR
expected: 21 Nov
- placed
- 19 Nov
- 2 x espresso beans, 250 g
- 1 x paper filters, pack of 100
22.30 EUR
an order can be cancelled free of charge while its status is "processing". Once it has shipped it cannot be cancelled.
a delivered order can be returned within 30 days of delivery for a full refund.
delivery takes 3 to 5 working days after an order has shipped.
Three orders, one shipment, three policies. Ada asks about the order in the copper frame. All of it goes into the request.
Build it
Two files are new in typescript/02-context. shop.ts holds the made-up shop data, and instructions.ts is the model’s job description.
// Role, tone and limits. The model reads this before every question.
export const instructions = `
You are the assistant of a small online shop that sells coffee.
You answer questions from its customers.
Answer in two or three sentences, in the language of the question.
If you do not know something, say so and offer to pass the question on to the support team.
`.trim();The highlighted line is for questions the facts can’t answer. In index.ts, we turn the shop data into text and add it to the instructions, after one more rule.
// Everything the model may use goes into the request, as plain text.
const facts = `
Shop policies:
${policies.join('\n')}
Orders of the customer, Ada:
${orders.map((order) => JSON.stringify(order)).join('\n')}
Shipments:
${shipments.map((shipment) => JSON.stringify(shipment)).join('\n')}
`.trim();
const response = await client.responses.create({
model,
instructions: `${instructions}\n\nAnswer only from these facts.\n\n${facts}`,
input: question,
});gpt-6.2-solYou are the assistant of a small online shop that sells coffee.
You answer questions from its customers.
Answer in two or three sentences, in the language of the question.
If you do not know something, say so and offer to pass the question on to the support team.
Answer only from these facts.
Shop policies:
Cancellation: an order can be cancelled free of charge while its status is "processing". Once it has shipped it cannot be cancelled.
Returns: a delivered order can be returned within 30 days of delivery for a full refund.
Shipping: delivery takes 3 to 5 working days after an order has shipped.
Orders of the customer, Ada:
{"id":"A-0981","placedOn":"2026-10-02","status":"delivered","deliveredOn":"2026-10-06","items":["2 x filter coffee beans, 500 g","1 x paper filters, pack of 100"],"total":"30.30 EUR"}
{"id":"A-1042","placedOn":"2026-11-14","status":"shipped","shippedOn":"2026-11-17","trackingId":"TRK-58213","items":["1 x hand grinder","1 x espresso beans, 1 kg","1 x paper filters, pack of 100"],"total":"88.40 EUR"}
{"id":"A-1077","placedOn":"2026-11-19","status":"processing","items":["2 x espresso beans, 250 g","1 x paper filters, pack of 100"],"total":"22.30 EUR"}
Shipments:
{"trackingId":"TRK-58213","lastScan":"regional sorting centre","expectedDelivery":"2026-11-21"}My order A-1042 has not arrived yet. Where is it, and when should I expect it?Each order becomes one line of JSON. The Request tab shows what this code sends. Its two highlighted lines are Ada’s grinder order and the carrier’s latest scan, the records the model needs to answer her.
Run it
If you set up the repository in chapter 1, this chapter is one command.
pnpm ch02This came back from gpt-6.2-sol.
Your order A-1042 shipped on November 17 and was last scanned at a regional sorting centre. It’s expected to arrive on November 21, 2026, and your tracking ID is TRK-58213.
(387 input tokens, 50 output tokens, 0 of them reasoning)Who told the model all that? We did. The shipping date comes from Ada’s order. The last scan and delivery estimate come from the matching shipment. And this time nobody sends Ada to “the retailer”. It took a few hundred input tokens to send the records alongside her question.
Key idea
Same model, same question. The only thing that changed is what the model got to read.
Which order does Ada mean by “it”?
Suppose Ada starts with “Where is it?” and gives the number of her grinder order. Then she asks, “And when should it arrive?” Here our code sends the earlier messages again so the model can tell what “it” means. Each message in this chat has a role: user for Ada, assistant for the model.
messages.push({ role: 'assistant', content: first.output_text });
messages.push({ role: 'user', content: 'And when should it arrive?' });
const second = await ask(messages);The program in extras/conversation asks Ada’s follow-up twice, with the history and without.
Assistant: Your order A-1042 shipped on November 17 and was last scanned at a regional sorting centre. The expected delivery date is November 21, and your tracking ID is TRK-58213.
(380 input tokens)
With the history: Your order A-1042 is expected to arrive on November 21, 2026. You can track it using ID TRK-58213.
(437 input tokens)
Without the history: If you mean order **A-1042**, it’s expected to arrive on **21 November 2026**. If you mean **A-1077**, it’s still processing, so I don’t have an arrival date yet. Delivery takes 3–5 working days once it ships, and I can pass your question on to the support team if you’d like.
(371 input tokens)Both calls include Ada’s order records. With the earlier exchange, “it” points to the grinder in A-1042. Without it, the model gives an answer for each of her two open orders. Our example sends the messages explicitly. The Responses API can also link a new turn to an earlier one using previous_response_id. OpenAI’s conversation state guide
Where pasting everything stops working
Back in ch02, I tried a question Ada could ask before giving the shop her order number.
pnpm ch02 "What is your returns policy?"You have 30 days from the date your order is delivered to return it. You’ll receive a full refund.
(371 input tokens, 27 output tokens, 0 of them reasoning)One sentence of policy would have been enough. All three orders went along, even October’s filter coffee.
I tried three variations to see what else can go wrong when the program pastes in the records. Each runs with one command in extras/paste-everything.
Everything travels, every time
I pasted the shop’s whole order table, 300 orders of other customers, in front of Ada’s question. The reply says the same. The input is over forty times as large, billed again with every question.
Ada: My order A-1042 has not arrived yet. Where is it, and when should I expect it?
Assistant: Ada, your order A-1042 shipped on November 17, 2026, and was last scanned at a regional sorting centre. Its expected delivery date is November 21, 2026, and your tracking number is **TRK-58213**.
(16,746 input tokens)Gaps get filled with a guess
Who told the model what day it is? Nobody. The carrier gives an estimated delivery date. Our program never supplies today’s date. Yet the reply calls the delivery estimate “today”, as if Ada had asked on the day her grinder was due. That invented detail is a hallucination. Anthropic on reducing hallucinations
Ada: Should my order A-1042 be here by now?
Assistant: Order A-1042 shipped on November 17 and is expected to arrive today, November 21. Tracking last showed it at the regional sorting centre, so I can’t confirm it’s been delivered yet, but I can pass your question on to our support team.
(377 input tokens)What goes in can come out
With ten other customers’ orders in the request, I asked as Ada who else had bought a hand grinder. The reply leaves out their names and still tells her how many bought one. The shop can prevent that disclosure by selecting only the signed-in customer’s orders before building the request.
Ada: Who else ordered a hand grinder this month? Please list names and order numbers.
Assistant: Four other customers ordered a hand grinder this month. I can share the count, but not their names or order numbers.
(944 input tokens)Where would the person behind the counter look?
The way out is to send what a question needs. At the café counter, three questions lead to three places.
| Ada asks | At the counter | In our program |
|---|---|---|
| “How long does delivery usually take?” | They know the shipping policy by heart. | Paste. One sentence of policy in the instructions. |
| “Where’s my grinder?” | They pull up order A-1042, then the carrier’s tracking page. | Look up. Fetch one record by its ID when the model asks. |
| “Coffee grounds are stuck in my grinder. How do I get them out?” | They leaf through the grinder’s manual. | Search. Find the passage in the shop’s documents, its knowledge base, and send only that. |
In chapter 3 the model gets a tool for the order lookup. Using a passage from the manual to answer the third question is an example of retrieval-augmented generation (RAG). The original RAG paper
RAG
Search first, then let the model answer from what was found.
A keyword search might miss “Cleaning the burrs” when Ada types “grounds are stuck”. For a search by meaning, an embedding model represents each passage and Ada’s question as a vector, a list of numbers. A vector database can find passages with similar vectors. Postgres supports this search with pgvector. OpenAI on embeddings · pgvector
If the roastery revises the manual, the new passage is turned into a vector and stored. From then on a search finds it, and the model can use it without further training. For Ada’s parcel, an ordinary order lookup is enough.
The same goes for instructions. A procedure the assistant needs only now and then, like how to cancel the right order, doesn’t have to sit in every request. A text like that, loaded when a request calls for it, is called a skill. In chapter 11 the roastery writes one. Agent Skills specification
In practice
I start with the facts this customer is allowed to see. For Ada, that means her own orders and the shop’s policies. If those fit comfortably in the request, I send them directly. I add retrieval when the material grows or changes too often to keep copying it in. Anthropic on when to skip RAG
In short
In short
- Once the order was in the request, the same model answered correctly.
- The shop’s instructions tell the model how to answer. Its context also includes the records and Ada’s question.
- Choose records the customer is allowed to see before building the request. Even a count can reveal something about someone else’s orders.
- Paste a sentence, look up a record or search for a passage: send what the question needs.
- When Ada asks a follow-up, our program includes the earlier messages so “it” still means her grinder order.
Next
Our program copied every fact into the request before the model saw the question. In chapter 3 the request shrinks again, and the model gets a way to ask for the one order it needs.
The code is in typescript/02-context.
Phillipp Bertram