Add token_count and usage_percent to PreCompact hook input
Summary
The PreCompact hook currently receives {trigger, custom_instructions} but not the token count or usage percentage. Hook scripts cannot make informed decisions about what to preserve because they don't know how full the context is.
The data already exists internally — preCompactTokenCount is calculated by MG() in the compaction orchestrator before CGH() (PreCompact hook executor) is called. This is purely a plumbing change.
Related Issues
- #34340 — Expose context window usage to hooks via environment variable
- #30590 — Context threshold hook / expose context usage to hooks
- #32881 — Add PreCompaction hook event to flush context before automatic compression
Also related to: "Add ContextThreshold hook event" and "Inject context usage into system-reminder" (filing separately).
Proposed Hook Input Schema
{
"session_id": "abc123",
"transcript_path": "/path/to/transcript.jsonl",
"cwd": "/current/working/directory",
"hook_event_name": "PreCompact",
"trigger": "auto",
"custom_instructions": "",
"token_count": 820000,
"context_window_size": 1000000,
"usage_percent": 82,
"auto_compact_threshold": 850000
}
Implementation Detail
From binary analysis of v2.1.81:
In CGH() (PreCompact hook executor) — the hook input is constructed as:
let L = {
...g4(void 0),
hook_event_name: "PreCompact",
trigger: H.trigger,
custom_instructions: H.customInstructions
};
Add the token fields:
let L = {
...g4(void 0),
hook_event_name: "PreCompact",
trigger: H.trigger,
custom_instructions: H.customInstructions,
token_count: H.tokenCount ?? null,
context_window_size: H.contextWindowSize ?? null,
usage_percent: (H.tokenCount && H.contextWindowSize)
? Math.round((H.tokenCount / H.contextWindowSize) * 100) : null,
auto_compact_threshold: H.autoCompactThreshold ?? null
};
In RGH() (compaction orchestrator) — M = MG(H) is already calculated before CGH() is called. Just pass it through:
let O = await CGH({
trigger: f ? "auto" : "manual",
customInstructions: D ?? null,
tokenCount: M,
contextWindowSize: getContextWindowSize($.options.mainLoopModel),
autoCompactThreshold: _?.autoCompactThreshold ?? null
}, $.abortController.signal);
Complexity
Low. The token count already exists in the calling function. This is a one-line plumbing change — pass one extra field through to the hook input constructor.
Use Case
With token count available, PreCompact hooks can:
- Decide how aggressively to flush memory based on how much context is being compacted
- Log context pressure metrics for debugging
- Trigger different preservation strategies at different usage levels
- Provide meaningful context to the model via
additionalContextin hook output
Community Demand
8+ existing issues requesting context usage exposure to hooks, with significant community engagement. This is the simplest of the three related proposals and could be shipped independently.
---
Labels suggestion: enhancement, area:hooks
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗