How to keep state across Claude Code context compaction
Long Claude Code sessions fill the context window. When that happens, Claude Code compacts: it replaces the conversation with a summary. Summaries lose detail. After a compaction you may find Claude has forgotten which step it was on, which server it started, or the numbers it had measured, and it starts over or repeats work. The fix is to keep the important state outside the conversation and make sure it comes back after compaction.
What gets lost
A summary favours the story of what happened over the details you need next. The usual casualties are:
- The exact next step of an unfinished task.
- Background processes, branches and temp files the session created, which then never get cleaned up.
- Measured numbers: test counts, file sizes, timings, money.
- Decisions you made earlier and their reasons.
The principle: a file is the source of truth
Conversation memory is temporary; a file on disk is not. Keep a short ledger file in your project, for example .longhaul/LEDGER.md or a plain PROGRESS.md, with one line per finished milestone and the next concrete step. Then tell Claude, in CLAUDE.md or the prompt, to read it first and to append a line after each step. Even this manual version helps a lot. A CLAUDE.md file is loaded at the start of each session, so it can hold the standing rule.
Automating it with hooks
Manual reminders get forgotten, so use Claude Code hooks, scripts that run on lifecycle events. Three events matter here:
- PreCompact runs just before compaction. Your hook can print instructions that are added to the summarising step, for example "keep verbatim: the ledger path, every unfinished task with its next step, every process you started, and the numbers measured so far".
- PostCompact runs after it, and receives the new summary. Your hook can append that summary, with a timestamp, to the ledger so a copy exists on disk.
- SessionStart runs when a session starts, resumes or continues after compaction. Your hook can return the newest part of the ledger as additional context, so Claude begins with the last unfinished step instead of a blank slate.
Two practical limits: hook output injected as context has a size cap (10,000 characters), so load only the tail of a long ledger, and keep ledger lines short, one per milestone, so the tail stays useful.
What to write in the ledger
- Done: "step 3 finished, 212 tests pass."
- Next: the next concrete action, not a goal.
- Running: PIDs or ports of servers, and temp paths you created, with how to clean them.
- Numbers: anything expensive to measure again.
Longhaul's ledger
Longhaul is a Claude Code plugin made by the MacAppKit team. Its ledger uses exactly the three hooks above. Before compaction it asks the summariser to keep the ledger path, every unfinished task with its next step, every uncleaned process, server, temp file or branch the session created, and the numbers measured so far, and to drop the narration. After compaction it appends the summary to .longhaul/LEDGER.md with a timestamp. When a session starts, resumes or continues after compaction, it feeds the newest part of the ledger back to Claude, capped well below the hook limit, with an instruction to continue from the last unfinished step and append a line after each finished step.
The same plugin has a cleanup guard that records the exact processes and temp files a session creates, and nudges Claude once at the end to clean up (it never deletes or kills anything itself), and a token budget guard described in our guide on limiting token use. It uses only the Python standard library, makes no network calls, and works on macOS and Linux with Python 3.8 or later. Installation is unzip, then claude --plugin-dir ~/tools/longhaul, and /hooks shows the entries it loaded. The ledger part is free and MIT-licensed on GitHub; the full Longhaul is pay-what-you-want from $9.
See Longhaul
Test that it works
- Start a task with several steps and let Claude write a few ledger lines.
- Run
/compactyourself. - Ask "what is the next step?" The answer should match the ledger, not a vague summary.
- Open the ledger file and confirm a dated compaction entry appeared.
One honest limit: a ledger only helps if the information gets written. Keep the rule simple (append one line after each finished step) and check the file now and then, because Claude can still skip a step.