Feature request: Context threshold hook / expose context usage to hooks

Resolved 💬 5 comments Opened Mar 4, 2026 by RLADR Closed Apr 7, 2026

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)

  1. 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.'"
}]
}]
}
}
``

  1. Expose $CLAUDE_CONTEXT_USED_PERCENT as an environment variable in all hook commands — this would let existing hooks (like PostToolUse) implement threshold logic themselves
  1. 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.

View original on GitHub ↗

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