← Back to BlogAgentic Coding

Why Claude Code Keeps Fixing the Same Bug

Claude Code stuck in a loop fixing the same bug and burning tokens? How loops start, a symptom table, and six ways to break out.

When Claude Code gets stuck in a loop fixing the same bug, more rounds will not end it. The loop usually runs on a wrong design or on a failure nobody can observe, so the way out is to stop, reproduce the bug with a failing test, make Claude state a hypothesis before it edits, and re-plan from a clean context after two or three failed rounds.

Rounds are also where the tokens go. Each round typically means more reading, editing and summarizing, and a loop pays that bill again and again for work that never lands. Disclosure: we build TLM Forge, a process layer for Claude Code, so weigh the product section near the end accordingly. The fixes below work without it.

What a Claude Code fix loop looks like

Developers describe the same pattern in different words. A Hacker News commenter running a semi-autonomous setup, where Claude writes code and a second model reviews it, reported what should have been a trivial feature going for 13 rounds of review and fix before they stopped it, with “each round was just flip-flopping the same logic back and forth to try to make the tests pass.” The same commenter said the reviewer (Codex) kept re-identifying the issues Claude was flip-flopping on, and that they had not seen anything that bad on an earlier model version. That is one person's account of one setup, not a measurement.

Sources: cjk on Hacker News

The GitHub tracker holds the same shape from other angles. One reporter describes 13 rounds of fixes for one class of bug across seven days, in separate sessions, where each round ended with Claude saying it was fixed after checking that the code had changed, not that the live output was right. Another says a session of three or more hours went on fixing two failing tests and rule violations that they attribute to earlier sessions, with no new features delivered. Two shorter reports describe Claude repeating the same mistakes or going in circles while using up tokens. Treat each as one user's report, not a measurement or a finding about how often this happens.

Sources: anthropics/claude-code #80950, anthropics/claude-code #71618, anthropics/claude-code #75948, anthropics/claude-code #6095

Anthropic's own engineers have seen it. In a write-up of how the Claude Code team reviews code, they note that “An agent author and an agent reviewer can keep each other busy indefinitely,” and that one CLI pull request went through more than fifty rounds of review and fixes overnight before its author stopped it. Among the changes they describe is a round cap rather than a better prompt: after three rounds, the reviewer asks the author to step back and reconsider the approach. That is their rule for pull request review, not for debugging sessions. More on that below, and in our breakdown of How Anthropic Does Code Review on 90% AI-Written Claude Code.

Sources: How the Claude Code team does code review (claude.dev, Oct 15, 2026)

How the loop starts

These are four common starting conditions, our own grouping rather than a formal taxonomy. The symptom table later in the post covers a few more patterns. Knowing which one you are in helps you pick a fix.

1. The design was wrong from the first edit

If the first implementation rests on a bad assumption, every later fix is a patch on the wrong structure. A patch for case A breaks case B, the patch for B breaks A, and the code swings between them. This is the flip-flop. No amount of effort at the line level helps, because the line is not where the problem sits. The Claude Code docs say as much about planning: plan mode lets Claude explore and “proposes an approach for your approval, preventing expensive re-work when the initial direction is wrong.”

Sources: Claude Code docs: Manage costs effectively

2. There is no failing test that shows the bug

Without an observable failure, Claude has only its own reading of the code to judge success. The best practices guide puts it directly: Claude stops when the work looks done, and without a check it can run, “looks done” is the only signal available. You then become the verification loop, and every round costs you a manual check plus another full turn. The docs' own example of a good bug prompt ends with “write a failing test that reproduces the issue, then fix it.”

Sources: Claude Code docs: Best practices

3. The check passes but does not measure the bug

A green test suite that never exercised the broken path tells Claude it is finished. In the 13-round GitHub report, where each round was a separate session, the reporter's complaint was that Claude checked that code changed and merged, then called it fixed without looking at the real output. We cover that failure in detail in Claude Code Said It's Done. It Wasn't.. In a loop like that, each false "done" triggers another round.

Sources: anthropics/claude-code #80950

4. The context fills up with failed attempts

The docs warn that if you correct Claude more than twice on the same issue in a session, the context is cluttered with failed approaches. Their prescribed fix is blunt: “After two failed corrections, /clear and write a better initial prompt incorporating what you learned.” Our post on context management for AI coding covers context hygiene, so we will not repeat it. The point here is narrower: a loop is both a symptom of a bad context and a cause of one.

Sources: Claude Code docs: Best practices

Why loops cost so much more than they look

Much of the cost of a loop is rework on a build that was never right, not any single expensive turn. Per the Claude Code cost docs, Claude Code sends your full conversation with every request, and each time Claude uses tools it sends another request carrying that batch of tool results. So a long loop pays for the accumulated history on every pass, and the history now includes every failed attempt. The cache lowers the rate at which that history is billed. It does not make the rounds disappear.

Sources: Claude Code docs: Why usage climbs in a long session

Part of the cost advice centres on hygiene: start fresh sessions, watch your usage, tune caching. That advice is sound and it trims the per-round price. It does little about the number of rounds. The plan mode and verification advice in the same docs is what targets rounds. If a wrong design needs thirteen patches, trimming the price of each round leaves all thirteen in place. We are not putting a dollar figure on it, because it varies with the task, the model and the plan. The mechanism is what matters: every round that builds on a wrong foundation is paid for twice, once to write it and once to unwrite it.

There is a quality cost as well. SlopCodeBench, a research benchmark in which coding agents repeatedly extend their own earlier solutions, reports that structural erosion and verbosity rose in most trajectories as the agents kept extending their own code. The benchmark measures extending code for new requirements, not fixing bugs, so a fix loop is only an analogy: in both, the agent keeps building on a structure it did not get right the first time.

Sources: SlopCodeBench (arXiv 2603.24755)

Loop symptoms, causes and fixes

Loop symptomLikely causeBreak it
The same logic flips back and forth between roundsTwo requirements conflict and the design has no answer for bothStop editing. Ask Claude to list both requirements and propose one design that satisfies them, then approve it before any code changes
Claude says fixed, the bug is still thereNo check that exercises the real failureWrite a failing test or a command that reproduces the bug first, and require its output as evidence
Each fix breaks something that workedEdits made without understanding what else depends on the codeAsk which other callers and tests are affected before the edit, and run the full suite after each round
Claude ignores a suggested fix and tries its own againContext is full of failed attempts that anchor the next oneClear the session, restate the problem with what you learned, and put the rejected approaches in the prompt
Review finds new issues every roundThe author and the reviewer disagree about the target, or the design is unsoundCap the rounds at two or three, then re-plan. Review only the diff after the first few passes
The change keeps growing and nobody can say whyScope drifted; each fix added a workaroundRevert to the last good commit, shrink the task, and rebuild from the written design

Six ways to break the loop

1. Reproduce the bug with a failing test before any fix

Ask for the test first and watch it fail for the right reason. A test that fails on the real symptom gives Claude and you the same fixed target. A test that passes before the fix is a warning: it does not measure the bug, and the loop would have kept going. The best practices guide tells you to describe the symptom, the likely location and what fixed looks like, and its example prompt adds the failing test. For bugs that only appear in a live system, the reproduction can be a command and its expected output rather than a unit test.

Sources: Claude Code docs: Best practices

2. Set a round limit and re-plan when you hit it

Pick a number before you start. Anthropic's pull request review process uses three rounds before asking the author to reconsider the approach. The docs advise clearing and rewriting the prompt after two failed corrections. Our rule of thumb from both: if round four on the same bug is coming, the plan is probably wrong, not the code. Stop, state what was learned, and design again.

Sources: How the Claude Code team does code review, Claude Code docs: Best practices

3. Ask for the hypothesis before the edit

A loop edits first and explains later. Reverse it. Before Claude touches a file, ask for a short written hypothesis: what is causing the failure, what evidence supports it, which line it will change and what result the test should show afterwards. If the hypothesis repeats an earlier one, you have caught the loop before it spent another round. If it names evidence that does not exist, you have caught a guess.

4. Diff between rounds

Look at what changed from round to round, not just the final state. If round six undoes round four, the loop is a flip-flop and the real issue is a conflict between two requirements. Git makes this cheap: commit each attempt locally, then compare. Anthropic's review process also moved to diff-only re-review after the first few passes and asks for the class of bug to be flagged, so the author fixes every instance and not one line. Checkpoints and rewind help here too, but they have limits we describe in What Claude Code Rewind Cannot Undo.

Sources: How the Claude Code team does code review

5. Change one variable at a time

A round that changes the logic, the test and a dependency at once teaches nobody anything, because a pass or fail cannot be pinned on one cause. Hold the test fixed and change the code, or hold the code fixed and change the input. The docs make a related point about verification: write one file, test it, then continue. Small steps keep each result readable.

Sources: Claude Code docs: Manage costs effectively

6. Escalate to a fresh context or an independent reviewer

The author of a failing fix is the worst judge of it. The best practices guide notes that a fresh context improves code review since Claude will not be biased toward code it just wrote, and it describes a writer and reviewer pattern in separate sessions. Give the reviewer the diff and the criteria, not the history of attempts. One caution from the same guide: a reviewer told to find gaps will usually report some even when the work is sound, so ask it to flag only gaps that affect correctness or the stated requirements.

Sources: Claude Code docs: Best practices

Pro Tip

A reusable break-the-loop prompt: “Stop editing. List every approach tried so far and why each failed. State one hypothesis for the root cause and the evidence for it. Name the test that will fail before the fix and pass after. Do not change any file until I reply.”

When not to add process

Not every bug needs a design review. The best practices guide says that if you could describe the diff in one sentence, skip the plan, and it lists typos, a log line and a rename as direct tasks. Save the heavier steps for changes that touch several files or that you do not fully understand. The wider cost of agents producing code faster than teams can finish and ship it is a separate problem, covered in Study: AI Agents Ship More Code, Not More Finished Work.

Sources: Claude Code docs: Best practices

Where TLM Forge fits

TLM Forge targets the front of the loop. It sizes each task as Light, Medium or Deep. On Medium and Deep work it shows a design for your OK before building, so you can read it before code is written, and it tells Claude to build tests first so the failure is observable (tests first, one small phase at a time, with proof saved). A final review wave of independent reviewer agents then looks at the change before commit.

The limits are real. Tests-first is an instruction to Claude, and no hook checks the test results. The reviewer agents use tokens too, and we give no savings figure. Light tasks skip the review agents, since sizing exists so small work does not carry heavy process (checks big and risky changes harder than small ones). A reviewer can also miss things. None of this replaces your own round limit.

Frequently asked questions

01Why does Claude Code keep making the same mistake?

Usually the context holds failed attempts that steer the next one, or nothing observable tells Claude the fix failed. The docs advise that after two failed corrections you clear the session and write a better prompt with what you learned. Add a failing test so the failure is visible.

02How do I stop Claude Code from burning through tokens?

Reduce rounds, not only the price per round. Reproduce the bug with a failing test, approve a design before building, and stop after two or three failed attempts to re-plan. The official cost guidance also recommends plan mode and verification targets to avoid expensive re-work.

03How many fix attempts should I allow before stopping?

The Claude Code docs suggest clearing after two failed corrections. Anthropic's code review process asks the author to step back after three review rounds. Pick a number before you start, and treat reaching it as a signal that the design needs rethinking.

04Should I start a new session when Claude is looping?

Yes, once you have learned something. Clear the session, then write a new prompt that states the symptom, the failed approaches and the test that must pass. The docs say a clean session with a better prompt almost always outperforms a long one with accumulated corrections.

05Can a second AI reviewer fix a fix loop?

It helps when it works from a fresh context with only the diff and the criteria. It can also join the loop: Anthropic's engineers saw an author agent and a reviewer agent keep each other busy for more than fifty rounds. Combine a reviewer with a round cap.

Ship AI-written code you can trust

TLM Forge is the missing process layer for Claude Code: a spec audit, independent multi-agent review, enforced TDD, and an adversarial red-team gate.

Get TLM Forge