Debug with AI, step by step.

Reproduce, gather evidence, test hypotheses one at a time, bisect, fix, and lock it in with a test. A prompt for each step.

A small beetle walking across a leaf
Photo by Autumn Guinn on Unsplashdithered by Cyborb

To debug code with AI, give the agent a bug it can reproduce and the evidence around it, then ask for ranked hypotheses instead of a fix. Test one hypothesis at a time, fix the cause once the evidence points to it, and keep the failing test as a regression test.

The method matters more than the model. Ask an agent to “fix this error” and it will often make the error disappear without fixing what caused it. The five steps below turn debugging from a guessing game into a search.

The short version
  • Reproduce the bug first, ideally as a failing test the agent can run.
  • Hand over evidence: the exact error, the full stack trace, the logs and what changed.
  • Ask for ranked hypotheses with a check for each, and test one at a time.
  • If an older version worked, let git bisect find the commit that broke it.
  • Fix the root cause, then keep the reproduction as a regression test.

Why debugging code with AI goes wrong

An agent sees only what you show it and what it chooses to read. With a vague report, it fills the gaps with plausible guesses, and plausible is exactly what makes a wrong fix hard to spot. Developers feel this:

66%
named AI answers that are “almost right, but not quite” among their biggest frustrations
Stack Overflow Developer Survey 2025
45%
said debugging AI-generated code takes more time
Stack Overflow Developer Survey 2025
3%
highly trust the accuracy of AI tools
Stack Overflow Developer Survey 2025

The fix is not to trust less. It is to make the agent work like a good debugger: from a reproduction, with evidence, one change at a time.

Step 1: Reproduce the bug

A bug you cannot trigger on demand is a bug you cannot prove fixed. Before anyone changes code, get a reproduction the agent can run, ideally a failing test.

PromptReproduce first
Before fixing anything, reproduce this bug: [what happens, the steps, and what should happen instead].
Write the smallest failing test or script that shows it, run it, and show me the failure.
If you cannot reproduce it, tell me what information you need. Do not guess at a fix.

Keep the reproduction small. A test that calls one function with one bad input beats a script that clicks through the whole app. If the bug only appears in production, start from the logs and try to rebuild the exact input locally. A wrong number with no error at all is often a bad SQL query; see our guide to writing SQL with AI for the classic traps.

Step 2: Hand over the evidence

The agent can read your code, but it cannot see your screen, your production logs or your memory of what changed last week. Paste those in.

Evidence to give the agent0 of 6

A few commands answer “what changed” quickly, and the agent can run them itself:

Terminal
# Recent commits, and what changed since the last good release
git log --oneline -15
git diff v2.3.0 --stat

# Dependency changes hide in the lockfile
git diff v2.3.0 -- package-lock.json | head -50

Step 3: Test one hypothesis at a time

This is where agents need the firmest hand. Ask for hypotheses, not a fix:

PromptRank the hypotheses
Here is the failing test and the evidence: [paste].
List up to three hypotheses for the root cause, most likely first. For each, give the evidence for and against it, and the cheapest check that would confirm or rule it out: a log line, an assertion or a small test.
Run the check for the first hypothesis only, then report back before changing any code.

When the evidence is thin, add some. Ask the agent to log the values that matter for the failing input, and to mark each temporary line with a tag such as DEBUG so they are easy to remove later.

Change one thing per run. If the agent edits three files and the bug disappears, you do not know which change fixed it, or what else the other two broke.

When an answer sounds certain, ask where it comes from. Models state guesses and facts in the same confident tone, which our guide on why AI makes things up explains.

Step 4: Bisect when an older version worked

If the bug is new, the fastest route to the cause is often the commit that introduced it. git bisect runs a binary search through your history, so even a thousand commits take about ten tests. It is one of several git commands worth knowing well; our guide to git for beginners covers eight of them.

Point it at a known bad and a known good version, then let your reproduction test do the judging:

Terminal
# Mark the bad version (HEAD) and the last known good one
git bisect start HEAD v2.3.0

# Test each midpoint automatically with the reproduction test
git bisect run npx vitest run tests/checkout-total.test.ts

# When it names the first bad commit, go back to where you started
git bisect reset

Two details make this work. Leave the new test uncommitted, so it stays in place while bisect checks out older commits. And know the exit codes: 0 means good, 125 means skip this commit because it cannot be tested, and most other failures mean bad.

Once bisect names the commit, give its diff to the agent. “Here is the commit that broke it” is the strongest evidence you can hand over.

Step 5: Fix the cause and keep the test

With the cause confirmed, the fix is usually small. Ask for it in a way that rules out the common shortcuts:

PromptFix the root cause
The cause is confirmed: [cause].
Fix the cause, not the symptom. Do not add a try and catch that hides the error, do not weaken any check, and do not edit the failing test.
Keep the reproduction as a regression test. Run the full test suite and show me the result, then remove every temporary DEBUG line.

Then check the test itself. It should fail on the old code and pass on the new code. A regression test that passes either way proves nothing. Our guide to test-driven development with AI shows how to lock tests so an agent cannot quietly edit them.

Traps: confident guesses and shotgun fixes

Bad AI debugging sessions tend to fall into a few recognizable traps. Each one has a simple counter.

TrapWhat it looks likeWhat to do instead
Confident guess“The issue is clearly the cache,” with no evidenceAsk for the evidence and a check for each claim
Shotgun fixFive edits at once, and the error goes awayOne hypothesis, one change, one run
Symptom fixA new try and catch, null check or retry that hides the errorAsk why the value was wrong in the first place
Edited testThe assertion now matches the buggy outputDiff the test files before you accept the fix
Invented APIA fix that calls a function your version does not haveCheck the docs for the version you have installed
The loopThe same failed fix, offered againStart a fresh session with a summary of what you ruled out

The last one deserves emphasis. After two failed corrections, a long thread works against you. Write down what you know and what you ruled out, then start clean. The copyable prompts in our prompts for coding agents are a good starting point.

FAQ

Can AI find bugs in my code?

Often, yes, especially with a reproduction and good evidence. It is weaker on bugs that depend on timing, load or data it cannot see, such as a race between two requests or a bad row in production.

What should I paste when I ask AI to debug?

The exact error, the full stack trace, the input that triggers it, expected versus actual behavior, what changed recently and what you already ruled out. Redact secrets and personal data first.

Why does the AI keep suggesting the same wrong fix?

The conversation is anchored on its earlier attempts. Start a fresh session with a short summary of the bug, the evidence and the hypotheses you have ruled out.

Should I let the agent run commands while debugging?

Yes, on a branch and with approval for anything destructive. Running the reproduction, the tests and small experiments itself is where an agent saves you the most time.

Next, learn to spot almost-right code in review, or set up an AI pair programming workflow that catches bugs before they ship.

Sources
  1. 2025 Developer Survey: AI, Stack Overflow, July 2025
  2. git-bisect documentation, Git
  3. Best practices for Claude Code, Anthropic
cyborb.ai

Stop reading about it. Build it.

Describe what you want in plain words. Cyborb plans the work, writes and runs the code, makes the assets, and puts the result online.

Download Cyborb

Free to start. No card required.