MacAppKit Guides

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 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:

  1. 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".
  2. 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.
  3. 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

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.

LonghaulClaude Code plugin made by the MacAppKit team. Pay what you want, from $9.
See Longhaul

Test that it works

  1. Start a task with several steps and let Claude write a few ledger lines.
  2. Run /compact yourself.
  3. Ask "what is the next step?" The answer should match the ledger, not a vague summary.
  4. 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.