Skip to main content
← Prompt library

debug-code · claude-3-5-sonnet · coding · 90% confidence

Debug code with Claude 3.5 Sonnet — optimized prompt

A Claude 3.5 Sonnet debugging prompt using XML tags, a forced <scratchpad> execution trace, and explicit no-refactor constraints to surface the true root cause.

90% confidenceUpdated 9/7/2026debug-code · claude-3-5-sonnet · coding
Original prompt
Hey Claude, can you help me debug this Python code? It's giving me an error. Here's the code: [PASTE CODE] and the error: [ERROR]. Thanks!
Optimized prompt
You are debugging code for an engineer who wants the real root cause, not a patch over the symptom.

<code language="LANGUAGE_AND_VERSION">
[PASTE CODE]
</code>

<failure>
[ERROR / STACK TRACE / WRONG OUTPUT vs EXPECTED]
</failure>

<instructions>
1. In a <scratchpad>, trace execution with one concrete input until you reach the failure. Do not skip steps.
2. Then give:
   - Root cause: the specific defect and the line it lives on.
   - Fix: the minimal change, as a diff.
   - Verification: the input or assertion that proves the fix and would have caught the bug.
3. Do NOT suggest unrelated refactors, renames, or style changes.
</instructions>

<output_format>
Root cause: ...
Fix:
```diff
...
```
Verification: ...
</output_format>

Why this Claude 3.5 Sonnet prompt works

Tuned for Claude 3.5 Sonnet: XML tags for the code and failure, a forced <scratchpad> execution trace before any answer, and an explicit no-refactor rule. Claude honours structured tags and negative constraints unusually well, and the scratchpad is where it actually finds the root cause instead of guessing.

What to check in the output

  • The optimized prompt provides clear instructions and examples for each output field, ensuring the model's output is precise and directly usable.
  • The optimized prompt uses a 'chain-of-thought' approach by outlining specific analysis steps, guiding the model towards a more accurate and reasoned solution.
  • The optimized prompt explicitly requests 'traceback_info', which is critical for effective code debugging.

Slug8b9465cd-213d-4b13-a180-7908c1fa8c07

ModelClaude 3.5 Sonnet