Thursday, 3 Dec 2026 Global Edition
Live Wire
AI Today Daily AI journalism & intelligence
Front Page Tips & Tutorials Baca versi Indonesia

Stop Paste-and-Pray: How to Debug Code with AI, Systematically

Beyond paste-and-pray: a systematic AI debugging flow — full evidence, minimal repro, ranked hypotheses, and one proven fix at a time.

Stop Paste-and-Pray: How to Debug Code with AI, Systematically

It goes like this: you ask an AI to write a small script for a daily task, everything feels fine, and then you run it — a wall of red errors, fifteen lines long. The reflex for most people: select the error, paste it into ChatGPT or Claude, hit Enter, and pray. Sometimes it works. Often what comes back is a confident-sounding fix that repairs one thing while quietly breaking three others.

The model is rarely the problem. The problem is how we feed it information. Debugging is detective work, and a good detective never shows up with half a report. They bring complete evidence, a timeline of events, and a list of things already tried. This article walks through a six-step workflow that turns an AI chat into a disciplined debugging partner: preparing evidence, building a minimal example, testing hypotheses one at a time, and documenting the fix so the same bug never eats your afternoon again. The flow works for Python scripts, JavaScript, broken spreadsheet formulas, and small vibe-coded apps alike.

1. Gather Full Evidence Before You Ask Anything

The most common mistake is pasting only the last line, like TypeError: cannot read properties of undefined. The most valuable information sits above it: the stack trace showing which file and line the failure started in.

Before asking, prepare four things:

  • The full error, from first line to last, inside a code block — not a partial screenshot.
  • Expectation vs reality: "it should produce a CSV with 200 rows; instead the program stops at line 42."
  • A timeline: it worked yesterday, it broke today after I changed the file path. The last change is the golden clue.
  • How you run it: the exact command you typed, the operating system, and how the file gets invoked.

A decent opening prompt:

I have an error in a Python script and can't find the cause.
I will send: the full code, the full error, and what I already tried.
Help me find the root cause. Don't rewrite the code right away —
ask me first if anything is missing.

2. Build a Minimal Reproducible Example

A model reads the entire conversation context. The longer the code you paste, the more its attention splinters — and the higher your token bill climbs. The fix is a minimal reproducible example: the smallest version of your code that still triggers the same error.

How to shrink it:

  • Copy the code into a fresh file, then delete everything unrelated to the error.
  • Replace real data (possibly sensitive) with tiny dummy data.
  • Run it. Still errors? Good — that's a reproduction worth sending. No longer errors? Congratulations, you just discovered on your own that the bug lives in the part you deleted.

This loop often closes the case without any AI at all. If it doesn't, you now hold a tiny debug package that is far easier for the model to understand than your whole project.

3. Always Include the Environment: Versions, OS, Run Command

ModuleNotFoundError in Python 3.9 and Python 3.12 can have different fixes. Code that is valid on Node 18 can fail on Node 22. The AI cannot guess your environment — and it guesses wrong confidently.

State three things every time you ask for help: the language/runtime version, the operating system, and the exact command you ran. Save this template and refill it for each new case:

Role: You are a senior engineer who debugs systematically.
Environment: Python 3.12, Windows 11, run with "python etl_daily.py".
Code: [paste code or minimal example]
Error: [paste full error]
Expected: output_2026.csv filled with 200 rows.
Actual: stops at line 42 with KeyError: 'date'.
Rules: don't rewrite the whole file. Explain the root cause first,
then give me the smallest change needed.

The "Rules" line is the key: it stops the AI from replacing your entire architecture over a misplaced comma.

4. Ask for Ranked Hypotheses, Not One Verdict

A single answer keeps you passive: accept it or reject it. Compare that with asking the model to think like a detective:

Give me the 3 most likely root causes, ranked by probability.
For each, name one quick way to prove or disprove it —
for example, printing a specific variable at a specific line.

This format turns the conversation from guesswork into a structured experiment. For genuinely confusing bugs, two classic techniques pair beautifully with AI:

  • Binary-search isolation: comment out half the code. If the error disappears, the bug lives in that half; repeat until found. Ask the AI to help pick sensible cut points.
  • Digital rubber duck: ask the AI to explain your code back to you, line by line, fixing nothing. Very often, while reading that explanation, you spot the flawed logic yourself.

5. Apply Fixes One at a Time, with a Safety Net

The second-biggest mistake: accepting five suggestions at once, pasting them all, and losing track of which change actually fixed anything. The rule is simple — one change, one test.

Before applying anything, set a safety net: a git commit, or as simply as copying the file to etl_daily_backup.py. If the first suggestion fails, don't start a new chat; report the result back:

Tried it. The error now changes to: [paste new error].
Is this progress? What's the next step?

That "the error changed shape" detail is gold — it usually means the first hypothesis was partially right. And if the AI offers a massive rewrite, hold off: a healthy fix is usually small, and the AI should be able to explain why a change that small is enough.

6. Close the Case: Root Cause, a Relapse Alarm, and a Bug Journal

A bug that gets fixed without being understood will come back. Before closing the chat, ask for three things:

1. Explain the root cause in 3-4 plain sentences.
2. Give me one quick way to detect if this comes back
   (input validation, logging, or a custom error message).
3. One piece of advice so I don't repeat this class of mistake.

Then save the answers. Keep a personal bug journal: one file listing the error, the cause, and the fix. Next month when a similar bug appears, paste that old entry into the AI — and a case that once took two hours closes in ten minutes. Learning from old cases is what separates ordinary AI users from people who get faster every month.

One final warning: if the AI names a function or API you can't find in the official documentation, don't take it on faith — check the docs for the exact version you're running. The Stack Overflow 2025 survey found 66% of developers frustrated with AI answers that are "almost right, but not quite", and 45% say debugging AI-generated code actually takes more time than writing code themselves. The six steps above are designed precisely so you don't fall for those half-right answers: every claim gets tested, every fix gets proven, and every case becomes a stored lesson.

References

Partner Spotlight Explore today’s curated technology offers