Rubber Duck Debugging With AI: A Practical Method
Rubber duck debugging with AI works when you make the model ask questions instead of fixing code. Here is the prompt, a worked bug, and what to avoid.
Rubber duck debugging with AI means explaining your bug to a model and having it ask questions, instead of asking it for a fix. The classic version is talking to a rubber duck on your desk until the cause becomes obvious while you speak. An AI version is better in one way, because it can answer back with the question you skipped, and worse in another, because it will happily hand you a confident wrong fix if you let it. The method below keeps the good half.
Why asking for a fix often makes debugging slower
When you paste an error and ask "fix this", the model has to guess the cause from a few lines. It will pick the most common cause for that error message, change some code, and sound sure. If the common cause is not yours, you now have a patch for a bug you do not have, plus the original bug.
Rubber duck debugging works the other way around. The value is in being forced to state what you believe the code does, what it actually does, and where those two stop matching. Most bugs live in the gap between those two descriptions. A model that asks you to describe that gap helps you find it, and a model that jumps over it hides it.
A rubber duck debugging prompt for AI
Here is a prompt that sets the rules. Paste it first, then your description.
Act as a rubber duck. I will describe a bug. Do not propose a fix and do not
rewrite my code until I say "fix it".
First, ask me up to three questions, one at a time, that would expose a wrong
assumption I am making. Wait for my answer to each.
After the questions, tell me which assumption you think is most likely wrong and
why, in two sentences. Then stop and wait.Three rules do the work. No fix until you say so. One question at a time, so you actually answer instead of skimming a list. And a stop at the end, so the model names a suspect and leaves the next move to you.
A worked example
Here is a small function that is supposed to return the last three items of a list:
def last_three(items):
return items[len(items) - 3 : len(items) - 1]Calling last_three([1, 2, 3, 4, 5]) returns [3, 4]. You expected [3, 4, 5].
You paste the prompt, then say: "last_three returns two items, not three. I think my slice is off."
A good duck session goes like this:
Model: "What is the value of
len(items) - 1for a five-item list?" You answer: four.Model: "In Python, does a slice include or exclude its end index?" You pause, and answer: it excludes it.
Model: "So which index does your slice stop before?" You say: index four, so it stops at index three, and the last item is never included.
You found it yourself by answering. The model's summary then names the assumption: you treated the end index as inclusive. The fix, items[-3:], was never the interesting part.
Notice what did not happen. The model did not guess at a rewrite, and you did not have to evaluate a patch for a bug you had not yet understood.
When to say "fix it"
Once you can state the cause in a sentence, switch modes. Say "fix it", and also say what you want preserved: "keep the function signature and don't change the other callers." Without that line, models often rename things or restructure code that was fine. If you work with a coding agent, the post on stopping an AI coding agent from editing unrelated files covers keeping that change small.
What to include in your description
The model can only ask good questions about what you tell it. Give it:
What you expected to happen, in one sentence.
What actually happened, including the exact error text or output.
What you already tried, so it does not suggest it again.
The smallest piece of code that shows the problem, not the whole file.
If you cannot reduce the problem to a few lines, that is a finding too. Reducing it is often where you spot the cause.
Where this method fails
Bugs that depend on your environment. If the problem comes from a configuration, a version, or data the model cannot see, questions will circle. Give it the facts, or run the command yourself and report back. A flaky local server is a common example, and an AI coding agent against a flaky dev server shows how to rule it out.
Bugs you cannot describe. If you honestly have no theory, say so. Then change the prompt to "ask me what I would check first", and work from observation rather than assumption.
Long sessions. If the conversation drifts and the model starts repeating itself, start a fresh chat with a short summary of what you have established. A model also benefits from a plan reviewed before it runs when it is allowed to take actions.
This method does not replace tests. Once you have the cause, a failing test that captures it keeps the bug from coming back. See how to get an AI coding agent to write tests first for that step, and the AI coding tools overview for where debugging fits among the rest of the workflow.
Frequently asked questions
What is rubber duck debugging?
It is the practice of explaining your code, line by line, to an inanimate object such as a rubber duck. Putting the problem into words often reveals the mistake without any other help.
Can ChatGPT or Claude act as a rubber duck?
Yes, if you tell it to ask questions and hold back fixes. By default most assistants try to solve the problem right away, so the instruction not to is the important part.
Is it faster to just ask the AI to fix the bug?
For simple, common errors, often yes. For bugs where the cause is unclear, a guessed fix can cost more time than it saves, which is when this method pays off.
Does explaining a bug to AI expose my code?
You send whatever you paste to the service you use. Share only the minimal snippet needed, and leave out secrets, keys, and customer data. Keeping secret files out of the repository in the first place also helps, and five gitignore mistakes to catch lists the usual ones.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


