CCDV-F · Prompt and Context Engineering
Context drift and bloat: prune, compact and isolate
A migration agent follows every rule for half an hour. After an hour it starts editing files under vendor/ and skipping the tests. The traces show that almost all of the context is full file reads and test logs from old steps that nobody looked at again. Restating the rules in capital letters changed nothing. The problem isn’t the rules: it’s what surrounds them.
Drift and bloat
- Context bloat: the window fills up with material that’s no longer useful (old tool results, full history, documents already processed).
- Context drift: the consequence. With so much material on top, the instructions from the beginning carry less weight and behavior strays from them. Response quality drops as the conversation grows, well before reaching the window’s limit.
The way out is to keep the active context small, with what the task needs right now. There are three techniques, from finest to coarsest.
1. Prune at the source and clear old results
- At the source: each tool returns only what’s useful. The fields that matter and not the whole record, one page of results and not a thousand, the test summary and the failures, not the full log.
- Tool result clearing (context editing): the API can automatically replace the oldest results with a placeholder once the prompt passes a threshold, keeping the most recent ones and leaving out the tools you choose. Your history stays intact on the client side.
This is the right fit when what bloats the context is reads and logs that won’t be used again.
2. Compact what must be remembered
Compaction replaces the old turns with a summary and leaves the system prompt and, if you want, the latest turns intact. The API offers it on demand or when a token threshold is reached, and you can give it your own summarization prompt so it doesn’t lose what matters.
This is the right fit when the conversation holds decisions that can’t be lost: a travel chat of hundreds of turns has to remember dates, budget and destinations. Dropping the oldest messages once a threshold is hit (a sliding window) deletes exactly those decisions.
3. Isolate with subagents
Context isolation moves a subtask into a subagent with its own window. The subagent reads the 40 contracts and returns only the risk clauses; the main agent writes the memo with those findings, without the raw text on top. The typical clue: small cases come out fine and large ones don’t. The problem is volume, not the model’s capability.
For data that has to survive everything before it (decisions, progress on a long task), it’s worth storing it outside, in note files or with the memory tool, and rereading it when needed.
What doesn’t solve the problem
| Idea | Why it falls short |
|---|---|
| Prompt caching | Lowers cost and latency; the tokens still take up the window |
| Model with a larger window | Postpones the limit, but the drift remains |
| Repeating the rules every turn | Adds more text to a context that’s already bloated |
| Always passing the full history | It’s the cause, not the solution |
The usual traps
- Repeating the rules in capital letters or in every user message.
- Raising
max_tokensor the effort level so it “reasons more about the rules”. - Cutting the oldest messages when decisions must be kept.
- Pasting all the input documents into one giant message.
- Expecting the cache to make room in the window.
On the exam
Look at what fills the context. If it’s old tool results, prune or clear them. If it’s turns with decisions that must be remembered, compact while preserving that data and the system prompt. If a subtask brings in a lot of raw material that isn’t used afterward, delegate it to a subagent that returns a summary.
Quick question
A practice exam question on this domain. It counts towards your progress.
A logistics company extracts shipment data from carrier emails with the Claude API and gets back a JSON object with tracking number, carrier and estimated delivery date. A Python service parses that JSON into a strict data class and writes it to the warehouse database.
Last week the service crashed several times. Logs show the model's JSON was well formed, but some responses included an extra field, notes, with remarks about the email. The strict data class rejects unknown keys, so the whole batch failed.
The team is small and wants a fix that keeps bad records out of the database without stopping the pipeline.
Which change best fits the requirement?