[BUG] Hook loop silently burns all context tokens — session dies with no warning that a hook caused it
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported in this specific form
- [x] This is a single bug report
- [x] I am using the latest version of Claude Code
What's Wrong?
A custom PreToolUse hook entered a loop during an active agent session, firing on every tool invocation without completing cleanly. Each hook execution injects output as system-reminder context. The hook fired hundreds of times across a long session, silently consuming all available context tokens. The session died from context exhaustion.
The problem isn't just the death — it's the complete invisibility of the cause. The error shown to the user was the standard "context limit reached · /compact or /clear to continue." There was no indication that a hook was responsible. The user had no way to:
- Know a hook was looping
- See how many times it had fired
- Know how much context the hook had consumed
- Interrupt the hook before the session died
Hours of work were lost with no data recovery path.
Version
v2.1.92, Node.js runtime, Windows 11
Steps to Reproduce
- Create a
PreToolUsehook that reads state from a file and writes back to it (e.g., an audit/claims hook) - Run a complex multi-agent session with many tool invocations (100+ tool calls)
- If the hook encounters a state management issue or logic error, it fires on every tool call
- Context fills with hook output (each hook execution adds ~500-2000 tokens of system-reminder output)
- After N invocations, context limit is reached
- CC shows standard "context limit reached" — no mention of hooks
What Should Happen
At minimum, one of:
- Attribution in error message: "Context limit reached. Note: PreToolUse hook
.claude/hooks/claims_hook.pyfired 347 times this session, consuming approximately 180k tokens." - Hook execution counter: Visible in
/costoutput or status line — "Hooks: 347 invocations, 183k tokens" - Circuit breaker: When context > 85% full, pause hook execution and warn user: "Hook firing paused — context critically low. Hook
.claude/hooks/Xhas fired 200+ times." - Hook loop detection: If the same hook fires on >50 consecutive tool calls, warn: "Hook may be looping. Fire count: 50. Pause? [Y/N]"
Impact
- Complete session death with no recovery path
- Cause is invisible — user cannot diagnose why context filled so fast
- All in-progress work lost
- Particularly damaging for long-running agent sessions (overnight pipelines, multi-step analyses)
Related Issues
- #2038 — context loop / compaction spiral (oncall)
- #24179 — compaction death spiral (211 compactions)
- #35166 — MCP repeated requests loop ($500+ in tokens)
The pattern across all three: a runaway process silently consuming tokens with no user-visible warning until the session is already dead.
Proposed Fix Priority
Adding hook invocation counts to /cost output would take one engineer one day and prevent this entire class of invisible session deaths. Please prioritize this.
---
Reproduced on v2.1.92, Windows 11, Node.js runtime. Max plan subscriber paying $200/month — currently doing Anthropic's QA testing for free.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗