Claude Code forgets context mid-task: why, and how to keep the plan
In a long session the context window fills with file reads, tool output and back-and-forth. When it is compacted, details such as constraints you gave in chat or the remaining steps of a plan can be lost. Anything that only lives in the chat is at risk.
Why it happens
- Instructions given once in chat are not in any file, so they do not survive compaction or a new session.
- Large tool outputs (logs, whole files) use up the window fast.
- One session is used for several unrelated tasks.
Fixes, most effective first
- Write the plan to a file in the repo (for example PLAN.md) and ask the agent to tick steps off there.
- Move lasting constraints from chat into CLAUDE.md or AGENTS.md, so every session loads them.
- Start a new session per task. Point it at the plan file to continue.
- Ask for summaries of long logs instead of pasting them whole.
What survives compaction
- The project-root CLAUDE.md does: after /compact, Claude Code re-reads it from disk and adds it again.
- Instructions typed early in the chat may not: the docs say detailed instructions from early in the conversation may be lost, and to put persistent rules in CLAUDE.md.
- Skills used earlier keep only a short form; invoke the skill again to restore its full text.
Steer the summary
Run /compact with a focus, as in the docs' example /compact focus on the API changes, or add a "Compact Instructions" section to CLAUDE.md saying what every summary must keep. /context shows what is filling the window, and work that needs a lot of reading can go to a subagent, which has its own context and returns a summary.
When a skill or vault helps, and when it does not
Helps
- A skill reloads its full instructions whenever it is invoked, so a procedure kept in a skill is easy to restore after compaction.
- A vault keeps project knowledge on disk, where compaction cannot touch it.
Does not help
- No skill stops compaction; keep lasting rules in CLAUDE.md and plans in files.
- Loading big reference files into every session fills the window faster.