Skip to content
Home/Resources/Build guides/Fix bugs in AI-generated code
Build guide

How to Fix Bugs in AI-Generated Code Without Going in Circles: A Step-by-Step Routine

By Younus FardeenUpdated 3 Nov 20264 min read
Short answer

Commit the current state, reproduce the bug with exact steps, read the error yourself, write a failing test, then ask the AI for the cause before any fix. Make one change at a time and run the test. If two fixes fail, restore the last good commit and start a fresh session with a better prompt instead of piling on corrections.

You will buildA repeatable routine that turns an AI debugging loop into a short, controlled fix with a regression test, using one real bug from your own project.
TimeAbout 1 to 2 hours
Cost₹0
LevelBeginner to intermediate (you have a project with a bug)

Tools you need

The 9 steps

01

Stop and save a checkpoint

Before you try another prompt, record where you are:

git status
git add -A && git commit -m "Checkpoint before debugging login bug"

Now every attempt can be undone. If the bug appeared after a specific change, note which one.

02

Reproduce the bug on purpose

Write the exact steps, what you expected and what happened. A bug you cannot reproduce cannot be fixed, and a vague report such as 'login is broken' gets a vague fix. Include the browser, the user and the data.

03

Read the error yourself first

Open the browser console and the network tab, or the terminal output. Find the first error, not the last, and read the file and line it points to. Even a rough understanding helps you judge the AI's answer.

04

Write a failing test

Capture the bug in a test that fails now and should pass when fixed:

npm install -D vitest
npx vitest run

Ask the agent to write it from your reproduction steps, then run it and confirm it fails for the right reason.

05

Ask for the cause, not the fix

Describe the symptom, the location and what correct looks like, and include the error text:

The streak count resets to 0 when I tick a habit twice in one day.
File: src/lib/streak.js. Expected: it should stay the same.
Explain the likely cause. Do not change any code yet.

Read the explanation and check it against the code.

06

Make one change and run the test

Approve a single, small fix. Run the failing test and then the whole suite, and read the diff:

git diff
npx vitest run

If the diff touches files you did not expect, undo it with git restore . and ask again more narrowly.

07

Reset after two failed fixes

If two fixes have failed, stop correcting. The conversation now holds two wrong attempts that the model keeps re-reading. Restore the checkpoint, start a new session and write one clear prompt that includes what you learned: the exact steps, the error and what did not work.

08

Look at it with a debugger

Add temporary logging at the point the data goes wrong, or set a breakpoint in browser DevTools, and watch the values. If the bug is a regression, find the change that caused it:

git bisect start
git bisect bad
git bisect good <an-older-working-commit>
09

Understand the fix, then lock it in

Ask the agent to explain the cause and the fix in plain words and say it back in your own. Commit the fix together with the test. Add a line to AGENTS.md if the agent keeps making the same mistake, for example 'Always handle the case where a habit is ticked twice a day.'

Make it yours

  • Practise on a deliberately broken copy of a small project.
  • Compare how two different AI tools respond to the same bug report.
  • Keep a bug log: symptom, cause, fix, and what a better first prompt would have been.
  • Fix one bug entirely by hand and compare the time and the result.

Troubleshooting

ProblemFix
The AI says it is fixed but the bug is still thereAsk it to show the command it ran and its output. Run the failing test yourself rather than trusting a summary.
The fix breaks something elseThat is why you commit before each attempt. Restore, then ask for a fix that changes only the named file and runs the full test suite.
The agent edits tests to make them passCheck the diff for changed or deleted tests. Tell it to change code, not tests, and keep your failing test in a separate commit.
I do not understand the code it wroteAsk for a line-by-line explanation, or ask it to rewrite the function more simply. Leave out anything you cannot explain.

Frequently asked questions

Why does AI-generated code get stuck in fix loops?

Each failed attempt stays in the conversation, so the model keeps building on the wrong idea. Restoring a checkpoint and starting a fresh, specific prompt breaks the loop.

Should I paste the whole error to the AI?

Yes, with the file name, the steps to reproduce and what correct looks like. Add what you have already tried so it does not repeat it.

Does debugging with AI make me a worse programmer?

It can. Anthropic's 2026 trial found that people who delegated everything scored lower on a later quiz, with the biggest gap on debugging, while those who asked conceptual questions kept their scores. Use the AI to explain, and fix some bugs yourself.

Which free tools help me debug?

Browser DevTools, Git, Vitest and Playwright. See the free testing and security tools list.

Learn this with mentors

Programmes for builders and engineers with admissions open: live classes, mentors and project work.

Continue learning

About 5 to 8 hours

Build an app with an AI coding agent

Spec, plan and small commits with a free coding agent in VS Code.

Step by step →11 steps
Free tools

Free testing and security tools

Vitest, Playwright, secret scanners, Semgrep and more for AI-written code.

See the list →16 tools
About 2 to 3 hours

Secure a vibe-coded app

A 12-step security pass for AI-built apps, with real tests.

Step by step →12 steps