Feature Request: PreCompact Hook / Context Threshold Event
Problem Statement
When working on complex, long-running tasks (e.g., Linux kernel driver development, multi-file refactoring), the conversation context eventually fills up and triggers automatic compaction/summarization. While the summarization preserves key information, important implementation details, debugging context, or work-in-progress state can be lost.
Currently, there's no way to automatically save or document the current work state before compaction occurs. Users must manually remember to save context, which is error-prone during deep focus work.
Proposed Solution
Add a new hook type that triggers before context compaction:
Option A: PreCompact hook
{
"hooks": {
"PreCompact": [
{
"command": "claude-save-context.sh"
}
]
}
}
Option B: OnContextThreshold hook with configurable threshold
{
"hooks": {
"OnContextThreshold": [
{
"threshold": 0.8,
"command": "echo 'Context at 80%'"
}
]
}
}
Use Cases
- Auto-documentation: Automatically generate a markdown summary of current work before compaction
- State persistence: Save task lists, file modifications, and debugging findings to a recoverable format
- Session continuity: Create breadcrumbs that help restore context after compaction
- Team handoff: Generate status reports when context limit is approaching
Alternatives Considered
- Stop hook: Only triggers on session end, not on compaction
- Manual
/remember: Requires user awareness of context usage; easy to forget - Periodic auto-save: Could work but less efficient than threshold-based triggering
Additional Context
Complex development tasks like BSP/driver development often require:
- Hardware register mappings
- Debugging session history
- Cross-file dependency understanding
- Iterative fix attempts
Losing this context mid-session significantly impacts productivity. A PreCompact hook would allow users to build workflows that preserve critical information automatically.
Suggested Implementation
- Expose current context usage percentage via environment variable (e.g.,
$CLAUDE_CONTEXT_USAGE) - Add
PreCompactorOnContextThresholdhook type - Allow hook to output text that gets included in the compaction summary
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗