C² Method7 min read
Claude Code compaction ate my work: recovery and prevention
Auto-compact just fired mid-task and the agent has forgotten what it was doing. What is actually lost, how to get it back from disk, and how to set up so the next one doesn't hurt.
You are an hour into a task. Claude Code prints Conversation compacted, and the next answer is subtly wrong. It has forgotten the constraint you set at the start, or the file it was halfway through, or the reason you rejected the obvious fix. It carries on, confidently, from a summary that dropped the part that mattered.
Here's the short answer. Compaction didn't delete anything on disk. Your files, your git history and the full pre-compaction conversation are all still there. What went is the agent's working memory. Recovery means rebuilding that memory from disk, in a fixed order, before the agent writes another line. Prevention means making sure the important state never lived only in the chat.
This article is the recovery half. For why sessions lose context at all, and the routine that carries work between sessions, see how to stop Claude Code losing context between sessions. For context rot and why you'd compact on purpose, see context rot: how compaction keeps your agent sharp.
What does Claude Code compaction actually lose?
Per the context window docs, compaction replaces the conversation with a structured summary. Some things are rebuilt from disk afterwards: the project-root CLAUDE.md, auto memory, the plan from plan mode, and up to five of the files Claude most recently read or edited. Others are only as good as the summary: anything you said in chat, context hooks added earlier, and nested CLAUDE.md files and path-scoped rules until Claude touches a matching file again.
So the losses fall into three kinds:
- Instructions you gave in conversation. "Don't touch the migration", "use the old endpoint". If the summary didn't keep it, the agent no longer knows it.
- Why, not what. The summary usually keeps what was done. It is weaker on why an alternative was rejected, which is exactly what stops the agent re-trying it.
- Where it was. Which step of a multi-step task, which files are half-edited, what was verified and what wasn't.
It happens more than you'd think. I checked my own transcripts for my main monorepo. Between 24 October and 20 November 2026 there were 29 session transcripts, and 21 of them compacted at least once. In total they recorded 241 compactions. Every one was automatic. I never ran /compact by hand in that window. The biggest conversation compacted at 971,495 tokens. A typical one shrank from 262,756 tokens to 23,077 in 56 seconds. That is a lot of conversation turned into a page of summary.
What to do right after Claude Code compacts mid-task
Work through this in order. The first step matters most.
1. Stop the agent before it builds on the summary
If it's mid-run, press Escape. A wrong summary compounds: every step taken from it is another step to unpick. Nothing is lost by pausing.
2. Ask it what it thinks the state is
Ask one question: how's your context? Or more pointedly: what's the task, what's done, what's left, what constraints apply. You're not asking it to fix anything yet. You're reading the summary through its eyes so you can see what's missing.
When the setup is right, the answer is reassuring. In a live session, 22 minutes in and after the harness had already compacted, I asked exactly that. The agent's answer, verbatim: "everything load-bearing is externalized (PBs, briefs, PR bodies, memory), so even when the harness compacts the conversation, nothing about the program state lives only in my head." The full story is in what is context leadership. When the setup isn't right, the answer is vague, and that vagueness tells you where to dig.
3. Check git: the ground truth for code
Compaction never touches the working tree. So the state of the code is exactly what git says:
git status # which files are modified or new
git diff # what the edits actually are
git log --oneline -10 # what was committed, and in what order
git stash list # anything set aside
Show the agent the output, or have it run these itself. Half-finished edits are the most dangerous thing a compacted agent can forget, because it may start the same change again on top of the first attempt.
4. Re-read the brief or plan the task started from
If the task began from a written brief, a prompt brief, an issue, or a plan, have the agent read it again now. That file holds the intent and the acceptance criteria, and neither was written by the summariser. In my repos that means the prompt brief in docs/02-working/prompts/ and the latest session brief. A SessionStart hook can put the list in front of the agent automatically (more on that below).
5. Dig the lost detail out of the transcript
This is the step most people don't know about. Claude Code keeps a transcript of every session on disk. The .claude directory docs list it as ~/.claude/projects/<project>/<session>.jsonl: messages, tool calls and tool results. The <project> folder is your project path with the slashes turned into dashes. Transcripts are deleted after cleanupPeriodDays, which defaults to 30 days.
Compaction doesn't rewrite that file. In one of mine the compaction marker sits at line 1,084 of 2,521, and every message before it is still there. So the instruction the summary dropped is recoverable:
cd ~/.claude/projects/-Users-you-Projects-your-repo
ls -t *.jsonl | head -1 # newest session
grep -n '"compact_boundary"' <session>.jsonl # where each compaction happened
grep -n -i 'migration' <session>.jsonl | head # find the thing it forgot
What I saw on version 2.1.329: each compaction is a system record with subtype compact_boundary and a compactMetadata object holding the trigger (auto or manual) and the token counts before and after. The record after it is the summary, starting This session is being continued from a previous conversation that ran out of context. The docs document the file, not its fields, so treat those field names as what I observed, not a contract.
Two cautions. The transcript is plaintext and holds everything that passed through a tool, so don't paste it anywhere public. And it can be large. Search it. Don't ask the agent to read the whole thing back into the window you just freed.
6. Write the recovered state to a file, now
Once you've rebuilt the picture, put it on disk: a short session brief with what's done, what's half-done, what isn't verified, and the constraints that must hold. The next compaction then costs nothing, because the agent re-reads a file instead of trusting a summary.
How do I stop compaction losing context next time?
The real fix is upstream: don't let load-bearing state live only in the conversation.
One of my session briefs from April 2026 has a section headed Why this brief deserves to exist. Its first reason is recovery. The session had produced durable files, but in the brief's words, "the narrative of why those exist is in the live conversation. If the conversation is compacted or lost, a fresh session reading just the artefacts wouldn't know" why they were made. So the brief wrote the why down. That is the whole prevention strategy in two sentences.
In practice it is four habits:
| Habit | What it protects | Where it lives (C² layout) |
|---|---|---|
| Write the task as a brief before starting | Intent and acceptance criteria | docs/02-working/prompts/ |
| Commit small and often | The state of the code | git |
| Write a session brief before a long session gets longer | What's done, half-done, unverified | docs/02-working/session-briefs/ |
| Promote standing rules to the root CLAUDE.md | Constraints that must survive | re-read from disk after compaction |
My own monorepo, which predates that layout, has more than 1,100 files in its 2026 session-brief folder and more than 210 gotcha files. They exist so that no single conversation has to hold the project.
Can a hook protect context during compaction?
Yes, and there are three hook points worth knowing. All three are documented in the hooks reference.
SessionStart with the compact source. SessionStart hooks fire with a source of startup, resume, clear, compact or fork, and their plain-text stdout is added to Claude's context. The context window docs confirm that hooks matching compact run again after compaction and their output goes into the compacted context. My session-context.sh has no matcher, so it runs on every source, including after compaction. The list of recent briefs comes back automatically.
PreCompact. Runs before compaction, with a matcher of manual or auto. When it exits 0, its stdout becomes instructions for the compaction summary, capped at 10,000 characters per hook. A sketch, which I don't run myself:
#!/bin/bash
# .claude/hooks/pre-compact.sh: tell the summariser what to keep
echo "Preserve: the active task and its brief path, every file modified but not committed,"
echo "anything marked NOT verified, and every constraint the user stated in conversation."
git status --short | head -20
A PreCompact hook can also block compaction by exiting 2. Be careful. If the compaction was recovering from a context-limit error, blocking it makes the request fail. Steering the summary is safer than refusing it.
PostCompact. Runs after compaction and receives the generated summary as compact_summary. It can't change the result, but it can log the summary somewhere you'll see it, which is a cheap way to audit what your compactions keep.
Finally, when you do compact by hand, give it a focus: /compact focus on the refund flow, keep the rejected approaches. The docs also describe /rewind with Summarize from here, which compacts only part of the conversation.
The rule underneath
Compaction is not the failure. Treating the chat as the place where the project lives is the failure. The summary is a guess about what mattered. The brief, the diff and the transcript are records.
When it bites, stop, ask, check git, re-read the brief, search the transcript, write it down. When it doesn't bite, it's because you already wrote it down.
Start here: how to stop Claude Code losing context between sessions, write your first session brief, or run your first C² session.
Questions people ask
- Does Claude Code compaction delete my work?
- No. Compaction replaces the conversation in the context window with a summary. Your files, your git history and the session transcript on disk are untouched. What is lost is the agent's working memory: decisions and details that lived only in the chat and didn't make it into the summary.
- Can I see the conversation from before Claude Code compacted?
- Yes. Claude Code writes each session's transcript to ~/.claude/projects/<project>/<session>.jsonl, and the messages from before compaction stay in that file. You can search it with grep or jq for the instruction or decision the summary dropped. Transcripts are deleted after cleanupPeriodDays, 30 by default.
- What should I do right after Claude Code compacts mid-task?
- Stop the agent before it builds on a bad summary. Ask it what it thinks the state of the task is, then check that against git status, git diff and git log, re-read the brief or plan the task started from, and search the transcript for anything that is missing. Then write the recovered state to a file.
- Can I stop Claude Code from auto-compacting?
- A PreCompact hook that exits with code 2 blocks compaction. For a proactive auto-compact the conversation continues uncompacted, but if compaction was recovering from a context-limit error, blocking it makes the request fail. It is usually better to steer the summary than to block it.
- How do I tell Claude Code what to keep when it compacts?
- Run /compact with a focus, such as /compact focus on the auth bug fix. For automatic compaction, a PreCompact hook's stdout becomes instructions for the compaction summary, up to 10,000 characters per hook. A SessionStart hook that matches the compact source can also re-inject context after compaction.
Part of the C² Method. Start with the method
Put this in your repo
The C² Method is free and open. Point your agent at the kit and get started in minutes.