The agent forgot the plan right after compacting
Claude Code Last checked 2026-10-15
Compaction replaced the conversation with a summary and your plan did not survive it. The agent is not ignoring you — that text is gone from its context. Keep the plan in a file on disk, not in chat, so it survives the next compaction.
What you saw
Immediately after a Compacting conversation… / Context low · auto-compact event, the agent re-reads files it already read, re-asks questions you already answered, or starts re-implementing something it finished twenty minutes ago.
Rules you agreed earlier quietly stop being followed: "don't touch the migration files", "use pnpm not npm", "TypeScript strict, no any". Nothing errors; the behaviour just reverts to defaults.
The agent contradicts its own earlier decision and sounds confident about the new one.
A long task finishes half-done, and the final summary describes work it never did — the summary was written from the compacted summary, not from the work.
What it actually means
Auto-compact is not memory. When the context window fills, the client asks the model to summarise the conversation so far, then discards the original turns and continues from the summary. Everything that did not make the summary is gone — irrecoverably, from the model's point of view.
Summaries are optimised for narrative continuity, not for constraints. "We refactored the auth module" survives; "never edit schema.prisma directly, generate it" usually does not. Negative constraints and one-off exceptions are the first casualties, because they read as incidental detail.
This is why the post-compact agent often re-reads files: the summary says *what* was decided but not *what the file contained*, so the only way for it to act is to go and look again. That re-reading immediately eats the context you just freed, which is why a session can compact twice in quick succession and feel like it is going backwards.
It is also distinct from being stuck in a loop. A loop repeats because an action keeps failing; post-compact amnesia repeats because the record of success was deleted. The visible symptom is similar; the fix is not.
The fix
Stop the session and write the plan to a file before doing anything else. A file on disk survives every compaction; chat does not. Keep it short enough that re-reading it is cheap.
# PLAN.md
## Goal
Move auth from session cookies to JWT.
## Constraints (do not violate)
- Do not edit prisma/schema.prisma by hand; change the model then run `pnpm prisma generate`.
- pnpm only. Never npm.
- No `any`. Strict TS stays on.
## Steps
- [x] 1. Add jwt helper in src/lib/jwt.ts
- [x] 2. Issue token in src/routes/login.ts
- [ ] 3. Verify middleware in src/middleware/auth.ts
- [ ] 4. Delete session store
## Decisions already made
- 15-minute access token, 7-day refresh. Settled, do not revisit.
Tell the agent the file is the source of truth, and have it tick the checklist as it goes. The checklist is what makes recovery automatic rather than a conversation.
Read PLAN.md. It is the source of truth, not this chat.
After each step: update the checkbox and append anything you learned
under "Decisions already made". Re-read PLAN.md after any compaction.
Put the permanent rules in the project's own rules file, which is re-injected on every turn and is immune to compaction. Keep it short — these tokens are spent on every single request.
# CLAUDE.md (Claude Code — also .cursor/rules/*.mdc for Cursor,
# AGENTS.md for Codex and several others)
- Package manager: pnpm. Never npm or yarn.
- Never hand-edit prisma/schema.prisma.
- Tests: `pnpm test -- --run`. Always run before saying done.
- Before starting any multi-step task, read PLAN.md.
Compact deliberately instead of waiting for auto-compact, and say what to keep. A manual /compact with instructions produces a far better summary than the automatic one, because the automatic one does not know what matters to you.
/compact Keep: the PLAN.md checklist state, the three constraints,
the file paths we have already changed, and the decision about token
lifetimes. Drop: all file contents and all command output.
Recover a session that has already lost the plan — do not argue with it. Give it the file and an explicit restatement; re-litigating in chat just burns more context.
Stop. Read PLAN.md and CLAUDE.md, then tell me in three lines
which step we are on and what you are about to do.
Do not change any files until I confirm.
Watch the remaining context and break work at a natural seam before the client breaks it for you. In Claude Code, /context shows the breakdown; the status line shows the percentage left.
/context
# then, at a clean stopping point:
/compact
# or, for a genuinely new task:
/clear
For long jobs, hand each step to a sub-agent or a fresh session with a narrow brief. The parent keeps the plan; the child spends the context. This is the only approach that scales past a few hours of work.
How to stop it happening again
Treat chat as scratch space and files as memory. Anything you would be upset to lose — a plan, a constraint, a decision, an API shape — belongs in a tracked file in the repo within a minute of agreeing it.
Keep a PLAN.md with checkboxes for any task longer than about three steps, and make updating it part of the instruction rather than something you ask for later.
Keep CLAUDE.md / AGENTS.md / .cursor/rules short and absolute. Long rules files get partially ignored; five hard rules get followed.
Commit after every green step. Git is the one record neither compaction nor a crashed session can rewrite, and git log is a plan the agent can reconstruct from.
Start a new session per task rather than running one all day. A fresh window with a good brief beats a compacted window with a long history, every time.
Turn off auto-accept for the first few turns after any compaction. That is the window where a confused agent does the most damage.
Related questions
Can I recover what was compacted away?
Not from the model — those turns are gone from its context. But the transcript is usually still on disk: Claude Code keeps session JSONL files under ~/.claude/projects/<escaped-project-path>/, and claude --resume lists past sessions. You can read the old text yourself and paste back the part that matters.
Can I just turn auto-compact off?
In Claude Code you can (/config, or the autoCompact setting), but then you hit a hard context-limit error instead and the session stops dead. Off is only better if you are disciplined about /clear and keep state in files. For most people, keeping it on and writing plans to disk is the safer combination.
Why does it re-read the same files over and over after compacting?
Because the summary records the decision but not the file contents, so the only way to act is to look again. Each re-read refills the context and brings the next compaction closer. A PLAN.md that records what is in those files — the function signature, the chosen shape — breaks that cycle.
Is this the same as the agent being stuck in a loop?
No. A loop repeats a failing action; this repeats a finished one because the record of finishing was deleted. If the agent keeps retrying something that errors, that is a different problem — see the loop page.
Does a bigger context window fix it?
It postpones it. A 1M-token window compacts later, not never, and long contexts degrade in their own way — material in the middle gets weighted less than material at the ends. Files on disk are the structural fix; window size is a buffer.
Does this affect Cursor, Codex and other agents too?
Yes. Any agent that summarises to stay under a context limit loses the same kind of detail: Cursor's conversation summarisation, Codex's context management, Cline's auto-condense. The names differ, the failure mode is identical, and so is the fix.