Ravindra BagaleCourses & study guides Track your progress
AI CODING ASSISTANTS · CHAPTER 5 · WORKFLOWS

How to Debug Code with AI

Stuck on an error? Paste the failing test and the short stack trace — not the whole project. The AI suggests a fix; you prove it by re-running the test.

In short: reproduce → collect error lines → ask for one hypothesis → apply a small fix → re-run tests. Never paste secrets or .env files into the chat.

Why AI helps debugging

WHY

Reading a long stack trace is slow when you are tired. An AI is good at pattern-matching common mistakes (wrong type, off-by-one, float money math). You stay in charge: it guesses, you verify.

What to give the AI

WHAT
  • Failing test name: so it knows the expected behaviour.
  • Error / stack lines: the exception type and the 5–15 lines that point at your file.
  • The small function: not the entire repository.
  • What you already tried: one sentence, to avoid looping the same bad fix.

How debugging flows

1 · Reproduce failing test or minimal script 2 · Copy error lines not the whole log 3 · Ask for 1 fix hypothesis + patch 4 · Apply + re-run tests decide 5 · New chat if stuck fresh context

Example: GST total was wrong

BUG REPORT PROMPT (COPY)

Language: Python 3.12. Failing test: test_rounds_half_up. Error: AssertionError — expected Decimal('117.99'), got 117.9882…. Function below uses float. Fix money math with Decimal, keep the same API, return code only.

buggy idea (do not ship)
# Common AI mistake: float money math
def gst_total(price, rate_percent):
    return round(price + price * rate_percent / 100, 2)
# 99.99 * 0.18 in float can surprise you — use Decimal for money.
gst.py (fixed, tested here)
"""Indian GST on a price. Rate is percent, e.g. 18 for 18%."""
from decimal import Decimal, ROUND_HALF_UP


def gst_total(price: str | Decimal, rate_percent: str | Decimal) -> Decimal:
    p = Decimal(str(price))
    r = Decimal(str(rate_percent))
    if p < 0 or r < 0:
        raise ValueError("price and rate must be >= 0")
    tax = (p * r / Decimal("100")).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
    return (p + tax).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
test_gst.py
from decimal import Decimal
import pytest
from gst import gst_total

def test_18_percent():
    assert gst_total("100", "18") == Decimal("118.00")

def test_rounds_half_up():
    assert gst_total("99.99", "18") == Decimal("117.99")

def test_zero_rate():
    assert gst_total("50", "0") == Decimal("50.00")

def test_negative_raises():
    with pytest.raises(ValueError):
        gst_total("-1", "18")
cd code/debug
python3 -m pytest -q
# -> 4 passed

How to debug step by step

HOW
  1. Make it fail the same way twice with a test or a 5-line script.
  2. Copy only the useful error lines (file, line, exception). Drop huge HTML dumps.
  3. Ask for one hypothesis, not five random rewrites.
  4. Apply the smallest patch. Reject drive-by refactors.
  5. Re-run the same test. If it still fails, paste the new error only.
  6. Start a new chat when the thread is full of wrong guesses.

Where to debug in real tools

  1. VS Code + Copilot / Claude Code / Cursor: select the function, open chat or inline chat, paste the error.
  2. Agent mode: allow it to run the test command; keep approval on for shell.
  3. Terminal CLI (Claude Code): run from the project root so it can see the failing test file.
  4. Settings: put the test command in project rules so you do not retype it.
DoDon't
Paste failing test + short stackPaste .env, tokens, or full production logs
One fix round at a time“Rewrite the module from scratch” on first try
Re-run the exact same testAssume the chat “looks correct”
Rule: AI can invent a confident wrong cause. If the stack points at line 40, do not accept a rewrite of an unrelated file.

Quick quiz

1. What is the minimum to paste?

Failing test (or expected behaviour) plus the short error/stack lines.

2. Why not paste the whole repo?

Noise, cost, and risk of leaking secrets.

3. How do you know the fix worked?

The same failing test now passes.

4. What if the chat keeps guessing wrong?

Start a new chat with only the latest error and the small function.

Save tokens and money

SMART AI USE

Send the error and the function, not megabytes of logs. One hypothesis per turn. Prefer inline chat for a single function; save agents for multi-file bugs.

Save these cards

INTERVIEW CARDAI Coding

How do you debug with AI?

Reproduce, paste short error + function, ask one fix.
Prove with the same test; never paste secrets.
Ravindra Bagale · ravindrabagale.com/ai-coding
COMPARISON CARDAI Coding

Guessing vs verifying

Guess: chat explains a possible cause.
Verify: re-run the failing test after a small patch.
Ravindra Bagale · ravindrabagale.com/ai-coding
REVISION CARDAI Coding

Debug in 3

Reproduce.
Paste error + small code.
Re-run tests.
Ravindra Bagale · ravindrabagale.com/ai-coding