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.

Tools used
- Claude CodeReproduces, instruments and fixes the bug
- GitKeeps experiments reversible
Step navigator
A fix backed by a reproduction and a regression test, not just the agent saying it is fixed.
Developers who have watched an agent "fix" the same bug three times.
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
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.
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.

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.
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.
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."
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.


