Feature request: Context threshold hook / expose context usage to hooks
Summary
Add a hook event (or environment variable accessible to existing hooks) that allows users to trigger actions when context window usage reaches a configurable threshold — before auto-compaction kicks in.
Motivation
When working on long sessions, valuable context (decisions made, patterns discovered, debugging insights) gets lost during auto-compaction. The existing PreCompact hook fires too late — context is already full, leaving insufficient room to save learnings to files.
A threshold-based trigger (e.g., at 90-95% capacity) would give enough remaining context budget to write summaries, update memory files, or take other preservation actions before compression discards information.
Proposed Solutions (any of these would work)
- New hook event:
ContextThreshold— fires when context reaches a user-configured percentage
``json``
{
"hooks": {
"ContextThreshold": [{
"threshold": 0.90,
"hooks": [{
"type": "command",
"command": "echo 'Save important learnings to memory files before context compaction.'"
}]
}]
}
}
- Expose
$CLAUDE_CONTEXT_USED_PERCENTas an environment variable in all hook commands — this would let existing hooks (likePostToolUse) implement threshold logic themselves
- Add a threshold option to
PreCompact— fire it earlier, at a configurable percentage rather than only when compaction is imminent
Use Case
Users with auto-memory directories (~/.claude/projects/.../memory/) want Claude to proactively save session learnings before they're lost to compaction. Currently there's no reliable way to trigger this — PreCompact fires too late, and context metrics aren't accessible from hooks or external processes.
This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗