[BUG] Model emits end_turn while parallel background Task agents still running, returning to prompt prematurely
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report
- [x] I am using the latest version of Claude Code
What's Wrong?
When launching multiple Task agents with run_in_background=true in a single response, the model emits end_turn after some agents complete but before all finish. The session returns to the input prompt while 2-3 agents are still running in the background.
This is particularly problematic in batch/autonomous workflows (e.g., using --dangerously-skip-permissions --output-format stream-json) where the early exit terminates the entire Claude Code process. The remaining agents are killed mid-execution, and the orchestrating loop (which expected Claude to collect all agent results and act on them) gets an incomplete response.
Impact on autonomous workflows:
- External orchestrators (like ralphex) that feed prompts to Claude Code via CLI get truncated output
- Review phases that depend on collecting findings from all agents miss 50-60% of results
- The external loop cannot distinguish "all agents reported no issues" from "Claude exited before collecting results"
What Should Happen?
The model should wait for ALL background Task agents to complete (or call TaskOutput for each) before emitting end_turn. When 5 agents are launched, all 5 results should be collected before the model continues to the next step.
Previous versions (approximately before v2.1.32) exhibited this blocking behavior correctly.
Steps to Reproduce
- Create a prompt that instructs Claude to launch 5 Task agents with
run_in_background=true - Each agent should do moderate work (e.g., review a git diff, ~30-60 seconds each)
- Instruct the prompt to call TaskOutput(task_id, block=true) for each agent after launch
- Observe: after 1-2 agents complete, the model emits end_turn and returns to prompt
- Status bar shows "3 local agents" still running
Reproduction in batch mode:
claude --dangerously-skip-permissions --output-format stream-json -p "Launch 5 Task agents with run_in_background=true, then call TaskOutput for each. Agents: qa-expert, code-quality, go-test-expert, implementation-reviewer, documentation. Each should analyze git diff master...HEAD."
The process exits after 1-2 agents complete. Remaining agents are orphaned.
Actual Behavior
Timeline observed:
- Claude launches 5 background agents (correctly, all in one response)
- "All 5 agents are still running. Waiting for results."
- Agent "Code quality review" completes - Claude says "Waiting for remaining 4 agents"
- Agent "Documentation review" completes - Claude says "3 agents still running"
- Model emits end_turn - session drops to input prompt
- Status bar shows "3 local agents" still running
- In batch mode (--print or stream-json): process exits, agents killed
Attempted Workarounds
- Explicit TaskOutput(block=true) instructions - model sometimes calls TaskOutput for first 1-2 agents, then emits end_turn before calling it for the rest
- "Do NOT continue until every agent has finished" in prompt - ignored when model decides to stop
- Foreground parallel (no run_in_background) - agents run sequentially, not in parallel
None reliably prevent the early exit.
Claude Model
Opus 4.6
Is this a regression?
Yes - parallel Task calls previously blocked until all results were collected (approximately before v2.1.32)
Claude Code Version
2.1.34
Platform
Anthropic API
Operating System
macOS (Darwin 25.2.0, arm64)
Terminal/Shell
Kitty
Related Issues
- #17540 - Session freezes with multiple background agents + TaskOutput (different: freeze vs early exit)
- #14055 - Parallel Task agents intermittently lose output content
- #20236 - TaskOutput hangs after background agent completes
- #23646 - Background read-only agents return empty output
- #7406 - Claude thinks it spawns in parallel but does not (different: our agents DO launch correctly)
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗