Allow hooks to inject messages into conversation at context threshold
Problem
Claude Code's status bar shows context window usage percentage, but Claude itself has no visibility into this number. Users can instruct Claude in CLAUDE.md to warn them before auto-compaction, but Claude literally cannot comply — it has no access to context metrics.
The PreCompact and PostCompact hooks exist but are observability-only (logging/cleanup). They cannot inject a message into the conversation that Claude would see and act on.
Use Case
I run /session-wrap before compaction to preserve context into persistent memory. I want Claude to automatically trigger this when context gets low, but there's no mechanism for it.
Proposed Solution
One or both of:
- A context threshold hook — a new hook event (e.g.,
ContextThreshold) that fires when context usage crosses a configurable percentage (e.g., 85%), with the ability to inject a system message into the conversation that Claude can see and respond to.
- Allow hooks to inject system messages — let any hook (including
PreCompact) return text that gets added to the conversation as a system message, so Claude can act on it.
Either approach would let users set up automatic session wrap-ups, saving, or other cleanup before compaction silently drops context.
Current Workaround
User manually watches the status bar percentage and tells Claude to wrap up. This defeats the purpose of having an AI assistant manage the workflow.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗