AI Coding
How to Use AI to Debug Your Code (Step by Step)
Debugging used to mean me, a cryptic error message, and an hour of increasingly desperate searching. AI changed that more than almost anything else in how I code. Paste the error, paste the code, and most of the time you’ve got a clear explanation and a fix in under a minute. But there’s a right way to do it — one that fixes the bug and leaves you understanding why — and a lazy way that turns you into someone who can’t debug anything alone. Here’s the right way.
This works with any chatbot, and with coding assistants like Copilot (see my GitHub Copilot guide). The method matters more than the tool.
Step 1: Paste the error message (the whole thing)
The error message is gold, so give the AI all of it — not a paraphrase, the actual text. “Here’s the error I’m getting: [paste the full error]. Here’s the relevant code: [paste code]. What’s causing this and how do I fix it?” AI is genuinely excellent at decoding cryptic errors into plain English. Half the time, just reading its explanation of the error tells you exactly what went wrong. Don’t truncate the error; the part you’d cut is often the important part.
Step 2: Give it context, not just the broken line
Bugs often aren’t where the error points. So give the AI enough surrounding code to actually diagnose it — the function, the relevant variables, what you expected to happen versus what did happen. “I expected this to return a sorted list, but it’s returning an empty one” is a far better prompt than “this is broken.” The clearer you describe the gap between expected and actual, the faster it finds the real cause.
Step 3: Ask it to explain, not just fix
Here’s the discipline that keeps you sharp. Don’t just take the corrected code and move on. Ask “why did that fix it?” Understanding the cause means you’ll recognize the pattern next time and maybe not write the bug at all. This is the difference between using AI to learn and using it to stay dependent. The fix solves today’s problem; the explanation solves all the future ones. (I make this same case in my guide to learning coding with AI.)
Step 4: Use it to find bugs, not just fix errors
AI debugging isn’t only for code that crashes. It’s also great for code that runs but does the wrong thing. Paste working-but-misbehaving code and ask: “This runs but gives the wrong output — can you spot the logic error?” Or, proactively: “Review this function for bugs, edge cases, and things I might have missed.” AI is a tireless code reviewer, and asking it to hunt for problems before they bite you is one of its most underused tricks.
Step 5: Verify the fix (always)
AI is confident, and confidence isn’t correctness. It can suggest a fix that solves the symptom but not the cause, or one that introduces a new problem. So always test the fix — run the code, check the output, try the edge cases. Especially for anything important, don’t assume the suggested fix is right just because it’s stated firmly. You’re the one who confirms it actually works. A fix you tested beats a fix you trusted.
A realistic example
I had a script that was supposed to total a column of numbers but kept returning a weird, too-large result. No error — it just ran wrong. I pasted the code and described the gap: “expected the sum of the prices, getting a number way too big.” AI spotted it instantly: I was accidentally concatenating strings instead of adding numbers, because the values were text, not numbers. It explained why, showed the fix, and I tested it on a few rows to confirm. Five minutes for something that would’ve had me staring blankly for an hour. And because I asked why, I now catch that mistake on sight.
Frequently asked questions
How do I debug code with AI? Paste the full error message and the relevant code, describe what you expected versus what happened, and ask for the cause and fix. Then ask it to explain why, and test the fix.
Can AI find bugs in code that doesn’t crash? Yes — paste working-but-wrong code and ask it to find the logic error, or ask it to review a function for bugs and edge cases proactively.
Will the AI’s fix always be correct? No. It can be confidently wrong or fix the symptom not the cause. Always test the fix, especially for anything important.
Do I need to understand the fix? You should. Ask “why did that work?” so you learn the pattern. Blindly pasting fixes makes you dependent and unable to debug on your own.
What if the AI’s fix creates a new bug? It happens. Paste the new error and the changed code back and keep iterating — but also slow down and read what it’s changing. If you’re going in circles, step back and ask it to “explain the root cause before suggesting any more fixes.” Understanding beats trial-and-error.
Is it better to use a chatbot or a coding assistant for debugging? Both work. A chatbot is great for pasting an error and getting a thorough explanation; an in-editor assistant like Copilot is faster for quick fixes in context. Many developers use a chatbot for the tricky bugs and the assistant for the small stuff.
Will relying on AI to debug stop me from getting better? Only if you skip the “why.” Used as an explainer — paste the error, understand the cause, then fix it yourself — it actually accelerates your debugging skills, because you see far more worked examples than you would alone. Used as a copy-paste crutch, it stalls you. The choice is yours.
Where to go next
Debugging is one piece of coding with AI. If you’re still learning the fundamentals, my roadmap for using AI to learn coding shows how to do it without becoming dependent on the AI.
Leo Tan writes hands-on AI guides at AI Play Books. This is genuinely how I debug now. Got a better method? Tell me.