What to Do When Your AI Coding Agent Runs Out of Context
It rarely fails loudly. It contradicts an earlier decision, re-reads a file it already saw, forgets a stated constraint. Here is how to checkpoint and recover mid-task without losing the work already done.
What to Do When Your AI Coding Agent Runs Out of Context
An AI coding agent running out of context mid-task does not usually fail loudly, it degrades quietly: it starts contradicting a decision from earlier in the session, re-reads a file it already read as if seeing it for the first time, or forgets a constraint you stated at the start. The fix in the moment is not to keep pushing forward in the same degraded session, it is to checkpoint what has actually been done, then either compact or restart with that checkpoint as the new starting context.
The early signs, before it gets obvious
It re-reads or re-lists a file it already opened earlier in the session, as if it has no memory of doing so.
It proposes a change that contradicts a decision you both already made and moved past, not a reasonable reconsideration, but a genuine loss of that earlier context.
It asks a question you already answered, phrased almost identically to how you originally answered it.
Its responses get noticeably shorter or more generic on a long, detailed task, a sign it is compressing or dropping detail to fit.
Why this happens
Every model has a finite context window, and a long agentic session, especially one reading multiple files, running commands, and holding a multi-step plan, fills it faster than a normal conversation. Once the window is full, something has to give: older messages get truncated or summarized to make room for new ones, and whatever detail lived only in that older, now-compressed content is what starts slipping. This is not a bug in the agent, it is a hard resource limit, the same way a person cannot hold an unlimited number of details in working memory during a long, complex task.
Recovering without losing the work already done
1. Ask for a checkpoint summary before continuing
The moment you notice an early sign, stop and ask the agent to write a summary of exactly where the task stands: what has been done, what decisions were made and why, and what remains. This single step is the highest-leverage thing you can do, it converts context that may be about to degrade into a compact, durable artifact you can hand to a fresh session.
Before we continue, write a summary of this task's current state:
what has been completed, the key decisions made so far and the
reason for each, any constraints I gave you that still apply, and
exactly what remains to be done. Be specific enough that someone
with no memory of this conversation could pick up the task from
your summary alone.2. Start a fresh session seeded with that summary, rather than pushing forward
A degraded session does not reliably recover on its own, continuing to work in it risks compounding the errors that already started appearing. Starting a new session and opening it with the checkpoint summary as the first message gives the agent a clean, full context window with exactly the detail that matters, rather than a full-but-degrading one.
3. Break the next attempt into smaller, explicitly-scoped subtasks
If a task ran out of context once, the same task attempted the same way is likely to run out again. Split it into subtasks small enough to complete well within a single session's context, with an explicit handoff summary between each, the same technique as the checkpoint above, applied proactively rather than reactively.
Preventing it on the next long task
State constraints once, clearly, near the start, and repeat only the ones that matter most if the task runs long, rather than re-explaining everything periodically, which itself consumes context.
Avoid having the agent re-read large files it already has the relevant content from, point it at the specific section or ask it to reference what it already extracted instead.
For genuinely large tasks, plan the checkpoint-and-restart cycle in from the start rather than treating it as a failure recovery, some tasks are simply larger than one session's context comfortably holds.
Where this connects
This is the acute, within-task version of a problem covered more broadly in how much context to give an AI coding agent and how to keep an AI coding agent from losing context. If what you are actually seeing is the agent forgetting a correction across separate sessions rather than losing detail within one long session, that is a different mechanism, covered in why your AI coding agent repeats the same mistake. For the underlying mechanics of what a context window actually is, see what a context window is.
Frequently asked questions
How do I know if it is context loss versus the agent just being wrong?
Context loss has a specific signature: it contradicts or forgets something it correctly handled earlier in the same session, rather than getting something wrong for the first time. If it is making a genuinely new mistake rather than losing track of an earlier one, that is a different problem with a different fix.
Can I just tell it to remember everything and avoid this?
No, this is a hard resource constraint, not a matter of instruction. Telling a model to remember more does not expand its context window. The checkpoint-and-restart pattern works because it manages what has to fit into that fixed window, not because it asks the model to try harder.
Does a bigger context window solve this permanently?
It raises the ceiling, not eliminates the problem, a large enough task will still eventually exceed any context window, however big. The checkpoint habit is worth keeping even on models with very large context windows, for exactly the tasks that are large enough to matter.
Should I worry about this on short, simple tasks?
No, this specifically affects long, detail-heavy, multi-step sessions. A short task rarely approaches the context limit closely enough for this to be a real risk, save the checkpoint habit for tasks you already expect to run long.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


