Add PreCompaction hook event to flush context before automatic compression

Resolved 💬 3 comments Opened Mar 10, 2026 by glouie Closed Apr 10, 2026

^[[38;2;127;132;156m─────┬──────────────────────────────────────────────────────────────────────────^[[0m
^[[38;2;127;132;156m│ ^[[0m^[[1mSTDIN^[[0m
^[[38;2;127;132;156m─────┼──────────────────────────────────────────────────────────────────────────^[[0m
^[[38;2;127;132;156m 1^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m## Problem^[[0m
^[[38;2;127;132;156m 2^[[0m ^[[38;2;127;132;156m│^[[0m
^[[38;2;127;132;156m 3^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244mWhen a conversation approaches context limits, Claude Code automatically compresses prior messages. Currently there is no hook event fired before this happens, which means agents and plugins cannot save critical state (e.g., intermediate reasoning, key decisions, task progress) to persistent storage before context is lost.^[[0m
^[[38;2;127;132;156m 4^[[0m ^[[38;2;127;132;156m│^[[0m
^[[38;2;127;132;156m 5^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244mCompaction is lossy — compressed summaries lose nuance, exact code snippets, and intermediate reasoning. For long-running agentic workflows (multi-step debugging, feature development, orchestration pipelines), this can mean losing important context mid-task.^[[0m
^[[38;2;127;132;156m 6^[[0m ^[[38;2;127;132;156m│^[[0m
^[[38;2;127;132;156m 7^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m## Proposal^[[0m
^[[38;2;127;132;156m 8^[[0m ^[[38;2;127;132;156m│^[[0m
^[[38;2;127;132;156m 9^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244mAdd a PreCompaction hook event that fires before automatic context compression begins. This would allow:^[[0m
^[[38;2;127;132;156m 10^[[0m ^[[38;2;127;132;156m│^[[0m
^[[38;2;127;132;156m 11^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m- Auto-saving conversation state to memory files before context is compressed^[[0m
^[[38;2;127;132;156m 12^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m- Logging what's about to be compressed for auditing or debugging^[[0m
^[[38;2;127;132;156m 13^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m- Giving agents a chance to summarize and persist key context proactively, rather than relying on manual "remember this" prompts^[[0m
^[[38;2;127;132;156m 14^[[0m ^[[38;2;127;132;156m│^[[0m
^[[38;2;127;132;156m 15^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m## Use Case^[[0m
^[[38;2;127;132;156m 16^[[0m ^[[38;2;127;132;156m│^[[0m
^[[38;2;127;132;156m 17^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244mLong-running agentic workflows accumulate important intermediate state — partial results, decision rationale, progress checkpoints. Today, the only way to persist this is to manually ask Claude to "remember" things or proactively write to memory files. A PreCompaction hook would let this happen automatically, reducing information loss during extended sessions.^[[0m
^[[38;2;127;132;156m 18^[[0m ^[[38;2;127;132;156m│^[[0m
^[[38;2;127;132;156m 19^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m## Suggested Interface^[[0m
^[[38;2;127;132;156m 20^[[0m ^[[38;2;127;132;156m│^[[0m
^[[38;2;127;132;156m 21^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244mConsistent with existing hook events (PreToolUse, PostToolUse, SessionStart), a PreCompaction hook could:^[[0m
^[[38;2;127;132;156m 22^[[0m ^[[38;2;127;132;156m│^[[0m
^[[38;2;127;132;156m 23^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m1. Fire before compression begins^[[0m
^[[38;2;127;132;156m 24^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m2. Provide the current context size / token count as input^[[0m
^[[38;2;127;132;156m 25^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m3. Allow the hook script to write to memory or logs^[[0m
^[[38;2;127;132;156m 26^[[0m ^[[38;2;127;132;156m│^[[0m ^[[38;2;205;214;244m4. Proceed with compaction after the hook completes^[[0m
^[[38;2;127;132;156m─────┴──────────────────────────────────────────────────────────────────────────^[[0m

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗