Feature: Expose context window usage in hook input JSON

Resolved 💬 3 comments Opened Feb 22, 2026 by EveryToolAsHammer Closed Feb 26, 2026

Problem

When using hooks (e.g., UserPromptSubmit, PreToolUse) to guard against expensive operations, there's no way to know how much context window remains. This makes it impossible to reliably prevent auto-compaction from firing mid-test-run or mid-task.

Current workaround: Parse the transcript_path JSONL file from within the hook to extract the last usage.input_tokens value. This is fragile — it depends on transcript format internals and doesn't account for the prompt about to be sent.

Proposed Solution

Add context window fields to the hook input JSON for all (or select) hook events:

{
  "session_id": "...",
  "transcript_path": "...",
  "context_window": {
    "max_tokens": 200000,
    "used_tokens": 162000,
    "remaining_tokens": 38000,
    "used_percentage": 81.0,
    "compaction_threshold_percentage": 95.0
  }
}

This would be most valuable on:

  • UserPromptSubmit — block a prompt before it triggers compaction
  • PreToolUse — skip expensive tool calls (e.g., Bash running a test suite) when context is tight
  • PreCompact — make informed decisions about whether to allow or customize compaction

Use Case

Running long test suites or multi-step build/flash/verify loops where context compaction mid-task causes loss of intermediate state and broken workflows. A hook that blocks with "Context is 85% full — run /compact first" would prevent this entirely.

Alternatives Considered

  • Parsing transcript_path: Works but fragile and doesn't include the pending prompt size.
  • /cost command: Manual, not automatable via hooks.
  • Status line: Visual only, no programmatic access from hooks.

View original on GitHub ↗

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