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
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
- 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
Example: GST total was wrong
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.
# 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.pyfrom 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 passedHow to debug step by step
- Make it fail the same way twice with a test or a 5-line script.
- Copy only the useful error lines (file, line, exception). Drop huge HTML dumps.
- Ask for one hypothesis, not five random rewrites.
- Apply the smallest patch. Reject drive-by refactors.
- Re-run the same test. If it still fails, paste the new error only.
- Start a new chat when the thread is full of wrong guesses.
Where to debug in real tools
- VS Code + Copilot / Claude Code / Cursor: select the function, open chat or inline chat, paste the error.
- Agent mode: allow it to run the test command; keep approval on for shell.
- Terminal CLI (Claude Code): run from the project root so it can see the failing test file.
- Settings: put the test command in project rules so you do not retype it.
| Do | Don't |
|---|---|
| Paste failing test + short stack | Paste .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 test | Assume the chat “looks correct” |
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
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.