Background agents (run_in_background: true) terminate at ~20–30 tool uses with status: completed but mid-thought results, no signal for graceful wrap-up
Summary
When dispatching subagents via the Agent tool with run_in_background: true, the agents are returning status: "completed" after ~20–30 tool uses, with their result field containing a sentence fragment that is clearly mid-work (e.g., "Let me verify:", "Now run the unit tests"). The agents have not actually finished the task they were given — they were terminated by the runtime.
There is no observable mechanism by which the agent can detect "approaching termination" and emit a structured wrap-up message (e.g., commit partial work, leave a note describing what remains).
Reproduction
- Dispatch a subagent (
general-purpose, modelsonnetorhaiku) withisolation: "worktree"andrun_in_background: true. - Hand it a multi-step task whose natural completion would require ~30–50 tool uses (read several files, write tests, write implementation, run tests, write documentation, commit, push, open PR).
- Wait for completion notification.
- Inspect the returned
resultandusage.tool_usescount.
Observed across 5 dispatches in one session (different tasks, same pattern):
| Model | tool_uses | duration_ms | result snippet (visible mid-thought termination) |
|--------|-----------|-------------|--------------------------------------------------|
| sonnet | 29 | 189963 | "...Let me implement now. First, let me also check what \<helper\> returns to understand its mock interface:" |
| sonnet | 30 | 325535 | "...My changes are currently in the worktree. Let me verify:" |
| sonnet | 22 | 146084 | "Good. Now write the second document:" |
| sonnet | 21 | 179851 | "Now run the validator unit tests..." |
| haiku | 18 | 75270 | "Let me look at that method:" |
Expected behavior
Either:
- (a) Document the cap explicitly and emit a system reminder to the agent at, say, 80% of cap utilization so the agent can wrap-up: commit partial work, write a status note, return a structured
INCOMPLETEresult with a clear list of what remains; or - (b) Provide a parent-side mechanism to extend an agent's budget mid-flight (the
Agenttool docstring references "use SendMessage with the agent's ID" — but that tool is not available in this runtime, see separate issue).
Actual behavior
- No prior signal; the agent is killed mid-tool-use chain
statusis reported ascompleted(misleading — implies success), nottruncatedorterminated- The parent has to infer the termination from the mid-thought
resultsnippet - Recovery requires relaunching with the partial work salvaged manually from the filesystem (which only works if the agent happened to write something to disk before termination)
Impact
For any non-trivial multi-step task (write tests + implement + verify + commit + PR), the success rate is effectively zero. Workaround is to split each task into 3+ micro-tasks of < 15 tool uses each, but that adds dispatch overhead and loses the benefit of giving an agent the full context to make local decisions.
Environment
- Claude Code CLI
- Linux (WSL2)
- Subagents dispatched via
Agenttool withsubagent_type: general-purposeorExplore,run_in_background: true,isolation: "worktree"
Suggested next step
Confirm whether the cap is intentional (turn limit, token budget, time limit?) and document it. If intentional: add a near-cap signal so agents can emit structured incomplete results.
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗