AI Coding Agent Stuck in a Loop? How to Break the Fix-Break-Fix Cycle
The agent says it fixed the bug. It didn't. You say "still broken", it tries again, and now two things are broken. Every failed attempt stays in the conversation, so each retry re-reads the wrong approaches. Another "try again" rarely helps. Change what the agent knows instead.
The steps below were checked on 2026-11-02 against Anthropic's Claude Code best practices and checkpointing docs, Cursor's Agent docs on checkpoints and OpenAI's Codex best practices. These tools change often, so re-check anything you depend on.
1. Stop after the second failed correction
Anthropic's guide gives a concrete rule: "after two failed corrections run /clear and write a better prompt instead of a third." Its reasoning is that a third correction "mostly adds to a conversation that already contains two failed attempts for Claude to keep re-reading."
Use the same rule in any tool. In Claude Code, Esc stops Claude mid-action and keeps what it has done so far. In Cursor, stop the run from the chat panel. Don't type anything else into that thread yet.
2. Roll the code back to the last version that worked
Each failed fix may have left edits behind. Undo them before you try again, or the next attempt builds on a broken base.
| Tool | How to roll back | Watch out for |
|---|---|---|
| Claude Code | /rewind, or Esc twice on an empty prompt, then Restore code or Restore code and conversation |
Files changed only by Bash commands aren't tracked, and most subagent edits aren't restored. Use Git for those. |
| Cursor | Click a checkpoint in the chat timeline, or Restore Checkpoint on an earlier request | Restoring reverts files only; the messages stay in the conversation. |
| Codex, Copilot, anything else | git restore <file>, git stash, or reset to a commit you trust |
Commit before you hand the agent a risky task, so there is always a known-good point. |
Both Anthropic and Cursor say their checkpoints are not a replacement for Git. Cursor's docs: "Checkpoints are stored locally and separate from Git."
3. Start a clean conversation
A fresh context starts from what you know, not from what went wrong.
- Claude Code:
/clearstarts over. If you need the old thread later, the rewind menu offers/resume (previous session)until you exit. - Cursor: open a new chat. A checkpoint restore alone leaves the failed attempts in the thread.
- Codex: start a new thread or session.
4. Write the new prompt with a "done when" check
The new prompt should hold everything the old thread taught you, in a few lines. Name the file, describe the symptom, and say what "fixed" looks like. Codex's guide puts the last part as: "Done when: What should be true before the task is complete, such as tests passing, behavior changing, or a bug no longer reproducing?"
Copy this restart prompt and fill it in:
Goal: <what should happen, one sentence>
Where: <exact file paths or component names>
What I see: <exact error text or observed behavior, word for word>
Already tried (don't repeat): <the fixes that failed>
Leave alone: <files or behavior that already work>
Done when: <a command you can run yourself, e.g. "npm test -- auth passes">
First write a failing test that reproduces the bug, then fix the root cause.
Don't suppress the error or edit the test to make it pass.
Show me the command you ran and its output.
The "already tried" line matters most. Without it, the agent offers the same failed fix again in new words.
5. Give the agent a check it can run, then ask for proof
"Make sure it works" isn't a check. "Run pytest tests/test_login.py and stop when it passes" is. A good check is a command whose result the agent can read on its own.
Then ask for evidence rather than a summary. Anthropic's guide: ask for "the command it ran and its output, or the screenshot. 'All tests pass' on its own tells you very little." If the agent writes the test as well as the fix, read the test before you trust a green result.
6. Fix the blocker yourself when it isn't code
Some loops are about the environment: a missing package, a wrong Node version, a command that needs a login. The agent will keep rewriting code around them. Run the failing command yourself. If it fails for you too, fix that first (install it, switch versions, set the variable), then hand the task back.
7. Check whether your rules file is causing it
If the agent keeps choosing the same wrong approach across fresh chats, look at your instructions. A rule in AGENTS.md, CLAUDE.md or .cursor/rules might contradict what you're asking for, or be too vague to follow.
A quick test is to move the file aside for one session and retry the task. If the loop disappears, add rules back one at a time to find the one that interferes. Our troubleshooting guides cover the common causes: AGENTS.md not working, CLAUDE.md not working and Cursor rules not working.
8. Turn the lesson into a rule so it doesn't come back
OpenAI's advice for Codex works everywhere: "When Codex makes the same mistake twice, ask it for a retrospective and update AGENTS.md." Ask the agent what went wrong and what single rule would have prevented it, then add that line where your tool reads it:
## Lessons
- Run `pnpm test --filter auth` after any change in src/auth/. Don't mark a task done until it passes.
- Token refresh lives in src/auth/refresh.ts. Don't add a second refresh path in the API client.
Keep each lesson short and specific. Codex's guide says "A short, accurate AGENTS.md is more useful than a long file full of vague rules."
The short version
- Two failed corrections means stop.
- Roll the code back (checkpoint or Git).
- Start a clean chat.
- Restate the goal, the error, what was tried and a "done when" check.
- Ask for the command output, not a promise.
- Fix environment blockers yourself.
- Rule out a conflicting rules file.
- Write the lesson into
AGENTS.mdorCLAUDE.md.
Want a rules file with your build and test commands written down, so the agent always has a check to run? The free AGENTS.md generator builds one in a minute. The AI Coding Agent Config Kit has AGENTS.md templates for 12 stacks, plus Claude Code, Cursor and Copilot files and a prompt that checks your verify commands.
AI Coding Agent Config Kit
Instruction-file templates for 12 stacks and 12 prompts that write and audit them. 34-page PDF, $14.
Not ready to buy? Start with the free PDF.
Get 10 of our most-used prompts as a free PDF from Gumroad. You will be asked for your email so Gumroad can deliver it.