CLAUDE LABJP
COWORK — Claude Cowork expands to web and mobile with remote sessions, synced files, and a shared Chat and Cowork home across devicesM365 — Claude Cowork adds Microsoft 365 write tools to draft and send email, manage calendars, and update OneDrive and SharePoint filesMCP — Admins can provision MCP connectors org-wide through their identity provider, starting with Okta, with access granted on first loginCODE — Claude Code fixes SessionStart hook streaming in headless sessions so remote workers are not idle-reaped mid-hookGOV — Claude Code and Claude Cowork are in public beta in Claude for Government Desktop on a FedRAMP High authorized environmentLIMITS — Claude Code weekly usage limits are increased by 50% through July 13COWORK — Claude Cowork expands to web and mobile with remote sessions, synced files, and a shared Chat and Cowork home across devicesM365 — Claude Cowork adds Microsoft 365 write tools to draft and send email, manage calendars, and update OneDrive and SharePoint filesMCP — Admins can provision MCP connectors org-wide through their identity provider, starting with Okta, with access granted on first loginCODE — Claude Code fixes SessionStart hook streaming in headless sessions so remote workers are not idle-reaped mid-hookGOV — Claude Code and Claude Cowork are in public beta in Claude for Government Desktop on a FedRAMP High authorized environmentLIMITS — Claude Code weekly usage limits are increased by 50% through July 13
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 Code186hooks14context7automation90PreCompact

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-07-01
My Claude Code Hooks Stopped Firing After an Update — the Hyphenated Matcher Exact-Match Change in v2.1.195
In Claude Code v2.1.195, hook matchers containing a hyphen switched from partial match to exact match, silently disabling an existing PreToolUse hook. Here is how I isolated the cause and how to write matchers that won't break.
Claude Code2026-06-18
Moving Cleanup and Logging into a SessionEnd Hook
How to use Claude Code's new post-session hook to automate temp-file cleanup and log writing after a session ends, with real examples from a pipeline that processes several repositories in sequence.
Claude Code2026-07-07
I Could Detect Failures — Then the Alerts Grew So Loud I Stopped Noticing
The moment I could detect silent job failures, my phone started buzzing too often to be useful. Here is how I collapsed alerts by action rather than event, added hysteresis and quiet hours, and built a three-step escalation so only the pages worth waking me survive.
📚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
See all →