Skip to content

How to ask AI to fix a bug: the report that gets it right

Guide/ 12 min read

Stop typing "fix the bug". Give the AI the exact error, the steps to reproduce it and where to look, then ask for the cause before the change.

  • AI
  • Claude Code
In this article (9)
  1. Why "fix the bug" doesn't work
  2. 1. The exact error: full message and stack trace
  3. 2. Steps to reproduce: what you did, expected and got
  4. 3. Where to look: the suspect file, function or commit
  5. The extra: ask the AI for the cause before the change
  6. Full example: the request before and after
  7. Common mistakes
  8. Frequently asked questions
  9. Checklist

To ask AI to fix a bug and get a real fix, give it three things: the exact error with its stack trace, the steps to reproduce it (what you did, what you expected, what happened) and where to look. Then ask it to explain the cause before it changes any code. That way it stops guessing.

It works the same for Claude Code, Cursor or any coding agent that reads your repository. You will see it on a real bug we wrote and ran in a demo store's cart: the error it printed, the vague request, the complete request and the fix with its tests. If your agent has no standing project context yet, start with what context an AI coding agent needs.

Why "fix the bug" doesn't work

When you type "fix the bug", the AI does not know which error you saw, how to trigger it or what "fixed" means to you. It has to guess all three. Anthropic's prompting best practices put it this way: Claude responds well to clear, explicit instructions, and it helps to think of it as a brilliant but new employee who lacks context on your norms and workflows. Their quick test: show your prompt to a colleague with minimal context; if they would be confused, Claude will be too.

Claude Code's best practices use a bug as the example. Instead of "fix the login bug", they suggest describing the symptom, naming where to look and asking for a failing test before the fix. And the fix bugs efficiently recipe in the same docs says to give Claude the command that reproduces the issue and gets a stack trace, the steps to reproduce it and whether the error is intermittent or consistent.

The risk of a vague request is not just wasted time. It is a fix for the symptom: the error stops showing up on the line where it fires while the cause stays put. You will see it below with real output.

the same bug, two requestsvague request"fix the bugin the cart"the AI has to guess:· what the error is· how to reproduce it· where to look· what 'fixed' meanscomplete requesterrorTypeError: Cannot read propertiesof undefined (reading 'price')at cartTotal (cart.js:15:19)stepsadd MUG and COFFEE, remove MUGexpected 1800; the total throwswhereremoveLine() in src/cart.jsbroke with the remove buttonaskthe cause first, no edits;then a test and the fixevery line takes one guess away from the AIwith the vague one it can patch the symptom and miss the cause

Scroll the figure sideways to see all of it

Missing from "fix the bug"What the AI does without itWhat you give it
The exact errorSearches the whole repo or assumes which oneThe full message and the stack trace
A reproductionCannot check that it fixed anythingWhat you did, expected and got, plus the command
Where to lookReads files until it finds its bearingsThe suspect file, function or commit
What "fixed" meansStops when it looks fixedA test that fails today and must pass

1. The exact error: full message and stack trace

Copy the error as it comes out, all of it. "Something about undefined" is useless; the exact message and the stack trace give the error type, the property that failed and the line. This is what the demo store printed when we ran its tests on Node 22 (the store lives in a T:\store folder):

✖ total after removing a product (0.2862ms)
  TypeError: Cannot read properties of undefined (reading 'price')
      at cartTotal (file:///T:/store/src/cart.js:15:19)
      at TestContext.<anonymous> (file:///T:/store/test/cart.test.js:17:16)
      at Test.runInAsyncScope (node:async_hooks:214:14)
  1. The first line names the test that failed and how long it took.
  2. TypeError: Cannot read properties of undefined (reading 'price'): the code tried to read price from something that is undefined. Copy this line word for word, because it can be searched as is.
  3. at cartTotal (…/src/cart.js:15:19): the first frame of the stack trace. Function, file, line 15, column 19. That is where the error fires, which is not always where it starts.
  4. at TestContext.<anonymous> (…/cart.test.js:17:16): who called that function, line 17 of the test.
  5. The node: lines are Node's test runner internals. Paste them if you like; they are rarely needed.

If the error shows up in the browser, copy the text from the DevTools console. If it comes from a request, add the status code and the response body from the Network tab. A screenshot helps (Claude Code takes pasted images), but text can be searched for in the code. From the terminal you can also pipe the output straight in, as the docs suggest: cat error.log | claude -p "...".

2. Steps to reproduce: what you did, expected and got

The error tells the AI what failed. A reproduction lets it trigger the failure itself and confirm afterwards that it is gone. Three lines do it:

  • What you did, in order: "Add MUG (2) and COFFEE (1), remove MUG, ask for the total."
  • What you expected, with numbers when there are any: "A total of 1800 cents and one line in the cart."
  • What happened: "TypeError in cartTotal; no total."

Add the command that reproduces it (npm test, a URL, a curl) and whether it happens every time or only sometimes. The Claude Code docs ask for that last part, and in our experience it changes the search: an intermittent error usually points at timing, caching or data rather than at one line.

The best reproduction is a failing test. This is the store's:

test("total after removing a product", () => {
  const cart = { lines: [] };
  addLine(cart, { sku: "MUG", price: 1200, quantity: 2 });
  addLine(cart, { sku: "COFFEE", price: 1800, quantity: 1 });
  removeLine(cart, "MUG");
  assert.equal(cartTotal(cart), 1800);
});
  1. It starts from an empty cart: { lines: [] }.
  2. The two addLine calls are the "what you did": two products, prices in cents.
  3. removeLine(cart, "MUG") is the step that triggers the bug.
  4. assert.equal(…, 1800) is the "what you expected", written so a machine can check it.

3. Where to look: the suspect file, function or commit

The AI can read the whole repository, but every file it opens to find its bearings costs time and context. You know things the code does not say: what changed yesterday, which button is new, which file you touched last. Say so: "The error fires in cartTotal, but it started when we shipped the remove button. Look at removeLine in src/cart.js."

In Claude Code you can type @ and a path (for example @src/cart.js) to include the file in your message. If you use git, the suspect commit is a great lead: git log --oneline -5 lists the last five so you can name one. How to keep commits small enough to be useful for this is in a git workflow for AI coding agents.

"Where to look" is a lead, not a diagnosis. If you have no idea, say so and ask it to investigate without editing first.

the 3 parts of a good bug report01the exact errormessage + stack traceTypeError: Cannot read properties ofundefined (reading 'price')at cartTotal (src/cart.js:15:19)02how to reproducedid · expected · gotdid: add MUG and COFFEE, remove MUGexpected: total 1800, one linegot: the total throws a TypeError03where to lookfile · function · commitsrc/cart.js · removeLine()started with the remove buttonthe error fires in cartTotal()with all three, the AI starts at the causewithout the third, it starts by reading half the repo

Scroll the figure sideways to see all of it

The extra: ask the AI for the cause before the change

The three parts tell the AI what is going on. What is left is how to work: cause first, change second. Anthropic's guide notes that current models follow instructions precisely: ask for suggestions and they sometimes only suggest; ask for changes and they change things. So "fix it" means it will edit. Asking for the cause on its own gives you a checkpoint before any fix exists:

Before you change any code, explain the cause: which line triggers it and why.
Do not edit files yet.

The same guide has a sample prompt that tells Claude never to speculate about code it has not opened and to read the files before answering. Once the explanation holds up, ask for the fix with its test: "Fix the cause, not the symptom. Add a test that fails today and run npm test when you are done." Claude Code's best practices push the same idea: give it something it can run that comes back pass or fail, and ask for evidence, the command it ran and its output, instead of "done". In Claude Code, plan mode builds this step in: it reads and proposes, and edits nothing until you approve.

cause first, then the changecauseexplain why it failswithout editing filesfixsmallest changeat the cause, not the symptomtesta test that reproduces+ the whole suitestill failing? back to the cause with the new outputin the demo cartdelete leaves a holein cart.linessplice(i, 1) if i !== -1no-op when i is -14 of 4 greennode --testa fix without a cause tends to hide the symptomthe test that reproduces the bug is the proof it is gone

Scroll the figure sideways to see all of it

Full example: the request before and after

This is the demo store's cart. Node 22, no dependencies, prices in cents:

// Demo store cart. Prices in cents.

export function addLine(cart, line) {
  cart.lines.push(line);
}

export function removeLine(cart, sku) {
  const i = cart.lines.findIndex((l) => l.sku === sku);
  delete cart.lines[i];
}

export function cartTotal(cart) {
  let total = 0;
  for (const line of cart.lines) {
    total += line.price * line.quantity;
  }
  return total;
}
  1. addLine appends the line to the array with push.
  2. removeLine finds the SKU's index with findIndex and removes it with delete. This is the bug.
  3. cartTotal walks the lines with for...of and adds up price times quantity. This is where, on line 15, the error fires.

The vague request:

The cart total is broken, fix the bug.

The complete request, with the three parts and the extra:

Error when running npm test:
TypeError: Cannot read properties of undefined (reading 'price')
    at cartTotal (file:///T:/store/src/cart.js:15:19)
    at TestContext.<anonymous> (file:///T:/store/test/cart.test.js:17:16)

Steps: add MUG (2) and COFFEE (1), remove MUG, ask for the total.
Expected: a total of 1800 and one line left in the cart. Fails every time.
Where to look: it fires in cartTotal, but it started with the remove button.
Check removeLine in @src/cart.js.

Before you change any code, explain the cause. Do not edit yet.

The cause, in one sentence: delete cart.lines[i] removes the element but leaves the array's length alone, so it leaves a hole. MDN says it plainly: deleting an array element does not affect length and creates a sparse array with an empty slot; to actually remove it, use splice(). Then for...of visits that hole as undefined, and reading price from undefined throws on line 15. The error fires in cartTotal but starts in removeLine. We checked it in Node: forEach and reduce skip the hole and for...of does not, which is how a bug like this can hide until someone rewrites a loop.

A fix that only looks at the failing line looks like this, and it makes the total test pass:

  for (const line of cart.lines) {
    if (!line) continue;
    total += line.price * line.quantity;
  }

The hole is still there, though. Add one more test, that the cart is left with a single line (what a cart badge would count), and the patch fails:

✖ removing a product leaves one line (0.7988ms)
  AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

  2 !== 1

The fix at the cause changes one line of removeLine:

export function removeLine(cart, sku) {
  const i = cart.lines.findIndex((l) => l.sku === sku);
  if (i !== -1) cart.lines.splice(i, 1);
}
  1. findIndex returns the SKU's index, or -1 when it is not there.
  2. if (i !== -1): without this guard, splice(-1, 1) would remove the last product in the cart. We checked it in Node, and a fourth test pins it down.
  3. splice(i, 1) removes the element and shifts the rest: the length goes down and no hole is left.

With the fix, all four tests pass:

✔ total with two products (0.8911ms)
✔ total after removing a product (0.1882ms)
✔ removing a product leaves one line (0.1438ms)
✔ removing a missing SKU leaves the cart alone (0.1234ms)
ℹ tests 4
ℹ pass 4
ℹ fail 0

To be clear: we wrote and ran the bug, both fixes and the tests ourselves so you could see real output. What the example teaches is the request. Against the vague one, the one-line patch was a reasonable answer; against the complete one, which asks for a single cart line and the cause first, it no longer is.

Common mistakes

  • Summarising the error. "Something about undefined" forces a guess. Paste the full message and the stack trace.
  • Pasting only the last line of the stack trace. The most useful one is usually the first line that names one of your files.
  • Saying what happened without what you expected. Without the right result, the AI cannot tell when it is done.
  • Sending only a cropped screenshot. It helps, but text can be searched for and copied into the code.
  • Asking for the fix without asking for the cause. That is how a patch ends up hiding the symptom.
  • Accepting "done" without evidence. Ask for the command it ran and its output.
  • Bundling three bugs into one message. One per request, each with its own test.

Frequently asked questions

Does this work the same with Claude Code, Cursor or ChatGPT?

Yes. The three parts are information no model can pull out of your code. What changes is how you attach files (in Claude Code, with @) and whether the agent can run the reproduction command itself.

What if there is no error message?

Then the wrong result is your error: describe what you saw and what you expected, with data, and add whatever does exist, such as the request in the Network tab, a value in the console or a log line. A bug without a message is exactly where a failing test pays off most.

Do I have to find the bug myself first?

No. "Where to look" is what you know and the code does not say: what changed, since when it fails, which part is new. If you know none of that, say so and ask the AI to investigate before editing.

How much of the stack trace should I paste?

All of it, if it is short. If it is long, paste the message and the first lines that name files in your project, and say the rest is framework code. Too much beats missing the one line that matters.

Checklist

  • You copied the full error message, not a summary.
  • You included the stack trace, at least the lines with your own files.
  • You wrote what you did, what you expected and what happened.
  • You gave the command that reproduces the bug and whether it always fails.
  • You named the suspect file, function or commit.
  • You asked for the cause before the change.
  • You asked for a test that fails today and for the test output at the end.

A good bug report is context for one task; the standing context for your project belongs in what context an AI coding agent needs. Before you ask for the fix, leave a clean commit so you can roll back, as covered in a git workflow for AI coding agents. And the whole method, from request to review, is in my 4-step Claude Code workflow.

Saw this on Instagram? Comment BUG on the Reel and I'll send you the guide.

Written by

Luis Morales

Luis Morales

Frontend and Shopify developer, EcomLabs

@luismoralesdev on Instagram

Published October 30, 2026 · updated November 1, 2026

← Blog