Context for a support bot
9 min read
The job
A customer writes to Riverside Bikes: "Can I still return it on 25 March?" The assistant has to answer correctly, quickly and without exposing anything it should not. Before it writes a word, the application must assemble a context. That means answering five questions.
- What does the model need to know about this customer's order?
- Which policy applies, and is it current?
- May this customer see that policy?
- How much of the conversation is useful?
- What should happen if no policy applies?
Each answer maps to a Lesson in this path. The program below uses all of them.
- Detect the topicDecide whether the question is about returns, shipping or something else.
- SelectKeep policies the customer may read, that are current, and that match the topic.
- MinimiseSend only the order facts the answer needs, and leave out the rest.
- ArrangeStable instructions first, evidence next, recent turns, then the question.
- ProtectEscape every part so none of it can fake a label.
- Hand overIf no policy applies, send the case to a person.
The whole program
The data stands in for a policy store and a customer record. In a real system these come from a search and a database. Read the program from the top and match each step to the flow above.
import html
policies = [
{"id": "returns-2026", "topic": "returns", "year": 2026, "audience": "public", "text": "Unused bikes can be returned within 30 days for a refund."},
{"id": "returns-2023", "topic": "returns", "year": 2023, "audience": "public", "text": "Unused bikes can be returned within 14 days."},
{"id": "staff-refunds", "topic": "returns", "year": 2026, "audience": "staff", "text": "Staff may refund up to 500 euros without approval."},
{"id": "shipping-2026", "topic": "shipping", "year": 2026, "audience": "public", "text": "Standard shipping takes 3 to 5 business days."},
]
customer = {"order": "ORD-4821", "bought": "2 March", "email": "mia@example.test"}
turns = ["Customer: Hi.", "Assistant: Hello, how can I help?", "Customer: I want to return my bike."]
def topic_of(text):
lowered = text.lower()
for topic, keys in {"returns": ["return", "refund"], "shipping": ["ship", "deliver"]}.items():
if any(key in lowered for key in keys):
return topic
def build(question, groups):
topic = topic_of(question)
log, chosen = [], []
for policy in policies:
if policy["audience"] not in groups:
log.append(f"left out {policy['id']}: not allowed")
elif policy["year"] < 2025:
log.append(f"left out {policy['id']}: out of date")
elif policy["topic"] != topic:
log.append(f"left out {policy['id']}: other topic")
else:
chosen.append(policy)
if not chosen:
return None, log
parts = [
("instructions", "Answer only from the policy. If it does not answer, say you cannot find it."),
("order", f"order {customer['order']}, bought {customer['bought']}"),
("policy", " ".join(policy["text"] for policy in chosen)),
("conversation", " / ".join(turns[-2:])),
("question", question),
]
return "\n".join(f"<{name}>{html.escape(text)}</{name}>" for name, text in parts), log
for question in ("Can I still return it on 25 March?", "Do you sell helmets?"):
context, log = build(question, {"public"})
print("\n".join(log))
print(context if context else "no policy applies: hand over to a person")
print("---")It prints:
left out returns-2023: out of date
left out staff-refunds: not allowed
left out shipping-2026: other topic
<instructions>Answer only from the policy. If it does not answer, say you cannot find it.</instructions>
<order>order ORD-4821, bought 2 March</order>
<policy>Unused bikes can be returned within 30 days for a refund.</policy>
<conversation>Assistant: Hello, how can I help? / Customer: I want to return my bike.</conversation>
<question>Can I still return it on 25 March?</question>
---
left out returns-2026: other topic
left out returns-2023: out of date
left out staff-refunds: not allowed
left out shipping-2026: other topic
no policy applies: hand over to a person
---Read the first result. Three policies were left out, each for a stated reason: the staff rule for permission, the 2023 rule for age, and the shipping rule for topic. One policy remains. The order facts are only the order number and the purchase date, because those are what the answer needs. The customer's email address never entered the context, and no instruction could make the model reveal what it was never given.
Read the second result. A question about helmets matches no policy, so the program does not call the model at all. It hands the case to a person. That is the safer behaviour, as the Lesson Citations and honest refusals argues: a clear gap costs less than a confident guess.
The parts that make it safe
Four decisions in the program carry most of the safety.
- Permission is checked in code. The audience test runs before anything is sent, using the signed-in customer's groups. A line in the prompt asking the model to keep staff rules secret would not do the same job.
- Fields are chosen, not copied. The record has an email address, and the context has none. A smaller context leaks less.
- Everything is escaped. The customer's own words and any text from a document are passed through
html.escape, so they cannot close a label. Order and labels in context explains why. - There is a way out. A GuardrailA check in your program that holds even when the model ignores its instructions. Examples are a step limit, a cost limit and a validation of the final answer. is a check around the model that limits what it can do. Here it is a rule that no policy means no model call. It sits alongside a person who takes over, which is Human in the loopA design in which a person approves an action before the agent runs it. It is used for actions that are hard to undo, such as sending money or deleting data. design.
What to log
For every request, store the context that was built, the log of what was left out and why, and the token count. When a customer disputes an answer, you can show exactly what the assistant was given. The Testing a context change Lesson shows how to turn these logs into a question set that guards against regressions.
Where to go from here
This program is small on purpose. A real bot would search a large store, use embeddings and keep a longer history with summaries, as the Lessons Embeddings and semantic search and History and summaries describe. The structure does not change. Decide what the model needs, select it by rule, arrange it, protect it, and know when to stop.
Where is permission checked in the support bot?
A staff-only refund rule is in the policy store.
A rule the model never sees cannot be leaked. A prompt request is not a permission check.
What happens when no policy matches the question?
A customer asks whether helmets are sold.
A clear gap costs less than a confident guess.
Why was the customer's email address left out?
The record has an email, but the answer needs only the order and the date.
Send fields, not records. A detail that never enters the context cannot be repeated.
Your reading position is saved
0 of 11 complete
Lesson complete
Context for a support bot
Up next