Workflow recipe
Debug a bug with an AI agent: reproduce it, then fix it
Write the bug report, have the agent reproduce it as a failing test, find the root cause, apply a small fix and prove it.
Bookmark
Bookmarks are kept in your free account, so you can find this page again on any device.
Sign in to bookmarkOn this page
To debug with AI without fooling yourself, write the bug report first, then have the agent reproduce it as a failing test and nothing more. Check the test fails for the reported reason, ask for the root cause with evidence before any code changes, approve the smallest fix, and prove it by running the suite with and without the fix.
The naive version is “here is the bug, fix it”. The agent edits the code, the tests pass, and you cannot tell whether it fixed the cause, patched a symptom, or changed a test until it agreed. Splitting the job into separate prompts, each with its own rules, gives you a checkpoint after every step: a red test you trust, a cause you understand, then a green test you can explain. The rules matter more than the wording. In our run, one prompt with two contradicting rules made the agent pick one and tell us afterwards.
EditorAbout 10 min
1. Write the bug report as evidence
The report is the agent’s only source of truth about what “fixed” means, so write it before you open the agent. Five lines are enough: where you saw it, the steps, what you expected, what happened, and how often.
Bug: the last orders never appear in order history
Seen on: commit 70baa80
Steps: a customer with 45 orders opens order history (20 per page) and clicks Next.
Expected: 3 pages; page 3 shows orders 41 to 45.
Actual: the page shows "Page 2 of 2" with no Next link, and orders 41 to 45 are never shown.
Frequency: every time, for any customer whose order count is not a multiple of 20.Put real numbers in “Expected”. “Should work” gives the agent nothing to assert. Then create a branch for this bug alone:
git switch -c fix/order-history-last-pageClaude CodeAbout 10 min
2. Have the agent reproduce it as a failing test
Ask for a reproduction only. An agent that is allowed to fix in the same turn usually does, and then the “reproducing” test was written against its own fix.
Paste your report where the bracket is, and approve the test command when the agent asks to run it. We got three new tests and the summary line 3 failed, 4 passed: page_count(45, 20) returned 2 instead of 3, and asking for page 3 raised ValueError: page 3 out of range (1-2).
TerminalAbout 5 min
3. Check the test fails for the reported reason
A test can fail for the wrong reason: a typo, a missing import, a fixture that does not exist. Run it yourself and read the failure, not just the count.
git diff --stat # only test files should have changed
python -m pytest -qThe failure message should describe the “Actual” line of your report in code terms. Ours said assert 2 == 3 on the page count, which is “Page 2 of 2”. If the failure is an error in the test itself, send it back. When it is right, commit the tests on their own, so you can later prove the fix against them:
git add tests && git commit -m "Reproduce: last partial page missing from order history"Claude CodeAbout 10 min
4. Ask for the root cause before any change
Switch to plan mode (Shift+Tab until the status bar shows plan mode) so the agent can read and run tests but not edit. Then ask for the cause, every caller of the faulty code, and the side effects of the fix it would propose.
Our answer named one line, return total // per_page, explained all three failures from it, and found a second symptom the report had missed: a customer with 5 orders got zero pages and an error on page 1. It also listed what rounding up would change for empty and exact-multiple histories. Open the line it names and check the explanation before going on. If you disagree with the cause, say why and ask again; do not move to a fix.
Claude CodeAbout 10 min
5. Approve the smallest fix, with limits
Leave plan mode and give the fix its own rules. Keep the rules consistent with each other: when we first told the agent to change only the source file and also to add a test, it had to break one rule, and only said so afterwards.
Fill in the brackets from step 4. We got a one-line change to page_count, one new test and 8 passed. Two runs of this prompt produced two different correct expressions for rounding up, -(-total // per_page) and (total + per_page - 1) // per_page. Pick the one your team finds easier to read; either is fine as long as step 6 passes.
TerminalAbout 10 min
6. Prove the fix, then review the diff
Green tests after a fix show only half of it. Take the fix out and check that the reproducing tests fail again, then put it back:
python -m pytest -q # all pass with the fix
git stash push -- orders/pagination.py # remove only the fix
python -m pytest -q # the reproducing tests fail again
git stash pop # restore the fixWe saw 8 passed, then 4 failed, 4 passed, then 8 passed. Then read git diff main against these red flags:
- Any change to an existing assertion or expected value, or a new
skiporxfail. - Edits outside the file the root cause named.
- A guard that hides the symptom (catching the exception, clamping the page number) instead of correcting the calculation.
- New behaviour for inputs nobody asked about, without a test.
Commit the fix with the root cause in the message, open a pull request, and paste the report and the before-and-after test output into its description.
Check it worked
Verification checklist
0 of 7 done
When it goes wrong
If the agent says it could not run the tests and gives a prediction instead, it was blocked by a permission prompt or a command it was not allowed to run. That happened in our first run. Do not accept a predicted failure: approve the test command, or run it yourself and paste the output back.
If the agent edits application code in the reproduce step, discard the change (git restore on the source files) and paste the prompt again, adding the file it touched to the “do not change” rule. A test written next to its own fix proves nothing.
If the reproducing test passes on the first run, either the bug depends on something the test does not set up (data, time, configuration) or the report is wrong. Go back to step 1 and reproduce it by hand first, then add what you did to the report.
If the fix makes the reproducing tests pass but breaks others, the root cause was probably shared code with other callers. Return to plan mode and ask for the callers list from step 4 again before trying a second fix.
Variations
With Codex, Cursor or another coding agent, use the same four prompts in order. The checkpoints between them (commit the red tests, read the cause, run with and without the fix) do not depend on the agent.
For a bug you can only see in a browser, have the agent reproduce it with an end-to-end test instead of a unit test; the systematic-debugging skill adds a root-cause-first routine, and verification-before-completion stops the agent claiming success without evidence.
For a failure that only happens in CI, start from the logs instead of a report and follow fix failing CI with an AI agent.
Related debugging guides
If the code is new to you, first understand the codebase with AI so you can judge the root cause. Before merging, use the checklist in how to review AI-generated code. The Claude Code tutorial covers permissions and plan mode, and the AI-assisted software engineering path puts these recipes in order. More recipes are in the AI workflow examples hub.
Outcome and tools
- Outcome
- A bug fixed on its own branch, with a test that failed before the fix and passes after it, a named root cause and a diff small enough to review.
- For
- Developers, QA engineers
- Variants for
- Claude Code
- Tools
Check your understanding
Debug a bug with an AI agent quiz
Bug reports as evidence, reproducing before fixing, checking the failure, root cause in plan mode, fix limits and proving the fix.
8 questions · pass mark 70%
Review
Related
- Fix a failing CI run with a coding agent
It reads the GitHub Actions logs, explains the cause and fixes it, with stop rules so it never loops or weakens tests.
- Understand an unfamiliar codebase with an AI agent
Get a read-only map where every claim cites a file and line, check the citations, trace one path and test what you learned.
- Reviewing AI-written code: what to look for
The ways agent-written diffs fail, a practical review checklist, and how to set up the work so review stays manageable.
- Your first session with Claude Code
Install it, run it in a real repository, approve its first changes, commit a reviewed edit and learn the commands you will use daily.
- AI-assisted software engineering
Learn AI-assisted software engineering on real repositories: set up coding agents, give them good instructions, review their diffs and run several in parallel.