Debugging with Claude
Debugging is one of Claude Code’s strongest use cases. Instead of staring at stack traces alone, you have an agent that can read your code, run commands, and systematically track down issues.
Sharing Error Context
Section titled “Sharing Error Context”The most effective way to debug with Claude is to give it the error:
Paste the Error
Section titled “Paste the Error”> I'm getting this error:>> TypeError: Cannot read properties of undefined (reading 'map')> at UserList (src/components/UserList.tsx:15:28)> at renderWithHooks (node_modules/react-dom/...)Claude will:
- Read
UserList.tsxaround line 15 - Identify the undefined variable
- Trace back to find why it’s undefined
- Suggest a fix
Pipe Logs
Section titled “Pipe Logs”npm run dev 2>&1 | head -50 | claude -p "what's wrong?"Screenshot Errors
Section titled “Screenshot Errors”If you have a browser error, take a screenshot and share the path:
> Look at @/tmp/error-screenshot.png and help me fix thisDebugging Strategies Claude Uses
Section titled “Debugging Strategies Claude Uses”1. Read and Trace
Section titled “1. Read and Trace”Claude reads the error location, then traces the data flow:
- Where does the variable come from?
- What function sets it?
- What conditions could make it undefined/null?
2. Search for Patterns
Section titled “2. Search for Patterns”Claude greps the codebase for similar patterns:
- Are there other places with the same bug?
- How is this function called elsewhere?
- Has this pattern worked before?
3. Check Recent Changes
Section titled “3. Check Recent Changes”> What changed in the last 3 commits that could have caused this?Claude diffs recent commits to find the culprit.
4. Add Debugging Output
Section titled “4. Add Debugging Output”> Add some console.logs to trace the data flow through the auth middlewareClaude adds targeted logging, you reproduce the issue, then share the output.
5. Test Hypotheses
Section titled “5. Test Hypotheses”> I think the issue is that the database connection pool is exhausted.> Can you verify?Claude checks connection pool config, current usage patterns, and confirms or refutes.
Common Debugging Prompts
Section titled “Common Debugging Prompts”> Why is this API endpoint returning 500?> The login form submits but nothing happens> This query is taking 30 seconds, help me optimize it> The build passes locally but fails in CI — here's the CI log: ...> Users report intermittent 403 errors on the dashboardAnti-Patterns
Section titled “Anti-Patterns”Don’t: “Fix the bug” Do: “The user list page shows a blank screen. Here’s the console error: …”
Don’t: Ask Claude to guess without context Do: Share the error, reproduction steps, and relevant file paths
Don’t: Let Claude make changes without understanding them Do: Ask Claude to explain the root cause before applying a fix
Exercise
Section titled “Exercise”- Introduce a deliberate bug in a project (e.g., misspell a variable name)
- Run the project and capture the error
- Share the error with Claude and watch it trace through the code
- Ask Claude to explain the root cause before fixing
- Verify the fix works