How to Debug Code With AI Without Trusting It Blindly
· 6 min read
To debug code with AI, give it the exact error and the smallest code that still fails, ask for possible causes before any fix, and test each cause. Then read every line it changes, because an assistant's explanation is a theory about your bug, not a result.
What should I give an AI when I debug code with it?
Three things, in this order: the exact error, the smallest example that still fails, and what you expected to happen.
Paste the error as the program printed it, from the first line to the end of the trace. "It crashes on save" says little; "TypeError: Cannot read properties of undefined (reading 'map')" with its trace adds the file, the line and the value that was undefined. If nothing crashes, give the input, the output you got and the one you wanted.
Then shrink the code until removing one more line makes the bug vanish; what is left is your example. Add versions and anything that changed just before the bug appeared. If the thread is cluttered with failed attempts, keep a long AI conversation on track or start a new one with the error, the smallest example, what you expected, and the versions and recent change.
The made-up bug used below is a library's event listing that shows a Saturday 14 March workshop on Friday 13 March to a volunteer in Toronto, while the organiser in London sees the right day. The page is JavaScript, and the smallest example is three lines: the stored text 2026-03-14, a call that builds a Date from it, and a call that formats it with toLocaleDateString in the viewer's time zone.
Why ask an AI for possible causes before a fix?
An assistant told to "fix this" will give you a fix, often a narrow patch: add a day, catch the exception, return a default. That can pass your example while the cause stays put. It may also use a function that does not exist; why AI makes things up explains why.
Ask for suspects, meaning possible causes, first: "List the likely causes, most likely first. For each, say what I could print or run to confirm or rule it out. Do not write a fix yet."
For the listing bug it might answer:
- JavaScript reads date-only text as midnight in UTC and the display uses the viewer's time zone, so anyone west of UTC sees the day before.
- The server shifts the date when it saves it.
- The display code subtracts a day when it formats the date.
Some of these may be wrong, and more than one can be at fault, so test each rather than stopping at the first that fits.
How do I test each suspect before changing anything?
Pick the cheapest check that can tell the suspects apart and run it yourself: print values at each step, feed the failing input to a one-off test, or use a debugger. A chat assistant cannot see what your machine prints, so paste the output back, including results that rule a suspect out. An agent that runs commands will see it, but you still choose the check and read what it shows.
In the listing bug, print the stored text, the parsed value in UTC and the value shown, once with the time zone set to London and once to Toronto. In a Node script, for example, set the TZ environment variable per run (TZ=Europe/London, then TZ=America/Toronto); browser developer tools may have a time zone override. If the display subtracted a day, London would be wrong too, so that suspect is out. If the stored text still reads 2026-03-14 after saving, the server suspect is out as well. What remains is the first. Date text with a clock time and no offset is read as local time, so the text's format matters.
This assumes the bug happens every time. If you cannot trigger it on demand, no test can confirm a fix: ask what to log (inputs, timing, the order of events), capture it on the next failure, and share that log instead of asking for a fix.
What should I check in the fix it suggests?
Only now ask for a fix, and keep it narrow: "Fix only the confirmed cause, and explain each line you change." Read the diff as the safe workflow for AI coding assistants describes, including its checks for deleted tests and new packages. If an agent runs commands, keep command approval on and read each command before you allow it. For debugging, also check that:
- The change touches only the confirmed cause. A catch-all, a default value or a retry hides the symptom instead.
- The explanation matches the output you printed, not just a plausible story.
A patch for the listing bug that adds a day to every date turns the Toronto case green and breaks London, where the date was right. A fix that matches the cause formats the date in UTC, or treats the text as a calendar date, not a moment in time.
Turn your failing example into a test that fails before the fix and passes after it, unchanged: here the Toronto case. Run the London case and the rest of your tests too, since a repair can break other code.
What should I keep out of what I paste?
Anything that would do harm if it leaked or were kept: API keys, passwords, access tokens, connection strings, session cookies, and customer details such as names, emails or real database rows. Secrets also hide in error output and settings files, and an agent sees whatever its commands print.
Before you paste, swap these for placeholders such as API_KEY_HERE or [email protected]. Often the shape is enough, such as invented events in the same date format. If the bug disappears with invented data, that is a clue: change one value at a time, keeping the format, until it returns, and share only the value that matters, with identifying parts removed. If a real secret does go in, treat it as exposed and revoke it. Check your employer's rules on sending code outside the company, and whether the service may keep or reuse what you paste.
When should I stop and ask a person?
As a rule of thumb, stop when any of these is true:
- The same fix has failed twice, or the suggestions go in circles.
- The change touches logins, permissions, payments, deleting data or a database migration.
- You cannot say, in your own words, why the fix works.
- The assistant proposes silencing the error or deleting a failing test.
- The bug appears only in production or with real customer data, and invented data does not reproduce it.
Bring your notes (example, suspects, results); when to keep a human in the loop covers how much checking a task needs.
Where NAMELESS stands
Our AI model, NAMELESS, is not yet available because it is still in training, and no launch date is set, so it cannot yet be used for any of this. One goal is unlimited context, which would let more of a project sit in one conversation but would not clear a thread cluttered with failed attempts; strong reasoning can still back the wrong cause. You would still test each suspect and read the diff. To follow progress, join the waitlist.