Feature Request: PreCompact Hook / Context Threshold Event

Resolved 💬 3 comments Opened Feb 4, 2026 by hulryung Closed Feb 7, 2026

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

  1. Auto-documentation: Automatically generate a markdown summary of current work before compaction
  2. State persistence: Save task lists, file modifications, and debugging findings to a recoverable format
  3. Session continuity: Create breadcrumbs that help restore context after compaction
  4. 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

  1. Expose current context usage percentage via environment variable (e.g., $CLAUDE_CONTEXT_USAGE)
  2. Add PreCompact or OnContextThreshold hook type
  3. Allow hook to output text that gets included in the compaction summary

View original on GitHub ↗

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