Gains:
- Ability to effectively describe a bug to AI with error message, stack trace, and smallest reproduction instance
- Ability to run a systematic debugging flow with AI to find the root cause by hypothesizing and narrowing it down step by step
- Ability to verify that the fix the AI suggested actually fixed the problem by reproducing and regression testing
Debugging is the task of finding out why a program is behaving differently than expected and fixing it, and it consumes a lot of most engineers' time. Good debugging is not based on a guessing game, but on systematic narrowing down: clarify the symptom, hypothesize, test the hypothesis, get to the root cause. AI is a very powerful partner in this cycle; But only if you give him the correct information. Saying “code doesn't work, fix it” forces the AI to guess and make blanket suggestions. Give it the full error message, the stack trace, and the smallest reproduction sample, and together you'll find the root cause.
In this unit we will see how to effectively describe a bug to AI, narrow down hypotheses step by step, and verify through regression testing that the proposed fix actually solves the problem. Remember: "fixing" a bug and "suppressing the symptom of the bug" are two different things; Correction made without finding the root cause moves the error to another place.
Concepts: Stack trace: A dump showing which functions were called in what order at the time of the error. Minimal repro: The simplest, shortest code/input that triggers the error. Root cause: The real source of the problem, not the symptom. Regression testing: Testing that ensures that the same error does not repeat.
Describing a Bug to AI
The likelihood of AI finding the root cause is directly proportional to the quality of the information you provide. A good error description includes: what you tried to do, what you were expecting, what happened, the exact error text and stack trace, the code involved, the environment (language/version/OS), and the smallest sample that produced the error.
- Clarify the symptom. In the format "Expected X, actualized Y".
- Paste the full error text and stack trace. Don't shorten it, censor it, but don't break the structure.
- Give the smallest reproduction. Minimum input and code that triggers the error.
- Specify the environment. Language version, library version, runtime environment.
Effective error description prompt: "I'm debugging a bug. Information:- What I'm trying to do: [X]- Expected behavior: [Y]- Actual behavior: [Z]- Full error message and stack trace: [paste]- Environment: [language/version, library/version]- Minimum code involved: [code] Don't give me a direct fix. First list the 3 most likely root causes in order of probability and tell me what check to verify for each."
Narrowing Flow by Hypothesis
Systematic debugging is the art of eliminating possibilities one by one. Use AI to generate hypotheses and design the experiment to test each hypothesis; Then run the experiment and return the result. This cycle is much faster than the habit of making random changes and stopping, called "shotgun debugging".
Binary search (bisection) helper prompt: "This error was not there yesterday, it is there today. I want to find out which of the last 20 changes brought the error with bisect. Give me a step-by-step plan: which point should I test, which half should I go to depending on the result. Also tell me exactly what to check at each step."
Log insertion strategy prompt: "I cannot find the error because I cannot see the intermediate values in this function. Tell me at which points I need to add log lines that print which variables. Add a 'what will I learn from this log' explanation for each log. Also specify the warnings that will prevent me from logging confidential data."
Tip: If you can't resolve an error, most of the time the problem is somewhere you assumed incorrectly. Ask the AI “what assumption of mine could be wrong?” Asking will break your blindness. The toughest mistakes hide in the place where you say "I'm sure this is working right".
Weak Prompt / Strong Prompt
WEAK:"My code gives an error, fix it: [200 lines of code]"(Result: AI does not know what error it is, what is expected; it gives general suggestions based on guesswork, most of them are useless.)STRONG:"I get NullPointerException. Expected: user list should be returned. Actual: Explodes on call to getUsers(). Stack trace: [paste].Environment: Java 17. Minimum repetition: It happens when the users list is empty, but not when it is full. Related 15 lines: [code]. Explain the root cause and why emptylist is triggered, then suggest a fix."
The powerful prompt puts the error in context: in which case it happens (empty list), in which case it does not happen (full list). This single clue ("happens when empty") almost directly points to the root cause. Since this information is not available in the weak prompt, AI makes a blind guess.
Verifying the Fix
A fix is only a real fix if it does three things:
control
Question
How to verify
Is the error gone?
Does the same entry work now?
Run minimal repro again
No new errors?
Is anything else broken?
Run the entire test suite
Won't it repeat?
Will the same error occur again?
Add regression test for this scenario
Corrections made without finding the root cause often suppress the symptom. For example, glossing over a null error with "skip if null" makes the real reason, "why is the data coming null?" invisible, and the error reoccurs elsewhere.
Mini Cases
Case 1 — The symptom suppression trap. A team silences an occasional null error with a try-catch; The error disappears but after 2 weeks the data appears missing. The real reason is that a service returns null on timeout. When you ask AI "why is it getting null?", the root cause emerges; The real fix takes 1 hour but is permanent.
Case 2 — Minimal repro power. A developer can't fix a bug that says "it crashes every once in a while." It reduces the error to the smallest input with AI's suggestion: the problem only occurs with filenames containing Turkish characters (encoding error). When 300 lines of uncertainty are reduced to 5 lines of definitive repro, the solution becomes obvious.
Case 3 — Anti-regression test. AI fixes a date calculation error. The engineer is not satisfied with this; adds a regression test for the erroneous scenario (month end, January 31 + 1 month). When another change touches the same area 4 months later, the test turns red and the bug is caught before it reaches production.
Common mistakes
- It means "it's not working, fix it". Without error text, expectation and repro, AI guesses.
- Not giving the stack trace. The stack trace often directly indicates the root cause.
- Keep making random changes. Experiments without establishing a hypothesis waste time.
- Suppressing the symptom and missing the root cause. The error is reborn elsewhere.
- Not securing the fix with regression testing. The same error comes back silently in the future.
In summary
Effective debugging is systematic narrowing, not guessing. Giving the AI the full error text, stack trace, minimal reproduction and environment information exponentially increases the chances of finding the root cause. Use AI to generate hypotheses and design the experiment to test each hypothesis; You run the experiment. Consider a fix "done" only when you see that the bug is gone, no new bugs are introduced, and it is protected by regression testing.
Application task
Consider a real or artificial error. First reduce the error to the smallest reproduction (in which input it occurs, in which it does not). Using the effective bug recipe prompt, ask the AI for 3 root cause hypotheses and a verification step for each. Find the root cause by testing the hypotheses one by one, fix it, then write and run a regression test for this scenario to show that the bug is gone and the test provides protection.
checklist
- [ ] I clarified the symptom as "expected vs realized".
- [ ] I gave the full error text and stack trace to the AI.
- [ ] I reduced the error to the smallest reproduction.
- [ ] By testing the hypotheses one by one, I found the root cause.
- [ ] Instead of suppressing the symptom, I fixed the root cause.
- [ ] I added and ran a regression test for the same error.