← Back to Blog

AI Coding Cost Per Debugging Session: How Many Iterations Before You Should Rewrite From Scratch?

By Eric Bush · July 3, 2026 · 8 min read

A developer analyzing bug reports and code on a workstation with debugging tools open

The Debugging Cost Curve Nobody Talks About

Everyone who uses AI coding agents has hit this pattern: you ask Claude or GPT to fix a bug, the fix does not work, you push back, the model tries again, still wrong, third attempt, fourth. The token bill for a stubborn debugging session can exceed the cost of writing the whole function from scratch — sometimes by 5x or 10x.

There is a mathematical point where continuing to iterate is more expensive than starting over. This post is the framework for finding it.

How Debugging Iterations Compound Cost

Each debugging iteration adds three things:

  1. The failed attempt's tokens. Input plus output for the wrong fix — usually 5K–15K tokens per iteration.
  2. Context growth. The failed attempt goes into context, plus your pushback, plus any new error messages. By iteration 5, context is often 4–5x the size of iteration 1.
  3. Developer time. Reading the wrong fix, testing it, formulating pushback — this is the hidden expense that outweighs the token cost.

Modeling a Typical Debugging Session

Consider a moderately complex bug in a 500-line file. Using Claude Opus 4.8:

Iteration Input tokens Output tokens Cumulative cost Cumulative dev time
1 8,000 1,200 $0.04 5 min
2 12,000 1,500 $0.10 12 min
3 17,000 1,800 $0.18 22 min
4 23,000 2,000 $0.29 35 min
5 30,000 2,200 $0.42 50 min
6 38,000 2,400 $0.57 67 min

By iteration 6, you have burned $0.57 in tokens and 67 minutes of your time. Meanwhile, rewriting the function from scratch (with a fresh context) usually costs about $0.15 in tokens and 15 minutes of developer time.

The Rewrite Break-Even

From the data across hundreds of debugging sessions, the pattern is consistent:

  • Iterations 1–2: High probability of resolution. Continue.
  • Iterations 3–4: Resolution probability drops but stays reasonable. Continue if the pushback quality is improving.
  • Iteration 5: Rewrite break-even point. If you have not fixed it in five iterations, the expected cost of continuing exceeds the cost of rewriting.
  • Iteration 6+: You are almost certainly locked in a broken loop. Abandon.

Why 5 Iterations Is the Threshold

Three reasons the number consistently lands at 5:

  1. Context pollution. By iteration 5, the model has seen 4 wrong answers. Every wrong answer subtly biases the next attempt. Reset the context and half the time the model gets it right on attempt 1.
  2. The problem is likely misspecified. If a competent model missed the fix 4 times, your description of the bug is probably missing key context. Rewriting forces you to re-specify.
  3. Sunk cost fallacy. Continuing feels productive because you have already invested. It is not. Cost is forward-looking.

Signals to Abandon Sooner

Do not wait for iteration 5 if you see any of these:

  • The model repeats the same wrong pattern. If iteration 3 tries the same approach as iteration 2 with minor variations, the model does not have the information to solve the problem. More iterations will not help.
  • Error messages are contradictory. The bug is deeper than you thought. Step back and investigate manually.
  • The model asks for information you already provided. Context is polluted. Reset.
  • You are running Opus and it still fails. If the strongest model available cannot solve it, no model will. Rewrite or investigate manually.

The Rewrite Pattern That Works

When you decide to rewrite, do not just start over. Do this:

  1. Fresh context. New chat session, no history of the failed attempts.
  2. Rewrite the specification. What is the input, output, and constraints? If you cannot state this in three sentences, the bug will not be fixed by rewriting either.
  3. Provide reference implementations. If a similar function exists elsewhere in the codebase, include it.
  4. Ask for tests first. Have the model write tests before writing the function. The test cases surface the specification.

Rewrite vs Investigate: A Related Decision

Sometimes the answer is not "rewrite the function" but "investigate the environment." If the same code works on your laptop but fails in CI, no amount of AI iteration will fix it — the problem is external. Signs the bug is environmental:

  • Errors mention specific system dependencies, versions, or paths.
  • The bug is intermittent.
  • Different developers reproduce it differently.
  • The fix seems obviously right but does not resolve the symptom.

In those cases, abandon the AI debugging loop and spend 10 minutes with logs, environment diffs, and manual reproduction steps.

Recommendation

  • Set a mental budget of 5 iterations for AI debugging. When you hit 5, rewrite or investigate manually.
  • Track your typical break-even. Some teams find their number is 3 (highly specialized codebases), some find 7 (very clean, well-typed code). Adjust from the 5 baseline based on your observed pattern.
  • Do not just count tokens — count minutes. Developer time is 10x more expensive than the token bill for most teams.
  • When you rewrite, use tests-first prompting. The test cases eliminate the specification ambiguity that caused the original bug.

Want to calculate exact costs for your project?

Frequently Asked Questions

How many AI debugging iterations should I try before rewriting?

5 is the baseline threshold. If you have not fixed a bug in 5 iterations of Claude, GPT, or another frontier model, rewriting from a fresh context is faster and cheaper than continuing. Adjust based on your codebase — some teams find their number is 3, others 7.

How much does a stubborn debugging session cost?

By iteration 6, tokens alone reach $0.50–$1.00 on Claude Opus. Developer time typically hits 60–90 minutes. Rewriting from scratch is usually $0.10–$0.20 and 15–20 minutes, so the break-even happens around iteration 5.

What causes AI debugging loops to fail?

Three main causes: context pollution (previous wrong answers bias new attempts), misspecified problem (the bug description is missing key info), and environmental issues that no code change can fix.

How should I structure a rewrite after abandoning a debug session?

Fresh chat session with no failed-attempt history. Rewrite the specification in three sentences. Include reference implementations of similar functions. Ask the model to write tests first — the test cases surface the real specification.

When should I skip the AI loop entirely and debug manually?

When errors mention specific versions or paths, when the bug is intermittent, when different developers reproduce it differently, or when your fixes seem obviously correct but do not resolve the symptom. These are environmental problems no model can solve.