Debug Code Prompt — Diagnose Before Fixing
The default failure mode of asking AI to debug code: the model immediately proposes a fix that papers over the symptom without finding the actual cause. This prompt forces a diagnosis-first flow — top hypotheses, clarifying question, then fix — that catches root causes instead of patching them.
The template
Replace the [bracketed] fields with your specifics. Send to any AI chat.
Bug: [describe the symptom in one sentence] What I expected: [behavior] What happens: [actual behavior] Reproduction: [minimal steps] Relevant code: [paste] Error message / logs (if any): [paste] What I've already tried: [list] Help me debug. Start by: 1. Listing the 3 most likely causes, most likely first 2. Asking me 1 question that would help narrow it down 3. NOT giving me a fix yet — diagnosis first.
How to use it
- Spend 60 seconds filling in ‘what I’ve already tried’ carefully. This prevents the model from re-suggesting the things you already ruled out.
- Use the model’s clarifying question. Don’t just give it more code and ask it to keep going — answer the actual question.
- Once you’ve confirmed the cause, ask for the fix. Two-step debugging produces better fixes than one-step.
Example output
Here’s an example of what a quality AI returns when you run this template:
Why this prompt works
Hypotheses first, fix later.
Without explicit instruction, models propose fixes immediately because that’s the default ‘helpful’ behavior. The bug stays unfixed because the patch masks the real cause.
Ranking the hypotheses surfaces the diagnostic principle.
Asking for ‘most likely first’ forces the model to reveal what evidence pattern it’s using to rank — which often itself reveals the cause.
One clarifying question, not five.
Models will spam clarifying questions when uncertain. Capping at one forces them to pick the highest-leverage discriminator.
‘What I’ve already tried’ prevents re-suggestion loops.
Without this, you spend the conversation getting the same 3 standard fixes over and over.
Which AI to use
Read the full comparison in ChatGPT vs Claude vs Gemini in 2026 →
Related prompts
Code review prompt →When the code works but you want feedback on quality.SQL query prompt →When debugging slow or wrong SQL queries specifically. All 27 prompt templates + free generator → Pick a category, fill in the blanks, copy your prompt.Frequently asked questions
Why won’t the AI just give me a fix?
It will if you ask. But for non-trivial bugs, jumping to a fix without diagnosis is how you spend 4 hours papering over a symptom while the real bug ships to production. Two-step debugging is faster overall.
How much code should I include in the prompt?
Just enough for the model to see the bug — usually 20-100 lines around the issue. Including a whole file dilutes attention and gets you worse diagnoses.
Can the AI run my code to check?
ChatGPT’s Python interpreter and Claude’s Code Execution can both run code. But they can’t run YOUR specific environment — they don’t have your database, your env vars, your dependency versions. Self-contained reproductions work; environment-specific bugs don’t.
What about race conditions and timing bugs?
These are genuinely hard for AI to diagnose because they can’t observe the timing themselves. Include any logs that show the ordering of events, and tell the model the exact reproduction conditions.
Should I send my whole stack trace?
Yes — include the full trace. The model will pick the relevant frames. Don’t paraphrase or trim.
Build any prompt in 30 seconds
Free tool, no signup, runs in your browser. Pick a category, fill in the blanks, copy your prompt.
Try the prompt generator → Read the full guide