Sub topic 3
How long sessions go wrong: symptoms of a crowded context
This chapter assumes you have used a coding agent in a terminal or editor for tasks longer than a few minutes. You should know what a token is, and you should know that a model can only read a limited amount of text at once. You should also be comfortable with git and the command line.
By the end, you will be able to recognize the common ways a crowded context hurts an agent's work. You will also be able to tell those symptoms apart from ordinary mistakes, and run a quick check to confirm the cause.
The working model: why a crowded context hurts
The context window is the fixed amount of text the model can consider on each turn. Everything the agent knows about your task has to fit in it: instructions, file contents, command output, and the conversation so far. The agent has no other memory of the session.
A crowded context degrades work through three mechanisms.
- Dilution. Relevant facts compete with a large amount of irrelevant text. A decision you made an hour ago is still present, but it is one short passage among thousands of lines of old test logs.
- Position effects. Models tend to use information at the start and end of a long input more reliably than information in the middle. The effect varies by model and is weaker in newer ones, but it is real enough to plan around.
- Loss through compaction. When the window nears its limit, most tools summarize or drop older turns. Anything the summary leaves out is gone. Detail loss is the most common cause of sudden, confusing behavior changes in long sessions.
Degradation is gradual. There is no single threshold where quality drops. A session at 40% usage can already show symptoms if much of that 40% is noise, and a session at 80% can work well if the content is focused.
The main symptoms
The symptoms below are roughly ordered from subtle to obvious. Learn to spot the early ones, because the late ones usually mean you have already lost work.
Repeated questions and repeated work
The agent asks you something you answered earlier, or reads a file it has already read, or re-runs a search with the same arguments. The earlier answer or result is still in the window or was summarized away. Either way, the agent is not using it.
Example: you said at the start, "Use tomllib, not a third-party TOML package." Two hours later the agent adds tomli to requirements.txt.
Drifting from stated constraints
The agent follows the instructions you gave in the first few turns less and less. Common forms:
- It changes a public function signature after you said to keep it stable.
- It drops your code style (naming, error handling, test layout).
- It edits files outside the area you scoped.
Drift often looks like carelessness. The cause is that the constraint is now a small fraction of the text the model is attending to.
Contradicting earlier decisions
The agent proposes an approach you already rejected, or reverses a design decision without noting that it did. This is a stronger form of drift. It is especially common after compaction, when the summary kept the decision but dropped the reason. Without the reason, the agent has no basis for resisting its default preference.
Invented or stale details
The agent refers to a function, flag, or file path that does not exist, or uses an old version of code you have since changed. Two separate causes produce this:
- Stale content. The agent read a file early in the session, you or the agent later edited it, and the agent is working from the old copy still in context.
- Fabrication. The agent has lost the exact text and fills the gap with a plausible guess.
Stale content is easy to detect, because the wrong detail matches an earlier version of the file. Fabrication usually does not match anything.
Fixing the same bug in circles
The agent applies a fix, a test fails, it applies a different fix, and then it returns to the first fix. Each attempt is locally reasonable. The history of what was already tried is buried in long outputs, so the agent cannot see that it is looping.
Slower and more expensive turns
Each turn sends the whole context to the model again, unless a cache covers the stable part. A larger context means higher latency and, on metered plans, higher cost per turn. A session that was fast at the start and is now slow is often just big.
Truncated or lost output
Tools sometimes cut off very long output. The agent then sees only part of a log or file and may draw a wrong conclusion from it. A truncation marker in the output is a sign the agent is working with incomplete data.
A diagram of the typical progression
The following diagram shows how a session typically moves from healthy to degraded, and where each symptom tends to appear.
Note the two exits from the compacted state. Compaction is not a guaranteed reset. A good summary returns you to a focused state, and a poor one produces a session that looks clean but has lost its memory.
Telling context problems from other problems
Not every mistake is a context problem. Before you act, separate the likely causes.
| Observation | Likely cause | First action |
|---|---|---|
| The agent ignores a rule stated 10 turns ago | Crowded context, or the rule was ambiguous | Restate the rule once. If it still fails, rewrite the rule more precisely. |
| The agent makes the same mistake in a fresh session | Not a context problem | Fix the prompt, the instructions file, or the task definition |
| The agent invents an API of a library | Knowledge gap in the model | Give it the documentation or the source |
| Quality fell sharply right after compaction | Summary lost information | Compare the summary with your notes and restore what is missing |
| The agent reads a file again and again | Output was truncated or summarized away | Read only the needed lines and record the finding in a notes file |
| Turns are slow but answers are fine | Large context, no quality loss yet | Plan a compaction at the next natural break |
The most useful test is the fresh-session test. Start a new session, give it the same task and the same key constraints, and see whether the problem recurs. If it does not, the old context was the cause. If it does, the problem lies elsewhere.
A quick check you can run
You can test whether the agent still holds a key decision by asking a direct question rather than waiting for it to fail. Ask something like:
Before continuing, list the constraints and decisions you are working under for this task, in your own words. Do not make any changes.Compare the answer with what you intended. Missing or altered items tell you what was lost, and you can restate them. This costs one short turn and often saves an hour of rework.
You can also check the state of your files independently of the agent's memory:
# What has actually changed, regardless of what the agent believes
git status --short
git diff --statIf the diff shows edits to files you did not scope, or the agent describes changes the diff does not contain, the agent's picture of the session no longer matches reality.
Edge cases
- Early overload from a single large read. A single huge file or log can crowd out everything else within one turn. Symptoms appear suddenly, not gradually. The fix is to discard that turn's content if your tool allows it, or to compact and re-read only the lines needed.
- Misleading calm. An agent can write confident, well-formed answers while working from lost or wrong information. Fluent output is not evidence of correct recall. Verify against files and tests.
- Subagent and tool-result mismatch. When work is delegated, the main session sees only a returned summary. If the summary omits a caveat, the main agent proceeds without it. This looks like a contradiction but is a handoff loss.
- Instructions that fight each other. A long persistent instructions file with conflicting rules produces inconsistent behavior at any context size. Do not blame crowding until you have read your own instructions.
- Compaction timing. An automatic compaction that fires mid-task can drop the details of the step in progress. Compacting yourself at a natural break, such as after a commit, gives better results than letting it trigger at the limit.
What to do with this
Treat the symptoms as signals to act, not as failures of the agent. The first appearance of repeated questions, drift, or looping is the point to take one of the actions covered later in this tutorial: check usage, compact with guidance, write the state to a notes file, or start a fresh session. Acting at the first symptom costs a few minutes. Acting after the agent has made several inconsistent edits costs far more.
Exercise
Midway through a long session, the agent adds a third-party library you explicitly ruled out at the start. A fresh session given the same task and constraints does not make this mistake. What is the most likely cause, and what is the lowest-cost next step?
Show answer
The most likely cause is context degradation: the early constraint is diluted or was lost in a summary. The fresh-session result rules out a flaw in the task or instructions. The lowest-cost step is to ask the agent to list its current constraints, restate the missing one, and revert the unwanted dependency. If the constraint matters across sessions, also add it to the persistent instructions or a notes file.
Exercise
An agent says it updated load_settings() and shows the new code, but git diff --stat shows no change to config.py. Name two possible explanations related to context, and one way to verify each.
Show answer
One explanation is stale or fabricated state: the agent is describing an edit it never applied, or is working from an old copy of the file. To verify, read the actual lines in config.py and run the tests. A second explanation is that the edit was applied to a different file or was lost in a failed tool call whose output was truncated. To verify, run git status --short to find which files changed and re-run the edit while watching the tool output for errors.
Exercise
Why is compacting at a natural break, such as just after a commit, usually safer than waiting for automatic compaction?
Show answer
At a natural break, no step is half done, so the summary does not need to capture in-progress details. You can also check the summary against the committed state and add anything missing before work continues. Automatic compaction at the limit can fire mid-step and drop the details the agent needs next.