Why Long Chats Make Coding Agents Worse: A Practical Guide to Context Management
Coding agents often start strong and then degrade as a chat grows: they ignore constraints, rewrite the same files, and invent APIs. This guide explains three failure modes—attention dilution, context pollution, and lossy compression—and a practical workflow of short sessions, durable handoff files, and delegated heavy work.
1. The pattern: longer context, weaker agents
A consistent pattern shows up across AI coding tools—inline editor assistants, terminal agents, and cloud development environments. Early in a project the output is strong. As turns accumulate, quality drops sharply. Old defects stay open while new ones appear. A rule such as “do not touch the test directory” is ignored. The same file is rewritten again and again, and the structure gets messier than before.
This has little to do with model size or version. A context window advertised in the millions of tokens is not the same as a million tokens of reliable working memory. The window is a capacity ceiling. Accuracy at locating critical information falls as the sequence gets longer. In practice, quality often becomes noticeable around the 100,000-token range and clearly worse beyond about 200,000 tokens.
2. Causes: three failure modes that stack
1. Attention dilution (Lost in the Middle)
When key facts sit in the middle of a long history, the model is less likely to use them. It is like searching today’s news inside a stack of old newspapers: the pages are still there, but retrieval is expensive.
2. Context pollution
Failed attempts, rejected designs, and stale stack traces all remain in the history. The model cannot reliably separate “discarded conclusions” from “constraints that still apply,” so it treats an earlier wrong path as a live clue. This is the main reason chats get messier the longer they run.
3. Lossy compression
Summaries shrink the context, but they drop detail: line numbers, field names, boundary conditions, and implicit agreements stated only in conversation. The more often you compress, the more those losses accumulate. The model then works from a distorted “project memory.”
3. A symptom checklist
If you see any two of the following, rebuild the context instead of asking another follow-up:
- A constraint you already stated is violated in later output
- The same file is edited repeatedly and only grows more complex
- The agent cites functions that do not exist, or invents file names or config keys
- Answers get shorter and more superficial, and the agent stops running checks (build, tests, static analysis)
- When asked to restate the current goal, the answer no longer matches your intent
- It starts proposing drastic moves, such as “delete the whole module and rewrite it”
4. Core principles: short sessions, durable truth, delegated heavy work
Effective context management comes down to three rules.
Principle 1: Keep the main session short.
The main session should decide and accept work, not explore and trial-and-error. After roughly twenty to thirty turns, noise starts to cancel out the benefit.
Principle 2: Write important facts to files.
A file is external memory: persistent, version-controlled, and reloadable in any new session. Anything you will need again should not live only in chat history.
Principle 3: Delegate heavy work.
Large searches, cross-module refactors, and bulk test fixes belong in a sub-agent, a branch session, or a fresh chat. Send only the conclusion back to the main session.
5. A concrete workflow: fixing an intermittent 500 on login
Suppose the chat is already about thirty turns deep. You have inspected logs, changed middleware, changed the connection pool, then reverted the middleware. The agent now suggests deleting the entire auth module and rewriting it.
What not to do: stay in the same session and say “look more carefully.” Failed attempts keep piling up, and the model gets more lost.
What to do, in three steps.
Step 1: write the truth to disk (about five minutes). Keep a handoff file at the project root:
# HANDOFF.md## Current goalFix intermittent 500s on POST /api/login. Error rate is about 3%, concentrated when the connection pool is exhausted.## Confirmed facts- Logs show `PoolTimeout`, not an auth-logic error- Middleware changes have been rolled back; the working tree is clean- Repro script: `scripts/repro_login.sh`## Ruled out- Raising the pool limit (did not help; the leak remains)- Rolling back middleware (did not help)## Next stepIn `db/session.go`, check early-return branches on the defer release path.## Constraints- Do not modify the tests/ directory- Do not add new dependencies
Step 2: open a new session and load only the handoff file plus the relevant source, so the agent continues from a clean context.
Step 3: freeze reusable project rules in a rules file so you do not restate them every time:
# Project rules- Run the build and unit tests before committing- Do not change existing assertions under tests/; add new cases in a separate file- All database access goes through the repository layer; handlers must not connect directly
6. Treating context as a budget
Manage context as a limited resource, not an unlimited supply:
- Layered memory: rules files (long-lived conventions) → handoff files (current progress) → the session (temporary exploration). Each layer has one job.
- Per-session budget: set a turn cap for exploratory work. When you hit it, write state to disk and start a new session.
- Retrieve instead of paste: do not dump whole files. Point at specific symbols and line ranges to cut useless tokens.
- Verification outside the model: run builds, tests, and static checks in scripts or a pipeline so “pass or fail” is an objective signal, not the model’s own claim.
- Observability: record token growth per turn and whether the task is converging, and use that data to decide when to split context.
7. Common mistakes
- Treating capacity as capability. A large window does not mean accurate long-range retrieval.
- Using compression instead of governance. Compression is a stopgap, not a long-term plan.
- Raising the volume inside a dirty context. Stronger wording does not repair polluted memory.
- Letting external memory go stale. An outdated handoff file is more dangerous than none. Update it as the work moves.
8. Summary
Long-chat degradation is not a model defect. It is missing context management. Treat the main session as a decision desk, files as durable memory, and sub-agents as disposable exploration sandboxes. That keeps output quality stable across engineering work of any length. The test is simple: if you need “look more carefully” to recover, the thing to rebuild is the context, not the prompt.
