Context Windows: Why Coding Agents Forget and What to Do About It
A context window is the amount of text a model can consider at once. Everything the agent has read, written and been told in a session must fit inside it. When a session gets long, older material is trimmed or summarised, and the agent may forget an instruction, repeat a mistake or contradict an earlier decision. Knowing this explains many "it was working fine and then got worse" experiences.
What fills the window
The window is shared by several things:
- The tool's own system instructions.
- Your project instruction file.
- The descriptions of every connected tool.
- Your messages and the model's replies.
- Every tool result: file contents, search output, test logs.
The last item is usually the biggest. Reading five large files or printing a 3,000-line test log can consume more space than the rest of the conversation combined. Window sizes are measured in tokens, roughly word fragments, and differ between models. Check your tool's documentation for the current number rather than relying on a figure from an article.
Symptoms of a crowded context
- The agent ignores a rule from your instruction file that it followed earlier.
- It re-reads files it already read.
- It reintroduces a bug you already fixed.
- It forgets the goal of the task and starts on side issues.
- Responses get slower and costlier.
What tools do about it
Most agents try to manage the window for you. They may drop old tool outputs, or summarise the earlier conversation into a shorter note and continue from that. Claude Code, for example, has a /compact command that summarises the session, and a /clear command that starts over with an empty conversation. Other tools have equivalents. Summaries are lossy, so exact details like a precise error message may disappear.
Habits that keep the context healthy
1. One task per session
When a task is done and committed, start fresh. A clean start costs one prompt and gives the agent full attention for the next job.
2. Point instead of paste
Say "see the validation in src/schemas/user.ts" and let the agent read what it needs, rather than pasting whole files into the prompt. Targeted reads of the relevant part of a file beat reading everything.
3. Trim noisy output
Tell the agent how to run tests quietly, or make your commands print only failures:
# Prints only failing tests and a summary line
pytest -q --tb=short -x
# Search with a limit instead of dumping everything
grep -rn "PaymentError" src | head -20
4. Write down state outside the chat
For longer jobs, have the agent keep a small notes file such as PROGRESS.md that lists what is done, what remains and key decisions. A new session can read it in seconds, so no knowledge depends on the conversation surviving.
# Migration progress
Goal: move all date handling from moment to date-fns.
## Done
- src/utils/dates.ts
- src/billing/*
## Remaining
- src/reports/* (uses moment().fromNow, check replacement)
## Decisions
- Keep UTC everywhere; format only at the UI edge.
5. Keep the instruction file short
It is loaded every time. Every unneeded sentence displaces useful content.
6. Disable tools you do not use
Each connected tool adds its description to the window. A dozen unused integrations waste space.
When to start over
- The agent has repeated the same mistake twice after correction.
- You have changed the plan significantly.
- The session covers more than one unrelated task.
- Tool outputs have been huge.
Before you reset, ask for a short summary of decisions and open items, save it to a file or copy it, then begin again with that summary as your first message.
Summary
The context window is a budget. Spend it on code and instructions that matter, keep tasks short, and store long-lived knowledge in files. Later in this course we return to long sessions in more detail.
Comments
No comments yet. Have a question or a tip? Be the first to share it.