[FEATURE] PreCompaction hook event to run commands before context is compressed
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)
Problem Statement
Problem
When a long conversation approaches the context limit, Claude Code
automatically compacts prior messages into a summary. This is the exact moment
where detailed session knowledge (debugging steps, error messages, discovered
patterns) is about to be lost — and it's the ideal time to persist lessons
learned.
Currently there's no way to trigger actions before compaction happens. The
available hook events (Stop, PermissionRequest, Notification) don't cover this
case.
Proposed Solution
Add a PreCompaction (or ContextCompaction) hook event that fires before the
context window is compressed. This would allow users to run commands like:
{
"hooks": {
"PreCompaction": [
{
"hooks": [
{
"type": "command",
"command": "echo 'Context is about to be compacted — consider
running /lessons-learned'"
}
]
}
]
}
}
Alternative Solutions
- ContextThreshold event — fire when context usage exceeds a configurable
percentage (e.g., 80%). More flexible than PreCompaction since it gives time
to act before the hard limit.
- Exposing context usage in the statusline API — let statusline.command
scripts access current context % so they can display a visual warning. Lower
scope but still useful.
Priority
Medium - Would be very helpful
Feature Category
Configuration and settings
Use Case Example
I use a /lessons-learned skill that extracts reusable patterns, bug fixes, and
workflow insights from a session and persists them to auto-memory files and
skill definitions. This compounds Claude's effectiveness across sessions.
The best time to capture these lessons is right before compaction, when the
full detailed context is still available. After compaction, the summary
preserves the gist but loses the specific code patterns, error messages, and
debugging steps that make lessons actionable.
Additional Context
A standing instruction in MEMORY.md tells Claude to suggest /lessons-learned
when resuming after compaction. This works but captures less detail since the
rich context has already been compressed.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗