Sub topic 2
How Context Goes Wrong
Assume you know how to call a language model, write prompts, and think in tokens. This chapter focuses on failures caused by the information included in a model call.
Common context failures
A model can produce a poor answer even when its instructions are correct. The problem may be the surrounding context.
- Distraction: irrelevant documents use attention and hide the useful evidence.
- Poisoning: incorrect or untrusted content causes the model to repeat false claims.
- Confusion: similar facts from different sources are present without clear labels.
- Clash: two instructions conflict, so the model follows the wrong one or behaves inconsistently.
| Failure | Typical cause | Useful response |
|---|---|---|
| Distraction | Too much unrelated history | Remove or summarize it |
| Poisoning | Unverified retrieved text | Check sources and mark trust |
| Clash | Conflicting instructions | Define priority and ownership |
Where to put things
Order context so that stable rules and essential constraints are easy to find. Keep the current task and its acceptance criteria near the question. Label each section clearly.
A practical order is:
- System rules and safety constraints.
- Tool descriptions and usage limits.
- Relevant reference material.
- A short summary of older history.
- Recent messages and the current question.
Do not repeat the same rule in several places. Repetition can create apparent clashes when one copy changes.
Retrieve data just in time
Retrieval means selecting external information for a particular question instead of placing an entire database in every context. Load documents when the task needs them, then remove them when they no longer matter.
def choose_documents(question, documents, limit=2):
# Replace this simple score with a search or embedding system.
terms = set(question.lower().split())
ranked = sorted(
documents,
key=lambda doc: len(terms & set(doc.lower().split())),
reverse=True,
)
return ranked[:limit]
docs = ["Refunds require an order number.", "The office closes at five."]
print(choose_documents("What is needed for a refund?", docs))Tool definitions and results
Tool definitions consume context before a tool runs. Describe only the tools and parameters the current step needs. Tool results also consume tokens, so return focused fields rather than complete records. Validate results before treating them as evidence.
Exercise
A model receives 40 pages of unrelated history and one relevant policy. Which change should you try first?
Show answer
Trim the history and retrieve only the relevant policy, while preserving the current question and governing instructions.