📚 Learning Hub
· 3 min read

5 AI Prompts That Actually Work for Debugging


Pasting an error message into ChatGPT works sometimes. But structured prompts work almost every time. Here are five templates I use daily.

1. The Stack Trace Decoder

I'm getting this error in my [language/framework] project:

[paste full stack trace]

My code at the failing line:
[paste the relevant function/file]

What's causing this and how do I fix it?

Why it works: You’re giving the AI the error, the context, and the code. Most people only paste the error message and wonder why the answer is generic.

The key is including the code at the failing line. The AI can’t help with “TypeError: Cannot read property ‘map’ of undefined” without seeing what you’re calling .map() on.

2. The “It Works Locally” Prompt

This code works in my local dev environment but fails in [production/CI/Docker]:

[paste the code]

Local environment: [Node 20, macOS, etc.]
Production environment: [Node 18, Linux, Docker, etc.]

Error in production:
[paste error]

What environment differences could cause this?

Why it works: “Works locally, fails in production” is almost always an environment difference. By listing both environments explicitly, you help the AI narrow down the cause — different Node versions, missing env vars, file path differences, etc.

3. The Rubber Duck Prompt

I'm trying to [describe what you want to achieve].

My current approach:
[describe or paste your implementation]

It's not working because:
[describe the unexpected behavior]

I've already tried:
- [thing 1]
- [thing 2]

What am I missing?

Why it works: This forces you to articulate the problem clearly — which sometimes solves it before you even send the prompt. The “I’ve already tried” section prevents the AI from suggesting things you’ve already ruled out.

4. The Performance Debugger

This function is slow. It takes [X seconds/ms] when I expect [Y seconds/ms]:

[paste the function]

It's called with data that looks like:
[paste example input or describe the data shape/size]

What's causing the performance issue and how can I optimize it?

Why it works: Performance problems need data context. A function that’s fine with 100 items might be O(n²) and terrible with 10,000. By including the input shape, the AI can spot algorithmic issues, unnecessary re-renders, N+1 queries, or missing indexes.

5. The “Explain Then Fix” Prompt

Explain what this code does step by step, then tell me why it produces [wrong output] instead of [expected output]:

[paste the code]

Input: [paste input]
Expected output: [paste expected]
Actual output: [paste actual]

Why it works: By asking the AI to explain the code first, it builds a mental model before diagnosing the bug. This catches logic errors that a direct “fix this” prompt would miss — the AI might “fix” the code by rewriting it entirely instead of finding the actual bug.


The Meta-Tip

Always include:

  1. The error or unexpected behavior — not just “it doesn’t work”
  2. The relevant code — not your entire codebase, just the failing part
  3. What you expected — so the AI knows what “working” looks like
  4. What you’ve tried — so it doesn’t waste your time with obvious suggestions

The difference between a bad AI debugging session and a great one is almost always the quality of the prompt, not the quality of the AI.

Works with any AI tool: Claude Code, ChatGPT, or local models via Ollama. See our best AI coding tools ranking for more options.

Related: 8 Browser Devtools Features You Didnt Know