Feature: Expose context window usage in hook input JSON
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 compactionPreToolUse— skip expensive tool calls (e.g.,Bashrunning a test suite) when context is tightPreCompact— 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. /costcommand: Manual, not automatable via hooks.- Status line: Visual only, no programmatic access from hooks.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗