How to Debug Code with ChatGPT or Claude: A Beginner's Checklist

You can debug code with ChatGPT or Claude, but how you ask decides how good the help is. Read the error yourself first, shrink the bug to a small example, give the AI a clear question, ask for the cause before the fix, and test the fix before you trust it.
This guide walks through each step with short Python and JavaScript examples. It ends with a checklist you can print or save. It works the same in ChatGPT, Claude, Gemini, or any other chat assistant.
Why "fix my code" is a weak prompt
Pasting 300 lines with "it doesn't work, fix it" gets you a guess. The AI does not know what the code should do, what went wrong, or what input caused it. It may rewrite half your file and hide the real problem.
It also teaches you nothing. Next time you hit the same bug, you are stuck again. The steps below take a few extra minutes and save you hours.
Step 1: Read the error first
An error message tells you what went wrong and where. Many beginners copy it into the chat without reading it. Spend 30 seconds on it first. Then you can tell if the AI's answer makes sense.
Here is a small Python program that should print the average of some scores:
scores = [72, 88, 95]
total = 0
for s in scores:
total = total + score
print("Average:", total / len(scores))
Running it gives:
Traceback (most recent call last):
File "grades.py", line 4, in <module>
total = total + score
^^^^^
NameError: name 'score' is not defined. Did you mean: 'scores'?
In Python, read the last line first. It has the error type (NameError) and the message (name 'score' is not defined). The line above it shows where: line 4.
So Python does not know the name score. The loop created s, not score. The fix is to use one name in both places, for example for score in scores:.
Notice the hint "Did you mean: 'scores'?" Newer Python versions add hints like this. They are helpful, but not always right. Changing the line to scores would cause a new error. This is exactly why you read the error yourself.
In JavaScript, the error is at the top. The lines below it show where it came from:
TypeError: Cannot read properties of undefined (reading 'city')
at showCity (profile.js:2:28)
This says you tried to read .city from something that is undefined. For example, user.address.city when user has no address. Look at the first line that points to your own file, not to a library.
Step 2: Reproduce the bug and shrink it
Some bugs give no error. The program runs and gives a wrong answer. For these, do two things before you ask anyone for help.
Reproduce it. Find the exact input that causes the bug every time. Write down three lines:
Input: 3 tickets
Expected: Total: 36
Actual: Total: 333333333333
The gap between expected and actual is the bug. Writing both down often shows you the cause on its own.
Shrink it. Remove every part of the code that is not needed to show the problem. Also shrink the input. If a list of 1,000 items gives a wrong result, try 3 items. The smallest version that still fails is called a minimal example.
Look inside with print. Add print() in Python or console.log() in JavaScript to see values as the code runs. Good places are inside a loop, before and after an if, and just before a return. Remove them once the bug is fixed.
Step 3: Use a clear debugging prompt
Now you have what the AI needs. Use this template:
I'm learning [language]. My code should [goal in one sentence].
Here is a minimal example that shows the problem:
[paste the short code]
When I run it with this input: [input]
I expected: [expected result]
But I got: [actual result, or the full error message copied exactly]
I already tried: [what you tried]
Please first explain what is causing the problem in simple terms.
Then show the smallest change that fixes it, and explain why it works.
A filled-in example for the ticket bug:
I'm learning Python. My code should ask how many tickets and print the total price.
quantity = input("How many tickets? ")
price = 12
print("Total:", quantity * price)
When I type 3, I expected: Total: 36
But I got: Total: 333333333333
I already tried: checking that price is 12.
Please first explain what is causing the problem in simple terms.
Then show the smallest change that fixes it, and explain why it works.
A good answer will explain that input() always gives back text. Text times a number repeats the text, so "3" * 12 gives twelve 3s. The smallest fix is to convert the text to a number:
quantity = int(input("How many tickets? "))
price = 12
print("Total:", quantity * price)
Step 4: Ask for the cause before the fix
The last two lines of the template matter most.
If you only ask for a fix, you get code to paste. It might fix the symptom and hide the real problem. Asking for the cause first helps in three ways:
- You can check the explanation against the error you saw. If they do not match, be careful.
- You learn the pattern, so you spot it yourself next time.
- The AI tends to reason more carefully when it has to explain first.
Asking for the smallest change matters too. AI assistants often rename things and restructure code while fixing one bug. That makes it hard to see which change fixed it, and it can add new bugs. One small, explained change is easy to check.
Step 5: Never paste secrets
Before you paste code into any AI chat, remove:
- Passwords, API keys, and tokens. Replace them with
YOUR_KEY_HERE. A leaked key can be used by other people and can cost real money. - Real personal data, like customer names, emails, or addresses used as test data. Use made-up examples.
- Code you are not allowed to share, such as an employer's or client's private code. Check the rules first.
A minimal example with made-up data rarely contains anything sensitive. That is one more reason to shrink the bug first.
Step 6: Verify the fix
AI fixes usually look right. Sometimes they only work for the one example you showed. Do not read the fix and assume it works. Run it, and test three kinds of input:
- Normal cases: typical input, including the one that failed.
- Edge cases: an empty list, zero, one item, a very large value.
- Boundary cases: exactly at a limit, like 18 when the rule is "18 or older."
Here is a real example. The rule is "orders of 50 or more get 10 percent off." Someone reported that an order of 80 got no discount, and an AI suggested this:
def discount(total):
if total > 50:
return total * 0.9
return total
print(discount(80))
print(discount(50))
print(discount(49.99))
print(discount(0))
The output:
72.0
50
49.99
0
The 80 order works now. But an order of exactly 50 gets no discount. The rule said "50 or more," and > means "more than." Only the boundary test shows this. The fix is >=:
def discount(total):
if total >= 50:
return total * 0.9
return total
Now discount(50) gives 45.0, and the other results stay the same.
You can ask the AI for help here too: "Suggest 6 test inputs for this function, including edge cases and boundary values, with the expected output for each." Then run them yourself. The AI can suggest tests. Only running them proves anything.
Also watch for functions that do not exist. AI can write code with a method that sounds right but is not real, or belongs to a different version of a library. If you get an error like ... is not a function or has no attribute right after a fix, tell the AI the exact error and check the official documentation. Our checklist for verifying AI output covers the same habit for all kinds of AI answers.
Common beginner bugs to recognize
Most beginner bugs fall into a few families. Knowing them helps you check whether the AI's explanation makes sense.
| Bug | What you see | Usual cause |
|---|---|---|
| Typo in a name | NameError (Python) or ReferenceError (JavaScript) | A misspelled name, or a name used before it was created |
| Off-by-one | IndexError in Python, undefined or NaN in JavaScript | Using <= instead of <, or forgetting lists start at 0 |
| Text vs number | TypeError, or "5" + "3" gives "53" | Input read as text and never converted |
| Missing return | None in Python, undefined in JavaScript | The function calculates but never returns the result |
| Wrong condition | No error, wrong result for some inputs | > instead of >=, or and instead of or |
| Syntax error | SyntaxError | A missing bracket, colon, or quote, often on the line before the one shown |
A quick JavaScript off-by-one example. This should return the last item:
function lastItem(items) {
return items[items.length];
}
console.log(lastItem(["pen", "book", "bag"])); // undefined
A list of 3 items has positions 0, 1, and 2. Position 3 does not exist, so JavaScript quietly gives undefined instead of an error. The fix is items[items.length - 1], which prints bag.
If the AI gets stuck
It is normal for the first fix to fail. Do not just say "still broken." Give the AI the new information:
I applied your change. Now I get this new error: [paste it]. Here is the updated code: [paste it].
If you go around several times:
- Ask for causes, not a fix: "List three different things that could cause this, and how I could check each one."
- Start a fresh chat with a short, clean summary. Long debugging chats fill up with failed ideas, and the AI can keep coming back to them.
- Check your assumptions: Did you save the file? Are you running the file you think you are? Is the input what you think it is?
For a very hard bug, a model with a thinking mode can help. Our guide to reasoning models vs regular models explains when that is worth the wait.
The printable checklist
Save this and use it every time you debug with AI.
Before you ask
- Read the error. Python: last line first. JavaScript: top line first.
- Find the line in your own code that the error points to.
- Reproduce the bug. Write down input, expected, and actual.
- Shrink the code and the input to a minimal example.
- Remove passwords, API keys, tokens, and real personal data.
When you ask
- Say the language and what the code should do.
- Paste the minimal example, the input, and the exact error or expected vs actual.
- Say what you already tried.
- Ask for the cause first, then the smallest fix with an explanation.
After you get a fix
- Do I understand why this works? If not, ask.
- Does it fix the cause, or only hide the error?
- Did it change only what was needed?
- Did I run normal, edge, and boundary tests?
- Does every function it uses exist in my language and version?
Key takeaways
- Read the error yourself first. It tells you what went wrong and where.
- Reproduce and shrink the bug before asking. A minimal example gets a better answer.
- Use the debugging prompt template: goal, code, input, expected vs actual, what you tried.
- Ask for the cause before the fix, and ask for the smallest change.
- Never paste keys, passwords, or real personal data.
- Test every fix with normal, edge, and boundary cases. Only running the code proves it works.
Frequently Asked Questions
Is it OK to use ChatGPT or Claude to debug my code as a beginner?
Yes, if you use it to understand the bug and not only to get new code. Read the error yourself first, ask the AI to explain the cause before it gives a fix, and test the fix. That way you learn the pattern and can spot a bad fix.
Should I paste my whole project into the AI?
No. Shrink the problem to the smallest piece of code that still shows the bug, plus the input that triggers it. A short example gets a much better answer, and it is less likely to contain anything private.
What should I never paste into an AI chat?
Passwords, API keys, tokens, real personal data, and code you are not allowed to share, such as an employer or client project covered by an agreement. Replace keys with a placeholder like YOUR_KEY_HERE and use made-up test data.
What if the AI fix does not work?
Give the AI the new error and the updated code, not just "still broken". If you go around several times, ask for a list of possible causes and how to check each one, or start a fresh chat with a clean summary of the problem.
How do I know the AI fix is correct?
Run it. Test the input that failed, then edge cases like an empty list or zero, then boundary values like exactly 50 when the rule is 50 or more. A fix that only works for your first example is not finished.
Which is better for debugging, ChatGPT or Claude?
Both can explain errors and suggest fixes well. The way you ask matters more than which tool you pick. The checklist in this post works the same in ChatGPT, Claude, Gemini, or any other chat assistant.
Practice for free
If you are still learning the basics, Python for Beginners and Interactive JavaScript Practice let you write and run code in your browser.
To practice everything in this guide, take the free Debug Any Code with AI micro course. You fix broken Python and JavaScript right in your browser, learn to read errors and shrink bugs, and practice checking an AI fix before you trust it.
Enjoyed this article?
Join The FreeAcademy Weekly
One practical AI email every Tuesday. New free courses, AI tips, and a short note from the founder.
Free forever. Unsubscribe anytime.
Related articles

How to Verify AI Output: A Checklist Before You Trust Any Answer
A short checklist to run before you trust an AI answer. Six plain-language questions that catch most mistakes, no coding or technical background needed.

AI Coding Assistants vs Learning to Code Yourself: Do You Still Need to Learn Programming in 2026?
With AI coding tools getting so good, should you still learn to code? A balanced look at what AI can and cannot do, why understanding code still matters, and how to become an AI-augmented developer.

Reasoning Models vs Regular Models: When to Use AI's "Thinking" Mode
Reasoning models think before they answer. Regular models answer right away. Learn which tasks need thinking mode, what it costs in time and limits, and how to prompt it.

