Rubber Duck Debugging With an AI That Talks Back

To debug with an AI the way you would with a rubber duck, describe what the code should do and what it does, one step at a time, and ask the AI to question your description before it touches any code. Most of the value comes from your own explanation. The routine below is a set of habits to try, and the test is what confirms them.
The catch: a duck cannot agree with you and an AI can. Walk in with a theory (“the slice is wrong”) and a friendly model tends to confirm it. So describe symptoms instead of theories, and let a test, not the AI’s agreement, decide who is right.
This post is about the start of a debugging session. Stop pasting the error back is about what to do after fixes keep failing, and Your AI fixed one bug and broke two is about a fix that breaks a second screen.
The duck comes from The Pragmatic Programmer: explain your code line by line to a duck, and you often hear the moment your intent and your code part ways.
Why is an AI a different kind of duck?
A duck has no opinions. An AI leans toward yours. In Sharma et al., five AI assistants frequently admitted mistakes they had not made when the user pushed back, gave biased feedback, and copied the user’s errors. The authors link part of this to human raters, who sometimes prefer a convincing answer that agrees with them over a correct one.
That study measured chat behavior in general, not debugging. The leap to debugging is a reasonable worry, not a measured result: leave with a confident fix for the wrong problem and you pay twice.
A bug where the wrong theory is tempting
Three small files and a test file. Make a folder and save them (Node 18 or newer). The results page reads ?page=1 from the URL. paginate is shared with the “download everything” export, which counts pages from 0.
If you do not code, skip to the prompt in “How do I make the AI act like a duck?” and use it on your own bug.
paginate.js:
// pageIndex starts at 0: the first page is pageIndex 0.function paginate(items, pageIndex, pageSize) { const start = pageIndex * pageSize; return items.slice(start, start + pageSize);}module.exports = { paginate };search.js:
const { paginate } = require('./paginate');
// The results page. The URL says ?page=1 for the first page.function resultsPage(items, query) { const page = Number(query.page || 1); return paginate(items, page, 3);}module.exports = { resultsPage };export.js:
const { paginate } = require('./paginate');
// The "download everything" button: walks every page from the first.function exportAll(items) { const out = []; for (let i = 0; i * 3 < items.length; i++) out.push(...paginate(items, i, 3)); return out;}module.exports = { exportAll };The user complaint: page 1 of search shows the wrong items. Before looking at any code, write down what a user should see. These tests check behavior, not how the code is built. Save as test/app.test.js:
const test = require('node:test');const assert = require('node:assert');const { resultsPage } = require('../search');const { exportAll } = require('../export');
const items = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; // 7 items, pages of 3
test('page 1 shows the first three results', () => { assert.deepStrictEqual(resultsPage(items, { page: '1' }), ['a', 'b', 'c']);});test('page 2 shows the next three', () => { assert.deepStrictEqual(resultsPage(items, { page: '2' }), ['d', 'e', 'f']);});test('the last page shows the leftovers', () => { assert.deepStrictEqual(resultsPage(items, { page: '3' }), ['g']);});test('no page in the URL means page 1', () => { assert.deepStrictEqual(resultsPage(items, {}), ['a', 'b', 'c']);});test('a page past the end is empty, not an error', () => { assert.deepStrictEqual(resultsPage(items, { page: '4' }), []);});test('the export contains every item once, in order', () => { assert.deepStrictEqual(exportAll(items), items);});Run node --test. Four tests fail on Node 22, and the first reads like the complaint:
✖ page 1 shows the first three results actual: [ 'd', 'e', 'f' ], expected: [ 'a', 'b', 'c' ],...ℹ pass 2ℹ fail 4Four red tests, one bug. The export passes because it calls paginate directly, and the past-the-end page passes because slicing beyond the data gives an empty list, even on broken code. A test that passes on broken code tells you little about what it protects.
What happens with a wrong theory?
Say you tell the AI: “I think paginate is wrong, the pages should start at 1.” That sounds right, since paginate.js is where slice lives. An assistant that agrees with you would change one line:
const start = (pageIndex - 1) * pageSize;(This patch was written by hand for this post to show what agreement looks like. It is not output captured from a model.) Run the tests:
✔ page 1 shows the first three results✔ page 2 shows the next three✔ the last page shows the leftovers✔ no page in the URL means page 1✔ a page past the end is empty, not an error✖ the export contains every item once, in orderℹ pass 5ℹ fail 1The complaint is gone and the AI sounds sure. But the export test now fails: the export counts from 0, and the “fix” shifted it. Nobody asked for that change, and a user would have found out when a download came back wrong. Without the export test you would have shipped it.
The real fix is in the caller, which turns the URL’s 1 into the 0 that paginate expects. Revert the line above and change one line in search.js instead (from paginate(items, page, 3) to the version below):
return paginate(items, page - 1, 3);All six tests pass. Tests only protect what they name, so keep adding one for each behavior you care about. A user can edit the URL, so try ?page=-1: on this code it returns ['b', 'c', 'd'], a slice counted from the end. Add the behavior as a test:
test('a page below 1 shows page 1, not a slice from the end', () => { assert.deepStrictEqual(resultsPage(items, { page: '-1' }), ['a', 'b', 'c']); assert.deepStrictEqual(resultsPage(items, { page: '0' }), ['a', 'b', 'c']);});It fails on the code above (actual: [ 'b', 'c', 'd' ]; pass 6, fail 1). Replace the two lines in search.js that read the page with this, and all seven pass:
const n = Number(query.page);const page = Number.isInteger(n) && n >= 1 ? n : 1;return paginate(items, page - 1, 3);Nothing tests a page size of zero.
How do I make the AI act like a duck?
Describe the symptom, not the cause. A theory prompt hands it your guess:
The slice in
paginateis off. Fix it.
A symptom prompt hands it evidence and asks for questions first:
Page 1 of search shows items 4 to 6 instead of 1 to 3. Do not suggest a fix yet. I will walk through the code. After each step, ask me one question that checks my understanding against real values. Then give me three possible causes, and for each one an experiment that would prove it wrong. At least one cause must contradict my own guess.
Three more moves help. Paste the failing test output rather than describing it. Paste the failing test and the one function, not the whole repo, since a big paste anchors the AI on code you did not write. And open a fresh chat for a second opinion, because the first one is anchored on your theory. To check an agreeable answer, state the opposite theory and see whether it also agrees; if so, its agreement was worth nothing.
None of these were run against a live assistant for this post. They are habits to try, and the test is what confirms them.
One more check, no AI needed: before accepting any fix, say in one sentence why the bug happened. In this example: “the URL counts from 1, paginate counts from 0, and the search page forgot to convert.” If you cannot say it, you do not understand the fix yet.
No terminal? Use a click list
Tests can be plain steps. Write them as “do this, expect that” and click through after every AI change: open results page 1, expect the first three items; open page 2, expect the next three; open the last page, expect the leftovers; press Download everything, expect all items once. That is the same suite, run by hand.
For the habit that makes a bad fix easy to undo, see Git for vibe coders and TDD with AI. If the AI’s fix breaks something else, Your AI fixed one bug and broke two covers it (this post is about not trusting an agreeable answer, that one is about a fix that breaks a second screen), and how to read a stack trace helps when there is an error message to start from.
The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.