Unit 6 / 12

Debugging and Root Cause Analysis

Gains:

  • Ability to reduce a bug to the smallest reproducible instance and move it to AI with full proof
  • Ability to test evidence-based hypotheses with the cheapest control and find the root cause
  • Ability to resolve the root cause and secure it with a regression test rather than patching the symptom

Debugging is the process of finding out why a software behaves unexpectedly and fixing it. It is the job where a developer spends the most time and gets the most tired; Because most of the time the mistake is not where it appears, but is hidden a few steps behind. AI is a powerful thinking partner that accelerates this research — but only if you give it the right evidence. Debugging without evidence is the area where AI produces the most hallucinations.

In this unit, we establish a disciplined flow from generating the error to getting to the root cause: clarifying the symptom, gathering evidence (error message, stack trace, log, entry), generating a hypothesis, testing the hypothesis, and validating the fix. AI helps at every step; but the "fixed" decision is made by seeing that the bug has actually gone away.

Why Evidence Is Everything?

An LLM doesn't see error the way you do; He only knows what you tell him. A sentence like “The application crashes” gives the model almost no information, and the model fills the gap with a prediction — that is, a hallucination. In turn, the full error message, stack trace — a breakdown of which function calls the error occurred through, the input that triggered the error, and what was expected, etc. Given observed behavior, the model can rank true probabilities.

In debugging, think of AI as an assistant to a detective: the more evidence you present, the more accurate the hypothesis it generates. If there is no evidence, the assistant will only guess and may lead you on the wrong trail.

Tip: Before porting a bug to AI, reduce it to the smallest reproducible example. The smallest code and input that triggers the error makes things radically easier for both you and the model; most often during this reduction you find the cause yourself.

Step by Step: Root Cause Analysis Flow

  1. Clarify the symptom. "What's going on, what did you expect to happen?" Write the two in one sentence.
  2. Gather evidence. Full error message, stack trace, relevant log lines, triggering entry, version information.
  3. Have the hypothesis generated. From AI “3 possible causes that explain this symptom and how do I test for each?” ask.
  4. Test the cheapest hypothesis first. Add a log, print a value, run a test. Does the evidence confirm the hypothesis?
  5. Fix the root cause, not the symptom. Instead of silencing the symptom with a patch, address the root cause.
  6. Validate and add regression testing. See the error disappear; Then write a test that will catch that error so it doesn't come back.

Three Mini Cases

Case 1 — The stack trace led to the correct file. An application was returning a 500 error on certain requests. The developer gave the full stack trace and triggering request to the AI; The model hypothesized that the error was caused by a None value in a date parsing layer. The developer added a log to that line, verified it and solved it in 15 minutes; 2 hours were wasted the day before with unproven experiments.

Case 2 — Hallucination led to wrong track. Another developer simply wrote "database connection is dropping". The AI ​​accused a connection pool setting without any evidence; The developer spent 40 minutes tinkering with this setting. The real cause was a timeout on the network side and was only revealed by looking at the logs. Lesson: a hypothesis taken without evidence is only probable, not reliable.

Case 3 — Flaky error caught. There was a test that occasionally failed. The AI ​​was given the test code, the failure message, and the information "sometimes it passes, sometimes it fails"; the model indicated a shared time/order dependency of the tests. The review confirmed that the test was based on the system's local time. Once the clock was fixed (mocked), the test became stable.

Four Copiable Templates

Evidence-based hypothesis generation:

I'm debugging a bug. Evidence below.- Expected behavior: {{expected}}- Observed behavior: {{observed}}- Error message / stack trace: {{trace}}- Triggering input: {{input}}- Environment/version: {{version}}List the 3 MOST LIKELY root causes that explain this symptom. For each: how do I test (cheapest check) and how to fix it if it's true. If the evidence is insufficient, tell me what additional information you need.

Interpreting the stack trace:

Read this stack trace. Distinguish between which line the error PROBABLY starts at (the root) and which lines are just continuations of the chain. Suggest 1-2 places to look first. Related code:{{code}}Trace:{{trace}}

Minimal repro subtraction:

The code below produces an error. Reduce it to the SMALLEST instance that still triggers the error but discards anything unnecessary. Do not assume that every piece you remove does not affect the error, but add a note saying "if the error disappears when you remove this, that is why".{{code}}

Post-correction validation and regression testing:

Assume the root cause is {{cause}} and I make the following fix: {{fix}}.1) Does this fix actually fix the symptom, will it have any side effects?2) Write a regression test that will catch this bug in the future.

Weak prompt / Strong prompt

Weak: "The code doesn't work, why?"
Strong: "Node 20 / Express. POST /orders returns 500 when items is an empty string in the body; should have returned 400. Stack trace: TypeError: Cannot read properties of undefined (reading '0') — attached is the full trace and associated handler. Give me the 3 most likely causes that explain this symptom and how to test each. [trace + code]"

Powerful version; It gives the environment, endpoint, trigger input, exact error type, and expected behavior. The model can no longer make predictions, but analysis.

step

AI's contribution

your control

gathering evidence

What evidence is needed, reminds

Really collects evidence

hypothesis generation

List possible reasons

Prioritizes with context

hypothesis testing

Recommends test method

Operates and observes personally

correction

patch recommends

Does it solve the root cause? It's true.

regression

writes a test

Verifies that the test is broken

Solving the Root Cause, Not the Symptom

Most of the time the AI will suggest a patch that quickly silences the symptom: add a try/catch, put a null check, swallow the error. This is sometimes true, often dangerous; because the original cause remains in place and erupts again from somewhere else. With each fix, ask yourself: “Does this fix the cause of the error, or does it make it invisible?” Once you find the root cause, the fix is ​​usually smaller, more robust and permanent.

Caution: Silently swallowing an exception (empty catch) does not resolve the error; it merely conceals and makes future diagnosis impossible. If AI suggests such a "solution", don't accept it without questioning the root cause.

Common mistakes

  • Asking questions without evidence. Ambiguous sentences push the model into hallucination; Give full error, trace and input.
  • Locking in on the first hypothesis. The AI's first suggestion may not be the most likely; Start with the cheapest controllable hypothesis.
  • Patching the symptom and missing the root cause. The silenced error returns.
  • Closing the fix without verifying it. See in production-like condition that the error actually disappears.
  • Not writing regression tests. If no tests are added, the same error will silently return in later versions.

In summary

In debugging, the power of AI is directly proportional to the evidence you give it: without the full error message, stack trace, triggering input, and expected behavior, the model just speculates. Disciplined flow—clarify symptom, gather evidence, generate hypothesis, test with cheapest control, fix root cause, verify, and add regression testing—closes the bug both quickly and permanently. AI is a hypothesis generator; You are the one who decides that the bug is actually solved.

Application task

Choose a real bug you've encountered recently (or reproduce a test bug). Do the "minimum reproduction" step first; Remove the smallest code and input that triggers the error. Then get 3 possible causes and testing methods from AI with the “evidence-based hypothesis generation” template. Test the cheapest hypothesis yourself, find the root cause, fix it, and finally write a regression test that will catch this bug in the future and verify that the test is actually broken.

checklist

  • [ ] I reduce the error to the smallest reproducible sample before moving it to the AI.
  • [ ] I am adding the full error message, stack trace, input and expected behavior to the prompt.
  • [ ] I start with the cheapest controllable one, without being locked into a single hypothesis.
  • [ ] I verify that I have resolved the root cause rather than patching the symptom.
  • [ ] I observe that the fix actually fixes the bug.
  • [ ] I add a regression test for each resolved bug.