The agent starts sharp and slowly goes dull in a long session. It forgets what you decided an hour ago, contradicts itself, edits the wrong file.
The cause is almost never the model. It is what you have let pile up in front of it. This chapter teaches the one skill that most separates people who get real work from AI: managing what the agent sees.
4.4.1The context window is finite
The context window is the fixed amount of text the agent can hold at once. Your files, your messages, and its own replies all count against one budget. When it fills, the oldest details fall out or the whole thing goes fuzzy.
More is not better. A window stuffed with irrelevant history produces worse answers, not richer ones. Managing that budget on purpose is called context engineering, and it is what this whole chapter is about. Martin Fowler has a good primer, Context Engineering for Coding Agents.
The always-loaded memory file you set up earlier is one small, permanent slice of that budget. Everything else is what you hand the agent per task.
4.4.2Feed only what's relevant
Hand the agent the two files this task touches, not the whole codebase. Give it the one error message, not the last hour of logs. The narrower the input, the sharper the output.
Rule of thumb: if you would not put a document in front of a new hire to solve this exact task, do not paste it to the agent either.
4.4.3A bloated session costs real money
There is a second reason to keep the window lean, and it is money. Models count text in tokens, chunks of roughly four characters. Every token the agent reads or writes is metered: you pay per token on an API key, or burn against the usage cap on a flat plan.
So a window stuffed with your whole repo and yesterday's chat is not just a duller answer. It is a bigger bill and a cap you hit hours sooner.
The fix is the one you already have. Lean input, a reset when the thread drifts, a summary before you continue: each of those buys you sharper answers and a smaller bill at the same time. You are not learning a new discipline for cost, you are getting paid twice for the one you already keep.
4.4.4Point to the spec, don't repeat it
Your agent can open files on its own. So do not paste a 500-line spec into the chat. Tell it where the file lives and which part to read: "the spec is in docs/spec.md, read the billing section."
This keeps the window lean. It also means the agent reads the current spec every time, instead of a copy that went stale the moment you pasted it. The same holds for the requirements and data model you wrote in the planning part: point, do not repaste.
4.4.5Reset when the thread drifts
When a session has wandered through three failed approaches, its window is now full of dead ends the agent keeps tripping over. It will keep re-suggesting the thing that already did not work.
Start a fresh session. A clean window with a tight prompt almost always beats fighting a long one that is carrying every mistake it made. This is hygiene, not failure.
4.4.6When a fresh window does not help, change the model
A reset fixes most of it. When it does not, when the agent fails the same task again in a clean window, change the model before you rewrite the prompt a fourth time.
Models are not interchangeable. A cheap fast one is right for boilerplate and repetitive edits, and the strong reasoning one earns its price on architecture and the bugs you cannot see. Most agents let you switch in their settings, mid-project.
4.4.7Summarize before continuing
When a long task is going well but the window is filling, do not just reset and lose the thread. Ask the agent to write a short summary first, then start fresh and hand that summary back to it.
Some tools do this for you: Claude Code calls it compaction. This prompt produces a clean hand-off you can carry into a new session:
Do this now: next time a session runs long, stop, ask the agent for that summary, and continue in a fresh window instead of pushing through the bloated one.