promptgarten ๐ŸŒฑ
๐ŸŒ ES
Guideโ—โ—โ—‹5 min ยท +40 XP

Debugging with AI Agents: Systematic, Not Guesswork

Agents debug best with a reproducible error, real logs, and a hypothesis before the fix โ€“ not with "fix the bug".

The most common mistake

Many people give an agent nothing but "fix the bug" โ€“ no error message, no context. Then the agent guesses. Systematic debugging works differently.

Step 1: Reproduce the error

Give the agent the command that triggers the error and let it trigger the failure itself. The Claude Code docs recommend exactly this: tell the agent how to reproduce the issue and get a stack trace. An error the agent can trigger itself is also one it can verify itself after the fix.

Step 2: Feed logs and stack traces

Paste the complete error message instead of describing it. Piping works too: cat error.log | claude -p "explain the root cause". Also say whether the error happens every time or only sometimes.

Step 3: Hypothesis before fix

Have the agent first name probable causes, ranked by likelihood โ€“ then test one deliberately. Important: fix the cause, not the symptom. The official docs phrase it as a prompt rule: "address the root cause, don't suppress the error".

Diagnostic tools

For problems with Claude Code itself there is /doctor: an automated check of your installation, settings, extensions, and context usage. If claude won't start at all, run claude doctor directly from your shell.

EXAMPLE

Prompt: "npm test fails. Here is the complete stack trace: [paste]. Reproduce the error, name the most likely cause, and write a failing test before you fix anything."

๐Ÿ› ๏ธ EXERCISE โ€” TRY IT YOURSELF

Deliberately plant a small bug in a practice project (e.g. a typo in a variable name) and have an agent find it systematically.

  1. Trigger the error and copy the complete error message including the stack trace.
  2. Give the agent the error message and the reproduction command โ€“ and demand a hypothesis first, no fix yet.
  3. Only allow the fix after you have read the hypothesis, and have the agent verify success with the same command.

โœ… SELF-CHECK

  • โ˜ Did the agent name the correct cause before changing anything?
  • โ˜ Did it prove the fix with the reproduction command (showing output) instead of just saying "done"?
  • โ˜ What would have been missing if you had only written "there is a bug"?

QUICK QUIZ

What is the most sensible first step when an agent is supposed to fix a bug?

SOURCES

RELATED TOPICS

Agent Stuck? Here's How to Get Unstuck โ—โ—โ—‹Pattern: Feed Context Instead of Asking Vaguely โ—โ—‹โ—‹Slash Commands, Hooks & More: Steering Claude Code โ—โ—โ—‹Pattern: Plan First, Then Code โ—โ—โ—‹