Debugging with AI

Turn a bug into a tight debugging prompt: the exact error, the code that threw, the input, and what you already tried.

TL;DR

  1. Give the full error and stack trace, the code that threw, and the input that triggered it.
  2. Ask for the root cause first, then the fix, so you understand the bug rather than just patching it.
  3. Say what you already tried so the model does not repeat dead ends.

Package The Bug

    Full Error

    Paste the complete message and stack trace verbatim.

    TypeError: Cannot read properties of
    undefined (reading 'map') at Cart.tsx:28
    The Code

    Include the function that threw and the types it touches.

    // paste Cart.tsx lines 20-35
    // + the CartItem type
    The Trigger

    Show the input or state that caused it.

    Happens when the cart is empty
    (items is undefined).

Ask The Right Way

    Cause First

    Request the explanation before the fix so you can vet the diagnosis.

    "Explain why this throws, then
    propose a fix."
    Multiple Hypotheses

    Ask for a few likely causes ranked, not one confident guess.

    "List the 2-3 most likely causes,
    most likely first."
    State What You Tried

    Rule out dead ends so the model does not suggest them again.

    "I already checked the API response;
    it returns items correctly."

Validate The Fix

    Cause, Not Symptom

    Confirm the fix addresses why it broke, not just that the error stopped.

    "Does this fix the cause or just
    silence the error?"
    Add A Test

    Ask for a test that fails on the old code and passes on the new.

    "Write a test that reproduces the
    empty-cart case."
    Check Side Effects

    Make sure the fix did not change unrelated behavior.

    "What else could this change affect?"

When Stuck

    Add Logging

    Have the model insert targeted logs to narrow where state goes wrong.

    "Add console.logs to show `items`
    at each step."
    Shrink The Repro

    Reduce to the smallest snippet that still fails to isolate the cause.

    "Help me make a 10-line repro that
    still throws."
    Rubber-Duck It

    Ask the model to restate the flow; the mismatch often reveals the bug.

    "Walk through what happens on an
    empty cart, line by line."

Tips

  1. Paste the complete stack trace, not a paraphrase; the frame and line often point straight to the cause.
  2. Ask 'what are the top 2-3 likely causes?' before accepting a single confident-sounding answer.

Warnings

  1. Models will confidently 'fix' code by changing behavior; confirm the fix addresses the cause, not the symptom.
  2. A fix that makes the error disappear is not always correct; it may just hide the problem.

In Practice

FAQ