◉CLAUDE LABJP
●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…
Articles/Claude Code
⟐ Claude Code/2026-07-10Advanced

Carrying Decisions Across Compaction with PreCompact and SessionEnd Hooks

Auto-compaction does not delete your conversation. It deletes the reasons behind it. Here is a working PreCompact / SessionEnd / SessionStart hook pipeline that rescues decisions to disk and hands them to the next session, with real code and measurements.

Claude Code264hooks22context11automation109PreCompact

✦ Premium Article

At 2 a.m. one of my unattended runs came back from a compaction and cheerfully proposed a design I had rejected two hours earlier.

The conversation had survived. The single line explaining why it was rejected had not.

Compaction trims for length, not for importance. What survives is what happened. What evaporates is why it happened. As an indie developer running four sites on overnight automation, that missing line turns into rework the next morning.

So I started rescuing decisions to disk right before compaction fires, and reading them back when the next session opens. Three Claude Code hooks do the whole job: PreCompact, SessionEnd, and SessionStart.

What compaction actually throws away

Auto-compaction fires as the context approaches its ceiling. It runs the same machinery as a manual /compact, except unattended runs do not get to choose the moment.

A six-hour generation session across four sites produced a 14.2 MB transcript here — roughly 3,900 JSONL lines. The summary left in context afterward was about 4,100 characters. That compression ratio is reasonable. What it did not include was the constraint "articles.json carries metadata only, because the Cloudflare Workers bundle ceiling is 62 MiB."

The summarizer was not at fault. I had simply never told anyone what had to survive.

Rather than negotiate with the summary, extract the decisions yourself and park them outside the context. That is the whole design.

What a PreCompact hook receives

PreCompact fires immediately before compaction runs, whether automatic or manual. Your script gets one JSON object on stdin.

{
  "session_id": "0b8f2c14-...",
  "transcript_path": "/Users/you/.claude/projects/.../0b8f2c14-....jsonl",
  "cwd": "/Users/you/work/claudelab.net",
  "hook_event_name": "PreCompact",
  "trigger": "auto",
  "custom_instructions": ""
}

Three fields matter.

trigger is either "auto" or "manual". It only reads "manual" when a human typed /compact. Unattended work is almost always "auto".

transcript_path is the absolute path to the session's JSONL history — one message per line, still in its pre-compaction state. This is the point of the exercise: miss PreCompact and the raw history is gone for good.

custom_instructions carries whatever you passed to a manual compact. It is an empty string on auto.

HookFires whenRole in this pipeline
PreCompactJust before compactionMine the raw transcript for decisions
SessionEndSession terminatesTake the final snapshot
SessionStartSession opens or resumesInject the rescued decisions
✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦If long sessions keep forgetting constraints after every compaction, you can now rescue just the decisions with a PreCompact hook
✦You get complete Python for safely reading the transcript_path JSONL and restoring it through SessionStart additionalContext
✦You can sidestep the three traps I hit in production: the 60-second timeout, stdout leaking into context, and the trigger value everyone guesses wrong
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

⟐ Claude Code2026-09-02
Two hooks that record every model switch and stop the ones you never agreed to
Build a PostModelSwitch hook that logs every model change and a PreModelSwitch hook that blocks unapproved switches during unattended runs. Complete scripts, measured timings, and the reason the two jobs must stay separate.
⟐ Claude Code2026-08-28
Don't let your verification script's full output flow back through a hook
The check was working the whole time. The conversation was what ran out of room. Measured side by side: 58 bytes from one scan of 833 files, 42KB from another. Which paths actually reach the model, and how to enforce a budget on them.
⟐ Claude Code2026-10-15
The Hook That Was Meant to Guard My Japanese Folder Never Blocked Anything — Python Was Reading stdin as cp932
A PreToolUse hook guarding folders like 経理 or 議事録 silently lets edits through when Python's stdin defaults to cp932. How I reproduced it, the three-line fix, and one command to check your own hooks.