Feature: Pre-compaction hooks and context usage detection
Summary
Claude Code should provide a way to detect when context compaction is approaching, and allow users to define triggered actions (hooks) that run before compaction occurs.
Use Case
When working on complex multi-session projects, users often need to preserve state in external files (like PROJECT_CONTINUITY.md) before context compaction happens. Currently, this requires:
- Manually noticing the context percentage is high
- Asking Claude to "update tears" (save state)
- Hoping the update completes before compaction
This is error-prone and leads to lost context.
Proposed Solution
- Detection API: Expose the current context usage percentage or a "compaction imminent" signal
- Hooks: Allow users to define pre-compaction hooks in settings (similar to existing hooks):
``json``
{
"hooks": {
"preCompact": [
{"command": "claude", "args": ["--prompt", "Update PROJECT_CONTINUITY.md with current state"]}
]
}
}
Alternative: System Reminder
If hooks are too complex, a simpler solution would be a system-reminder that triggers at (e.g.) 90% context usage, reminding Claude to save state to designated files.
Benefits
- Reduces context loss during long sessions
- Enables seamless multi-session workflows
- Gives users control over what gets preserved
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗