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.
Tools you need
The 9 steps
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.
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.
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.
Write a failing test
Capture the bug in a test that fails now and should pass when fixed:
npm install -D vitest
npx vitest runAsk the agent to write it from your reproduction steps, then run it and confirm it fails for the right reason.
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.
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 runIf the diff touches files you did not expect, undo it with git restore . and ask again more narrowly.
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.
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>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
| Problem | Fix |
|---|---|
| The AI says it is fixed but the bug is still there | Ask it to show the command it ran and its output. Run the failing test yourself rather than trusting a summary. |
| The fix breaks something else | That 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 pass | Check 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 wrote | Ask 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.












