StepGleam logoStepGleam
Coding guide

Debug with an agent: reproduce, hypothesise, log, then fix

Don't just ask the agent to fix it. Make it reproduce the bug, state a testable hypothesis, prove the root cause with logging or a failing test, and only then change code.

~30 minIntermediate 76/100 5 steps Updated Oct 26, 2026
Claude CodeGit
Debug with an agent: reproduce, hypothesise, log, then fix — illustration for this Coding guide

Tools used

  • Claude CodeReproduces, instruments and fixes the bug
  • GitKeeps experiments reversible
Step navigator0/5 done
0 of 5 steps done
What you'll get

A fix backed by a reproduction and a regression test, not just the agent saying it is fixed.

Who this is for

Developers who have watched an agent "fix" the same bug three times.

Read this first

Before you start

One r/ClaudeCode developer calls it the "Confidently Wrong" problem: describe a bug, the agent scans nearby code, changes something and declares it fixed. Their answer is a debugging subagent that follows the scientific method. Anthropic's guide describes the same shape for bug prompts.

Step-by-step instructions

Step 1 of 5 · Coding

Give the symptom, the likely location and what fixed looks like

Anthropic's example: "users report that login fails after session timeout. check the auth flow in src/auth/, especially token refresh. write a failing test that reproduces the issue, then fix it." The docs also say to give the command that reproduces it and whether it's intermittent.

Copy-ready promptClaude Code
Users report login fails after session timeout. Check src/auth/, especially token refresh. Reproduce it with a failing test first. Don't change source code until the test fails for this reason.
Debug with an agent: reproduce, hypothesise, log, then fix — guide thumbnail
Step 2 of 5 · Coding

Make it demonstrably reproduce the problem

The debugging subagent's first rule is that it "Demonstrably reproduces the problem". A failing test or a repro command it can run counts; a plausible explanation doesn't.

Step 3 of 5 · Coding

Ask for a testable hypothesis and targeted logging

Next it "Forms a testable hypothesis", "Designs an experiment using precise logging to test the hypothesis", and analyses the output "to validate, invalidate, or update the hypothesis." Another user simply puts the word "hypothesis" in their prompts.

Why this works

Logs turn guesses into evidence.

Step 4 of 5 · Coding

Only fix once the root cause is proven

"Only when the agent has proven the root cause is it allowed to attempt a fix." Anthropic adds: "address the root cause, don't suppress the error."

Step 5 of 5 · Coding

Clear the session after two failed corrections

Anthropic's rule of thumb: "If you've corrected Claude more than twice on the same issue in one session, the context is cluttered with failed approaches. Run /clear and start fresh" with a better prompt.

End of guide · Reviewed Oct 26, 2026

Pro tips2
  • Save the process as a subagent in .claude/agents/ so every bug gets it.
  • Keep the reproduction test as a regression test.
Common mistakes3
  • "Fix the login bug" with no reproduction.
  • Accepting "it's fixed" without re-running the reproduction.
  • Stacking patch on patch in one long session.
FAQ2
Which tests work best?
The subagent author uses end-to-end tests first, because functional-level bugs show up there.
Do I need a subagent?
No. The same steps work as a prompt; a subagent makes them repeatable.