Debugging with Claude Code

Use Claude Code to debug errors, analyze stack traces, find root causes, and fix bugs. Includes workflows for Python, JavaScript/Node.js, and general debugging patterns.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

Claude Code works best for debugging when you give it the full error output and let it read the source files. Here are the most effective patterns.

Paste and fix

# Just paste the error directly — Claude reads the relevant files:

"I'm getting this error:

Traceback (most recent call last):
  File 'src/api/users.py', line 47, in create_user
    db.add(user_obj)
  File 'src/db/session.py', line 12, in add
    self.session.add(obj)
sqlalchemy.exc.IntegrityError: (psycopg2.errors.UniqueViolation)
    duplicate key value violates unique constraint 'users_email_key'

Fix it."

# Claude reads src/api/users.py, src/db/session.py, and the relevant models.
# It identifies: create_user doesn't check for existing email before insert.
# It implements: a check (or upsert) before db.add().

Run and iterate

# Let Claude run the failing test and iterate until it passes:

"Run pytest tests/test_users.py::test_create_duplicate_user
and fix the test or the code until it passes."

# Claude runs: pytest tests/test_users.py::test_create_duplicate_user -x
# Reads the failure output
# Reads the test and source files
# Implements a fix
# Runs again to verify
# Repeats until green

Root cause analysis for intermittent bugs

# For bugs that don't reproduce consistently:

"This error appears in production logs but I can't reproduce it locally:

KeyError: 'user_id'
  File 'src/middleware/auth.py', line 23, in get_current_user
    return request.state.user['user_id']

Check the code path and find all conditions where request.state.user
might not have a 'user_id' key."

# Claude traces the code path:
# - How is request.state.user set?
# - Under what conditions could 'user_id' be absent?
# - What middleware runs before auth.py?
# Returns a list of code paths that can trigger the bug.

JavaScript / Node.js debugging

# Paste the Node.js error:
"Fix this:

TypeError: Cannot read properties of undefined (reading 'map')
    at UserList (/src/components/UserList.jsx:14:22)
    at renderWithHooks (react-dom.development.js:14985:18)

The component is trying to render before data loads."

# Claude reads UserList.jsx, identifies the missing null check,
# and adds appropriate loading state handling.

# For async/Promise bugs:
"This Promise chain is not resolving correctly:
[paste code]
Find why the catch handler is never called."

Debug logging strategy

# Ask Claude to add strategic debug logging:
"Add debug logging to the payment processing flow in src/payments/
so I can trace the state at each step. Use Python logging, level=DEBUG,
and include the transaction_id in every log line."

# Remove it later:
"Remove all the debug logging you added in src/payments/"

# For production debugging without code changes:
"Show me how to enable verbose logging for SQLAlchemy without
changing application code (environment variable or config approach)."

Bisect a regression

# When a bug was introduced by a recent commit:

"This test was passing last week and now fails:
[test output]

Check git log for commits to src/auth/ in the last 7 days and
identify which commit likely introduced this regression."

# Claude runs:
# git log --oneline --since='7 days ago' -- src/auth/
# git show  for each candidate
# Reads the test to understand what it checks
# Identifies the most likely culprit commit

Common debugging patterns Claude handles well

Bug typePrompt pattern
Import error / circular import"Fix: ImportError [paste error]"
Type error"Fix: TypeError [paste error + relevant code]"
Test failure"Run [test] and fix until it passes"
Missing env variable"Why does the app crash when REDIS_URL isn't set?"
Off-by-one / logic bug"This function returns [X] but should return [Y]. Fix it."
Memory leak"Find memory leaks in [module] — look for unclosed resources"

For code review (catching bugs before they happen), see Claude Code code review workflow. For using hooks to automatically run tests after edits, see Claude Code hooks tutorial.

Frequently asked questions

How do I use Claude Code to debug an error?
Paste the error message and stack trace directly into Claude Code, then say 'fix this'. Claude reads the relevant source files, traces the call path, identifies the root cause, and implements the fix.
Can Claude Code run my code to reproduce a bug?
Yes, if you allow Bash tool calls. Claude can run your test suite, execute specific scripts, and iterate on fixes until the error is resolved — all within the session.
How do I give Claude Code enough context to debug a complex bug?
Paste the full stack trace (not just the last line), mention what you expected vs what happened, and note any recent changes. Claude Code will read the relevant files automatically — you don't need to paste source code.
Can Claude Code debug async/concurrent bugs?
Claude is helpful for identifying async patterns (missing await, race condition logic errors, incorrect Promise chaining) by reading the code. It can't observe runtime state directly, so it works best when you can provide the error message or a test that reproduces the issue.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript