Your AI agent has spent an hour migrating a codebase to a new database driver. Early on, it skimmed the driver docs and wrote itself a note: the new driver retries failed connections, so the old retry code can go. The note is wrong. The agent trusts it anyway and deletes working retry code from every file it touches.
Context engineering is deciding what goes into an agent's context window at each step. Bigger windows don't fix this. Frontier models get less reliable as their window fills, a problem known as context rot. The fix is counter-intuitive: you make the agent better by giving it less.
A model doesn't look things up in its context the way a database looks up a row. Every token competes for a fixed amount of attention. Chroma's context rot research tested 18 frontier models, and every one degraded as input grew.
Most of the window isn't your instructions. It's what the agent piles up along the way:
AGENTS.md, loaded on every runThis applies whether the agent is Claude Code refactoring a repo, a Copilot Studio agent answering staff questions, or an n8n workflow processing invoices.
Drew Breunig named four failure modes. Each has a fix, and every fix works by throwing something away.
| Failure mode | What happens | Fix |
|---|---|---|
| Poisoning | A wrong conclusion gets written down and treated as fact | Validate before it's saved |
| Distraction | The agent's own history outweighs its training, so it repeats old moves | Compact and move detail to files |
| Confusion | Too many tools or irrelevant docs, so it picks the wrong one | Curate to what the task needs |
| Clash | Old and new versions of a fact both sit in the window | Prune the superseded version |
Make the agent check a conclusion against something real before it writes it down. One connection test would have killed the retry note. For code, builds, tests and linters do this automatically. See using back pressure to keep AI agents on track.
For non-code agents, verify against the source system, not against the agent's own summary.
Warning: An agent treats its own notes as ground truth. A wrong note doesn't fade. It gets repeated, built on and acted on in every turn after it lands.
After an hour of work, the agent's own history becomes the strongest pattern in the window. It starts repeating what it already did instead of asking what the job needs next.
Compact or clear the history. Clearing your AI agent's context between tasks covers when to use each. Compaction is lossy, and a summary of a summary drifts like a photocopy of a photocopy. Keep the detail in files instead, such as a plan or a decisions log, and load them only when needed. See the types of AI agent memory.
Every tool definition sits in the window whether the agent uses it or not. In one benchmark, a small, compressed model failed a task when offered 46 tools and passed the same task with 19.
Give the agent only the tools the job needs. A database migration doesn't need your ticketing system or calendar. Load extras on demand with skills, subagents and MCPs rather than wiring everything in up front. In Copilot Studio or n8n, switch off actions and connectors the agent doesn't use.
Eventually the agent notices retries aren't happening and records that the note was wrong. Now both versions sit in the window, and the model has to guess which one wins.
When information is superseded, delete the old version. If you can't, start a fresh session that restates only the current, corrected state of the job.
Tip: If an agent starts looping or contradicting itself, don't argue with it in the same session. Every correction adds another conflicting message. Start fresh with the corrected state instead.
A support agent is connected to 40 tools across CRM, email, SharePoint and the ticketing system. It runs all day in one session. Early on, it wrongly concludes a customer is on the Enterprise plan, writes that into its notes, and quotes Enterprise SLAs for the rest of the day. Corrections pile up next to the original mistake.
❌ Figure: Bad example - Unverified notes, every tool loaded, one endless session
The same agent gets the 5 tools support work needs. It checks the plan in the CRM before recording it. When a fact changes, it updates the note instead of appending a correction, and each customer conversation starts from a clean window.
✅ Figure: Good example - Validated notes, curated tools, pruned facts and a fresh window per conversation