[BUG] Calling `claude -p` from a `SessionEnd` hook can recursively create child sessions that trigger hooks again
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
An end-of-session automation in my local Claude Code setup appears to trigger claude -p as a child run, and that child run appears to participate in the normal Claude Code lifecycle again.
The result is recursive session creation:
- a parent session ends
- a
SessionEndhook or similar automation launchesclaude -p - the child run creates its own new root Claude Code session
- that child session ends
- the same hook fires again
- another
claude -prun is created
In my environment this caused a large number of new root session files under ~/.claude/projects/-Users-minjaewon, and the generated sessions were not normal user conversations. They were mostly meta-prompts asking Claude Code to summarize earlier sessions.
The prompt bodies also became recursively nested.
This eventually escalated into context and usage failures.
Observed impact:
- 5996 total JSONL files including nested entries
- 373 root sessions created within the most recent hour during investigation
- many recent sessions started with
queue-operation: enqueue - recent failures included
Extra usage is required for 1M context - later follow-up sessions hit
You've hit your limit
This looks like a product safety gap around child claude -p runs inheriting hooks or participating in lifecycle automation without a guard.
What Should Happen?
Claude Code should not allow lifecycle hooks or end-of-session automations to recursively re-trigger indefinitely on child claude -p executions.
Expected behavior would be one of:
- child
claude -pruns do not trigger the same lifecycle hooks by default - there is an official
--no-hooksor equivalent mode for child runs - Claude Code detects repeated self-similar queue-generated meta sessions and stops the loop
- Claude Code adds a circuit breaker for rapid repeated
SessionEnd-spawned runs
At minimum, there should be a safe default that prevents recursive session explosions and prompt self-nesting from consuming context and usage.
Steps to Reproduce
- Configure a Claude Code environment with lifecycle hooks enabled, including a
SessionEndhook. - Make the
SessionEndautomation invokeclaude -pto summarize or post-process the just-finished session. - Let the original session end.
- Observe that the child
claude -pexecution creates a new root Claude Code session instead of behaving as an isolated helper call. - If the child run is also subject to the same lifecycle hooks, observe that ending the child run triggers the same
SessionEndautomation again. - Repeat for several generations.
- Observe:
- rapid creation of many new root session files
- queue-style sessions with meta-only prompts
- recursive prompt nesting if earlier transcript or summary content is re-inserted
- eventual context or usage exhaustion
A simplified reproduction shape is:
SessionEndhook fires- hook runs
claude -p "<summarize previous session>" - child run creates a new Claude Code session artifact
- child run ends
- child's
SessionEndfires - same hook runs again
- recursion continues
Claude Model
Opus
Is this a regression?
I don't know
Last Working Version
2.1.104
Claude Code Version
2.1.104
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Warp
Additional Information
_No response_
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗