Script Broken Again? Build a Six-Step AI Debugging Routine, from Error Message to a Fix You Actually Understand
Stop pasting code and praying. A six-step debugging routine with AI chat that fixes your script and teaches you why the error happened.

AI-written scripts are heroes while they run. But the moment a red Traceback (most recent call last): floods the terminal, the ritual is predictable: copy the whole project, paste it into a chatbot, type please fix this. You get new code that sometimes works and is almost never understood — so next week, the same error returns.
This article assembles a six-step AI debugging routine that flips the pattern: the error gets translated, causes hypothesized, fixes tested one at a time, and the lesson recorded. For everyday bugs the whole process takes 10-20 minutes — and every case leaves knowledge that sticks.
Step 1: Copy the Full Error Message, Not Just the First Line
The most common mistake is copying only the last line, like TypeError: 'NoneType' object is not subscriptable. But a traceback is a map: every line shows the file, function, and line number the error passed through. The parts you throw away often contain exactly where to look.
Do this:
- Re-run the script and copy the entire terminal output, including file names and line numbers.
- If the output is long, take the block starting with
Tracebackin Python, or the first browser-console error. - Add mandatory context: language and library versions, for example Python 3.12 with pandas 2.2.
Then use this prompt:
I got this error running my Python script (Python 3.12, pandas 2.2):
[PASTE FULL TRACEBACK]
Context: the script reads a sales CSV and builds a monthly summary. Code near the error:
[PASTE 10-20 LINES]
Explain this error in plain language — what the error type means, which line triggered it, and the three most likely causes, ranked. No fix yet; I want to understand the problem first.
Do not give me a fix yet turns the AI into a translator instead of a slot machine — understanding sticks when a similar error returns.
Step 2: Build a Minimal Example That Still Fails
Don't paste 300 lines. The longer the code you send, the more likely the AI guesses in the wrong place — and the more tokens you burn. Shrink it to the smallest version that still fails the same way; 5-15 lines is usually enough.
The fast way: copy the script into a new file, comment out half, run. Still errors? Split again. Suddenly works? What you just disabled is the new suspect. Five minutes of detective work often gets you close to the root cause.
Send it with an expected-vs-actual frame:
Here is the smallest version of my code that still triggers the same error:
[PASTE 5-15 LINES]
Expected: the function returns totals per category.
Actual: I get [PASTE ERROR MESSAGE].
At which line does this minimal version break? Walk through the logic step by step, as if explaining to someone in their second month of learning Python.
As if explaining to someone still learning sounds trivial, but it stops the AI from jumping to advanced solutions.
Step 3: Ask for Ranked Hypotheses First
This step separates casual users from people who actually learn. Asking for a fix right away gives you one convincing guess; asking for three hypotheses plus verification steps gives you a thinking process you can follow, challenge, and correct.
From the minimal example, give me 3 ranked hypotheses. For each, tell me:
1. How to verify it in under a minute — for example, which line to add a print to.
2. The symptom if it's right, and the symptom if it's wrong.
No fix code yet.
Run the verification yourself: add the suggested print(), inspect the variable, compare with the prediction. If it misses, report back — Hypothesis 1 is wrong; the print shows X. Now the AI learns from evidence, not repeated guesses.
Step 4: Apply One Change, One Time
AI fixes often bundle several changes. Don't apply them together — if the script suddenly runs, you don't know which change healed it; if it still fails, you don't know which one broke it. Apply one, run, note the result, then continue.
When the fix is found, demand an explanation:
Hypothesis 2 turned out to be right. Give me a minimal fix: change as few lines as possible. Explain each change — what changed, why, and what would happen if it weren't. Finally, show me how to rewrite this section so the same class of error cannot recur.
What would happen if it weren't changed matters: it forces the AI to connect the fix to the root cause, not just paste its generic pattern.
Step 5: Lock the Fix with a Small Test and a Personal Debug Log
A fix isn't done when the script runs once. Re-test with old data so the change doesn't silently break something else — that is a regression. Keep one small sample file as your permanent test case and run the script against it after every change.
Record the case in a single debug-log.md — one entry per bug:
Summarize this debugging case in the following format so I can save it to my log:
- Symptom:
- Original error:
- Root cause:
- Fix applied:
- Lesson / prevention:
- Search keywords:
After a month this becomes your personal playbook: an error that used to eat an afternoon now takes two minutes to recognize — lessons from your own cases, the most honest way to learn coding. Bonus: paste the log next session so the AI sees your recurring patterns.
Step 6: Know the Three Blind Spots of AI Debugging
A chat AI is a fast diagnostic partner with three weaknesses to compensate for yourself:
- Hallucinated functions. AI sometimes invents function names or parameters that never existed in that library. Check unfamiliar functions in the official documentation — not in the AI's answer.
- Version mismatches. A fix for an old pandas version can fail on a new one — that's why Step 1 makes you state versions. Unrecognized syntax usually means a version difference.
- Secrets in the paste. Never paste API keys, passwords, tokens, or customer data into a chatbot. Use placeholders like
YOUR_API_KEYbefore code leaves your machine — for debugging, the structure is almost always enough.
Debugging isn't an annoying side task — it's the part of the work that teaches the most. With this routine the AI stops being a guessing machine and becomes a diagnostic partner that makes you sharper every time red text appears. Next bug: don't ask for a fix, ask for an explanation.
References
- Errors and Exceptions — Python 3 Documentation — the official guide to reading Python tracebacks
- Prompt Engineering Guide — OpenAI — strategies for clear, specific instructions
- Minimal Reproducible Example — Stack Overflow Help — the classic guide to minimal code examples
- Prompt Engineering Overview — Anthropic Documentation — techniques for step-by-step reasoning



