◉CLAUDE LABJP
●2.1.296 — Sonnet 5.5 cache reads drop to $0.10/Mtok (was $0.20), and the MCP description limit rises to 4,096 characters●2.1.295 — Hooks gain onFailure: "block", so a failed or timed-out hook no longer lets the action through●SONNET 4.5 — Retires on the Claude API on 11/30, 51 days left. Migrate to Sonnet 5.5●Q&A — A long-running report: the console jumps back to the top of history whenever output is appended●HAIKU 5.5 — Code written for Haiku 4.5 hits a 400 error on budget_tokens●NEW — When first requests keep missing, hand over a one-page description of your work first●2.1.296 — Sonnet 5.5 cache reads drop to $0.10/Mtok (was $0.20), and the MCP description limit rises to 4,096 characters●2.1.295 — Hooks gain onFailure: "block", so a failed or timed-out hook no longer lets the action through●SONNET 4.5 — Retires on the Claude API on 11/30, 51 days left. Migrate to Sonnet 5.5●Q&A — A long-running report: the console jumps back to the top of history whenever output is appended●HAIKU 5.5 — Code written for Haiku 4.5 hits a 400 error on budget_tokens●NEW — When first requests keep missing, hand over a one-page description of your work first
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 Code259hooks20context11automation109PreCompact

✦ 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 $15 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-05
I Wrote "I'll Do the Commits" in CLAUDE.md. The Next Morning git log Had One More Line.
When Claude Code commits despite being told not to: undo with git reset --soft, then stop it physically with permissions.deny plus a PreToolUse hook. Covers the git -c form that deny rules let through, and how to test the hook without starting Claude Code.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links