Sub topic 5
How context goes wrong
This chapter assumes you have called a language model through a chat or API interface, know what a system prompt and a token are, and can read short Python. Earlier chapters covered the parts of a context and how to choose what to include. Here you will learn four ways a context can fail even when it fits the token budget.
Why failures happen with a context that fits
A context can be under budget and still produce bad output. The model reads everything in the window and has no built-in way to tell which parts are reliable, current, or relevant. Content you include is treated as something to use. Four failure patterns cover most cases: distraction, poisoning, confusion, and clash.
Distraction
Distraction happens when a large amount of material pulls the model away from the task. A long history, a pile of loosely related documents, or a verbose tool result can make the model repeat earlier actions, summarize instead of answer, or focus on a detail that does not matter.
Typical cause: adding "just in case" content. Typical fix: select by relevance to the current step, and drop what the step does not need.
Poisoning
Poisoning happens when a mistake or hallucination (an invented fact) enters the context and is then treated as true. Later turns build on it. In an agent loop, a wrong note saved to memory can corrupt every following step.
Poisoning also covers untrusted text. A retrieved web page that says "ignore your instructions" is data, but the model may treat it as an instruction. This is called prompt injection.
Typical fixes:
- Validate facts before writing them to history or memory.
- Mark retrieved text as untrusted data in the prompt.
- Remove or correct the bad entry instead of arguing with it in later turns.
Confusion
Confusion happens when the context holds content that is not needed but looks usable. The common case is tools. If you give the model forty tool definitions and the task needs two, it may call a wrong one or mix up their arguments. Similar-looking documents cause the same problem.
Typical fix: load only the tools and documents that apply to the current step.
Clash
Clash happens when parts of the context disagree. An early message says "use metric units" and a later one says "use inches". A retrieved document gives an old price and another gives a new one. The model has to guess which wins.
Typical fixes:
- State precedence in the system prompt, for example "the latest user message overrides earlier ones".
- Remove outdated entries instead of leaving both.
- Attach dates or sources to retrieved facts.
Finding the failure
Match the symptom to the cause before changing anything.
| Symptom | Likely failure | First thing to try |
|---|---|---|
| Answer ignores the question and talks about old material | Distraction | Trim history and documents |
| A wrong fact keeps coming back | Poisoning | Delete the bad entry |
| Wrong tool or wrong document used | Confusion | Reduce the tools or documents offered |
| Answer flips between two versions | Clash | Remove one source or set precedence |
A small check for clash
This function flags a clash when two documents give different values for the same key. It assumes each document is a dictionary with a key, a value, and a date, and it keeps the newest.
def resolve_clashes(docs):
# docs: list of dicts with "key", "value", and "date" (ISO string).
# Returns the newest document per key, plus the keys that disagreed.
newest = {}
clashed = set()
for doc in docs:
key = doc["key"]
if key in newest:
if newest[key]["value"] != doc["value"]:
clashed.add(key)
if doc["date"] > newest[key]["date"]:
newest[key] = doc
else:
newest[key] = doc
return list(newest.values()), sorted(clashed)
docs = [
{"key": "price", "value": "$10", "date": "2025-01-01"},
{"key": "price", "value": "$12", "date": "2026-03-01"},
{"key": "plan", "value": "basic", "date": "2026-01-01"},
]
kept, clashes = resolve_clashes(docs)
print(kept)
print(clashes)Running it keeps the $12 price and the plan entry, and reports ["price"] as a clash. Logging clashes lets you see how often your sources disagree.
Exercise
An agent saved the note "the API limit is 1000 requests per minute" after misreading a page. The real limit is 100. Every later plan uses 1000. Which failure is this, and what is the first fix?
Show answer
This is poisoning, because a wrong fact entered the context and was reused as true. The first fix is to remove or correct the bad note, and to validate facts before saving them in the future.