claude-code - 💡(How to fix) Fix [BUG] Calling `claude -p` from a `SessionEnd` hook can recursively create child sessions that trigger hooks again [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#47871Fetched 2026-04-15 06:39:57
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Author
Timeline (top)
labeled ×3commented ×1
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • 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 SessionEnd hook or similar automation launches claude -p
  • the child run creates its own new root Claude Code session
  • that child session ends
  • the same hook fires again
  • another claude -p run 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 -p runs do not trigger the same lifecycle hooks by default
  • there is an official --no-hooks or 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

  1. Configure a Claude Code environment with lifecycle hooks enabled, including a SessionEnd hook.
  2. Make the SessionEnd automation invoke claude -p to summarize or post-process the just-finished session.
  3. Let the original session end.
  4. Observe that the child claude -p execution creates a new root Claude Code session instead of behaving as an isolated helper call.
  5. If the child run is also subject to the same lifecycle hooks, observe that ending the child run triggers the same SessionEnd automation again.
  6. Repeat for several generations.
  7. 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:

  1. SessionEnd hook fires
  2. hook runs claude -p "<summarize previous session>"
  3. child run creates a new Claude Code session artifact
  4. child run ends
  5. child's SessionEnd fires
  6. same hook runs again
  7. 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

extent analysis

TL;DR

To prevent recursive session creation, consider modifying the SessionEnd hook to exclude child claude -p runs from triggering the same lifecycle hooks.

Guidance

  • Review the SessionEnd hook configuration to determine if it can be modified to ignore child claude -p executions or to use a different automation trigger.
  • Investigate the possibility of adding a conditional statement to the hook to check if the current session is a child run before invoking claude -p.
  • Consider implementing a counter or a unique identifier to track the number of recursive sessions and prevent further claude -p runs once a threshold is reached.
  • Evaluate the need for an official --no-hooks mode for child runs to prevent lifecycle hooks from being triggered.

Example

# Example of a modified SessionEnd hook
if [ "$CLAUBE_PARENT_SESSION" != "true" ]; then
  claude -p "<summarize previous session>"
fi

Note: The above example assumes the existence of a CLAUBE_PARENT_SESSION environment variable that indicates whether the current session is a child run.

Notes

The provided information suggests that the issue is related to the SessionEnd hook triggering claude -p runs, which in turn create new sessions. However, without further details on the hook's implementation, it's challenging to provide a definitive solution.

Recommendation

Apply a workaround by modifying the SessionEnd hook to prevent recursive claude -p runs, as this approach can help mitigate the issue without requiring changes to the underlying Claude Code functionality.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING