AI is excellent at explaining clean code. It is mediocre at finding the defect that only appears in one specific path. The difference is almost entirely in how you frame the question.
The Debug Code Assistant and Code Explainer tools on aihowto.pro are built around a four-part pattern that dramatically raises the hit rate. The Coding Assistant can also be used once you have isolated the bug.
The four-part debugging prompt that works
Every time you ask for help with a bug, include:
- Observed failure (exact symptom, error message or wrong output, when it happens)
- Expected behavior (what should have happened instead)
- Minimal reproduction (the smallest input or sequence that triggers it)
- Question (not "fix it": "identify the line that causes X and explain why the data is in that state at that point")
Skip any of the four and the model fills the gap with generic advice.
Real example that most people get wrong
Bad prompt:
"Here is my function. It is returning the wrong total sometimes. Can you fix it?"
Good prompt:
Observed: When a user has three line items and one of them has a 10% discount applied at the line level, the order total is $4.20 too high.
Expected: The discount should reduce the subtotal before tax is calculated. The final total should be exactly the sum of (price × qty × (1 - discount)) for each line + tax.
Minimal repro:
- Item A: $50 × 1, no discount
- Item B: $30 × 2, 10% discount
- Tax rate 8.5%
- Current total returned: 50 + 54 + 4.59 = 108.59
- Correct should be: 50 + 54*0.9 = 50 + 48.6 = 98.6 + tax 8.38 = 106.98
Question: Walk through the exact calculation path the code takes for the discounted line and show me the line number where the discount is being applied after tax instead of before.
The second prompt usually surfaces the bug in one shot. The first produces "make sure you are using the discount field correctly."
Table: prompt shape vs. diagnosis quality
| Prompt shape | Typical response | Diagnosis rate |
|---|---|---|
| Code + "fix this" | Refactored version with no explanation of root cause | Low |
| Code + "what's wrong" | List of style issues and possible improvements | Low |
| Observed + expected + repro + "find the line" | Points to the exact calculation or state mutation | High |
| + "explain the data flow at failure time" | Shows the wrong assumption the code made | Highest |
The pattern in practice with the aihowto tools
Use the Debug Code Assistant exactly as written:
- Paste the observed vs expected.
- Paste the smallest code snippet that reproduces (never the entire 800-line file first).
- Let it ask clarifying questions if the repro is still ambiguous.
- Once it names a line or state, ask the Explain Code tool to trace only that slice with the actual values from your repro.
You will iterate faster than pasting the whole file and hoping.
Common classes of bugs AI finds quickly when framed correctly
- Off-by-one in date or index calculations (give the exact input dates)
- Discount/tax/fee order of operations (show the numbers before and after)
- Async race where a value is read before write completes (include the timing you saw)
- Conditional that looks right but the variable name is shadowed (paste the two definitions)
- Wrong assumption about input shape (give one real payload that fails)
When to stop using AI and add a test or log
If the model proposes three different root causes on three runs, you have not given it enough signal. Add a console.log or a unit test that prints the intermediate values at the point of failure, then paste those values back in.
AI is fast at hypothesis generation. It is not a replacement for measurement.
One rule that prevents 70% of wasted debugging turns
Never ask "why is this broken?" Ask "at the moment the total is wrong, what is the value of X and why did the code choose path Y?"
That single rephrasing moves the model from "here are five things that could be wrong" to "the discount was applied to the post-tax amount on line 47 because the reduce function runs after the tax step."
Use the Debug Code Assistant with the four-part structure every time. The 90 seconds you spend writing the observed/expected/repro section saves the 20 minutes of back-and-forth that generic prompts always produce.