[BUG] Critical Bug: Claude Code Spontaneously Executes Commands and Repeats Responses in Infinite Loop
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 Version: v2.1.29
- Model: Claude Sonnet 4.5
- Platform: Windows 11
- API Usage Billing: Yes
- Frequency: 15+ incidents in ~30 days
Bug Description:
Claude Code spontaneously executes commands (like /start-log and agent loading) and repeats its own responses in
infinite loops without any user input, consuming all available context until "context limit reached" error appears.
Steps to Reproduce (From Session Log Analysis):
- Start new Claude Code session
- Type
/start-logto begin session logging - Type "load the engineering agent" to load specialized agent
- Provide detailed project requirements (multi-paragraph description)
- Claude generates detailed response with multiple questions/tasks
- Bug triggers automatically - Claude spontaneously:
- Restarts session without user input
- Types
/start-logcommand without user input - Loads agents without user input
- Repeats previous response
- Cycles through steps multiple times (observed up to 9 repetitions)
- Repetitions happen too fast for user to interrupt
- Context limit is reached from massive token consumption
- Session becomes unusable
Expected Behavior:
- Claude should NEVER execute commands without explicit user input
- Claude should NEVER restart sessions automatically
- Claude should NEVER repeat its own responses
- There should be safeguards preventing infinite loops
Actual Behavior:
Claude autonomously executes commands and repeats responses, consuming 40-50K tokens per incident in repetition loops.
Frequency:
I have experienced this bug more than 15 times in approximately 30 days of use. This is NOT an isolated incident
but a frequent, reproducible issue.
Evidence:
From session log of most recent incident (Feb 1-2, 2026), the log clearly shows:
- Claude's responses
- Then automatic session restarts (without user typing anything)
/start-logcommands appearing (without user typing them)- Agent loading commands (without user typing them)
- Complete response repetitions
Patterns Observed:
The bug appears correlated with:
- Loading specialized agents (particularly "engineering agent")
- Generating lengthy, structured responses
- Creating tasks using the task system
- Possible state corruption in session management
Impact:
- Token waste: ~600K tokens wasted across 15 incidents (~$20 API cost)
- Lost productivity: 15+ sessions rendered completely unusable
- Lost context: Must restart and rebuild context each time
- Reliability: Makes Claude Code unreliable for production work
Workarounds Attempted (None Effective):
- Manual monitoring: Repetitions happen too fast to catch
- Shorter sessions: Doesn't prevent the bug
- Avoiding agents: Limits functionality, doesn't guarantee prevention
System Information:
- OS: Windows 11
- Terminal: PowerShell
- Claude Code installed via: [npm/other - user can fill in]
- Network: VPN environment (mentioned in user config)
Request:
This is a critical stability bug that makes Claude Code unreliable. Please investigate and prioritize a fix. Happy to
provide session logs or additional debugging information if helpful.
Additional Context:
User has been forced to develop workarounds including constant session monitoring and frequent manual session
restarts, which defeats the purpose of using an AI coding assistant. The bug's high frequency (15+ times per month)
makes it a blocker for serious production use.
## Technical Hypothesis (Based on Community Analysis)
This appears to be a Recursive Agent Loop caused by the CLI interpreting
Claude's text output as executable commands:
- Claude generates response containing
/start-logor agent loading syntax
(possibly as part of agent's system prompt instructions)
- CLI parser mistakes this output text for user input
- CLI executes the command, feeding result back to Claude as new prompt
- Claude repeats the pattern, creating infinite loop
- Loop executes faster than human reaction time (~0.1s per cycle)
Similar Pattern: Known in AI agent literature as "Agent Death Spiral"
Windows-Specific Factor: Possible terminal buffer race condition where
stdout is being re-read as stdin
Evidence Supporting This Theory:
- Bug only occurs after Claude generates detailed responses
- Session logs show
/start-logappearing without user keystrokes - Engineering agent most strongly correlated (likely has system prompt
that mentions session initialization)
- Happens too fast for human to generate (automated feedback loop)
## Suggested Fix (For Anthropic Engineers)
- Output Sanitization: Add filter preventing slash commands in model
output from being executed. Only commands typed by user should execute.
- Agent Prompt Audit: Review engineering agent's system prompt for
instructions that might cause it to output executable commands.
- User-in-the-Loop for Critical Commands: Require explicit user
confirmation before executing session management commands like
/start-log, agent loading, etc.
- Loop Detection: Implement safeguard that detects repeated identical
outputs and halts execution automatically.
- Windows Terminal Testing: Investigate stdin/stdout handling on
Windows for echo-back issues.
What Should Happen?
- Claude Code should NEVER execute commands without explicit user input
- Claude Code should NEVER restart sessions automatically
- Claude Code should NEVER repeat its own previous responses
- After generating a response, Claude should wait for the next user input
- There should be safeguards preventing infinite loops
- When context limit approaches, Claude should warn the user and wait for /compact or /clear command
- Session state should remain stable after loading agents and generating detailed responses
Error Messages/Logs
Steps to Reproduce
Note: This bug occurs spontaneously and is not deterministically reproducible, but here are the conditions under which
it has occurred 15+ times:
- Start a new Claude Code session in PowerShell/terminal
- Type
/start-logto begin session logging - Type "load the engineering agent" (or load any specialized agent)
- Provide a detailed, multi-paragraph project request (e.g., describing requirements for building a complex tool)
- Wait for Claude's response
- Claude generates a lengthy response (typically with multiple questions, task creation, or structured plans)
- Bug triggers spontaneously at this point - without any further user input, observe:
- Terminal shows session restarting automatically
/start-logcommand appears (without user typing it)- Agent loading commands appear (without user typing it)
- Previous response repeats verbatim
- Cycle repeats multiple times (observed 1-9 repetitions)
- Happens too fast to interrupt (within seconds)
- Eventually stops on its own (reason unclear)
- If user continues and provides another detailed response, bug may trigger again
- Eventually "Context limit reached" error appears due to massive token consumption from repetitions
Conditions that seem to correlate with bug occurrence:
- Loading specialized agents (particularly "engineering agent")
- Generating responses >1000 words with structured content
- Creating tasks using TaskCreate tool
- Multiple back-and-forth exchanges in same session
Frequency: 15+ occurrences over 30 days of regular use
Reproducibility: Spontaneous/Non-deterministic - cannot force it to happen, but it happens frequently during
normal usage matching the conditions above
Evidence available: Session logs showing the automatic command execution and repetition cycles (available
privately upon request)
Critical Detail: The session log shows /start-log text appearing
in the terminal WITHOUT corresponding keystrokes from user, suggesting
Claude's output text is being parsed as commands.
Claude Model
None
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
v2.1.29
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
PowerShell
Additional Information
_No response_
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗