[BUG] Opus 4.7 subagents enter generation loop on post-task finalization/return text (work completes, agent doesn't exit)
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?
Environment
- Claude Code: latest
- Model: claude-opus-4-7[1m] (Opus 4.7, 1M context)
- Invocation: Agent tool with subagent_type: "general-purpose", model: "opus", run_in_background: true
Reproduction
Dispatch a Opus 4.7 subagent with a moderately complex multi-step task (e.g., "apply 3 surgical fixes to file X, add ~6 regression tests, run pytest, commit"). Subagent completes the work successfully (verified via git log showing the expected commit landed) but then enters a generation loop emitting the same lines repeatedly. The completion notification never fires because the agent process never exits.
Observed pattern (3 incidents in one session)
┌──────────┬──────────────────────────────────────────────────┬───────────────────────────────────────────────────┐
│ Incident │ Work state │ Loop content │
├──────────┼──────────────────────────────────────────────────┼───────────────────────────────────────────────────┤
│ 1 │ All edits + tests + analysis done │ Repeated Update(test_file.py) calls + same prose │
├──────────┼──────────────────────────────────────────────────┼───────────────────────────────────────────────────┤
│ 2 │ Implementer correctly detected conflict + bailed │ (no loop — model self-recognized) │
├──────────┼──────────────────────────────────────────────────┼───────────────────────────────────────────────────┤
│ 3 │ Commit <SHA> already landed cleanly │ Repeated Update(test_file.py) calls in transcript │
└──────────┴──────────────────────────────────────────────────┴───────────────────────────────────────────────────┘
Mitigation we use
- User watches live transcript for repetition
- Controller kills via TaskStop
- Always git status after kill — work is often 95-100% complete; commit may already be done
- Add explicit "EXIT CLEANLY — no loops" to subagent prompts (works ~50% of time)
Expected
Agent exits with completion notification after final tool call (commit).
Actual
Agent process continues generating repetitive text indefinitely. No notification. Background dispatches wait forever.
Impact
- Wasted model compute on the looping generation
- Controller has to manually poll / be alerted by user
- Background dispatches with run_in_background: true give no feedback path
- The Agent tool has no timeout parameter to bound this
Suggested fix
Either:
- Add agent-side loop detection (same N consecutive identical tool calls or N tokens of identical text → auto-abort)
- Expose a timeout_seconds parameter on the Agent tool so controllers can bound runaway agents
- Surface partial-completion notifications so the controller can git status and salvage
PS: Bug description done and written by Claude Opus 4.7
What Should Happen?
Exit cleanly with no repetitive text
Error Messages/Logs
Repetitive text ..
Steps to Reproduce
It has happened about 4-5 times in my current coding. There is no specific way to reproduce. It just comes.
Claude Model
Opus
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.150
Platform
Anthropic API
Operating System
Other Linux
Terminal/Shell
Xterm
Additional Information
_No response_
This issue has 6 comments on GitHub. Read the full discussion on GitHub ↗