Debugging with AI: From Stack Traces to Root Causes
The Debugging Timewarp
You've all been there. A bug lands in production. The error message is cryptic. You have a stack trace that's 50 lines deep through libraries you don't own. The production logs show it happened 20 minutes ago but you can't reproduce it locally. You spend two hours digging through code paths that theoretically shouldn't execute, trying to understand how the system got into this state.
Meanwhile, your incident responders are asking if the site is down (no, just slow), whether it's affecting customers (yes, 0.3% of requests), whether you need to page more people (not yet).
Then someone finds the issue: a race condition between two async tasks, triggered only when the database is slow, which happens once a week when backups run. A one-line fix. Six-hour firefight.
This is where AI fundamentally changes your debugging workflow. Not by magically finding the bug (you still need domain knowledge for that). But by eliminating the tedious hours of pattern matching, stack trace decoding, and hypothesis testing.
What AI is Actually Good At
AI excels at debugging tasks that are mechanical:
- Decoding error messages and explaining what they mean
- Analyzing stack traces and identifying the actual failure point
- Recognizing patterns ("this looks like a race condition" or "typical database connection pool exhaustion")
- Suggesting hypotheses based on error patterns
- Identifying what logs to look for to test a hypothesis
- Spotting issues in code (null pointer, off-by-one, incorrect type conversion)
What it's not good at:
- Understanding your specific system architecture and how components interact
- Knowing which constraints are real and which are self-imposed
- Having domain knowledge about your business logic
- Remembering the 3 AM decision made two years ago that affects this code
You combine AI's pattern recognition with your domain knowledge, and you get fast debugging.
The AI Debugging Advantage: You supply the context. The AI supplies rapid hypothesis generation and pattern matching. You test the hypotheses with domain knowledge. This loop is 3-4x faster than manual debugging.
The Debugging Workflow
Step 1: Gather Information
Before asking the AI anything, collect data:
- The full error message and stack trace
- What was happening when the error occurred
- How often does it happen (always, intermittently, under load, etc.)
- Relevant code sections (the function mentioned in the stack trace, the caller, dependencies)
- Environmental context (was anything unusual happening, traffic spike, deployment, configuration change?)
Step 2: Ask the AI
Now you ask the AI to analyze. Here's a pattern that works:
"Analyze this error and suggest likely causes:
Error message:
[full error message]
Stack trace:
[full stack trace]
Context:
- This happens [always/intermittently/under load]
- It started [when? after what change?]
- The system was doing [what was happening at the time]
- Environment: [production/staging/local, relevant config]
Here's the code involved:
[relevant function, caller, dependencies]
What are the most likely causes? For each cause, suggest what logs to check and what code to examine."
The AI produces a prioritized list of hypotheses with specific steps to verify each one.
Step 3: Test Hypotheses
Instead of randomly digging through code, you systematically test the AI's hypotheses:
- Check the logs it suggested checking
- Look at the code it suggested examining
- Run any diagnostic commands it recommended
Most of the time, you'll find the bug in the first two hypotheses. If not, iterate: "The first hypothesis was wrong because [finding]. Try again with this new information."
Step 4: Verify the Fix
Once you've identified the root cause, ask the AI to help with the fix:
"Here's the bug:
[explain the issue]
Here's the code that needs fixing:
[paste code]
Suggest a fix that:
- Solves the root cause
- Doesn't introduce new bugs
- Maintains performance
- Follows our [language/framework] conventions"
You get a proposed fix. Verify it makes sense, test it, deploy it.
Real Debugging Examples
Example 1: The Intermittent Crash
Situation: Your payment processing service crashes randomly, about once a day, at 3am. No clear pattern. The error is always "connection timeout".
Initial Attempt (without AI): Spend 8 hours checking connection pool configuration, database load, network connectivity. Nothing obviously wrong. Eventually find that a batch job runs at 3:15am and exhausts the connection pool. But by then it's 11pm and you're exhausted.
With AI:
"Our payment service crashes daily with 'connection timeout'. It happens around the same time (3am). Here's the stack trace:
[paste stack trace]
Here's the code that times out:
[paste connection acquisition code]
What would cause timeouts at a specific time? What should I monitor or check?"
AI's response: "Connection pool exhaustion is the likely cause. A batch job running at the same time would do this. Check:
1. What jobs are scheduled around 3am?
2. How many connections does your pool allow?
3. Are connections being released properly?"
You check. Find the batch job. Fix it to use a separate connection pool. Done. Two hours instead of eight.
Example 2: The Null Pointer in Production
Situation: A NullPointerException in a service that's been running for 3 years. The stack trace shows it's in the middle of processing a user record. The null is happening somewhere in a 200-line function.
Without AI: Spend an hour reading the function, trying to figure out which variable could be null and why. The code has multiple null checks but not everywhere. You add defensive null checks everywhere until the crash stops happening. Ship a band-aid.
With AI:
"This function is throwing a NullPointerException. Here's the stack trace and the function:
[paste stack trace and function]
Which variable is most likely null? Why would it be null? Should I add a null check or fix the root cause?"
AI's response: "The userAddress variable is null. This happens when a user record has no address set. The function should either:
1. Check if address is null before using it
2. Require an address when creating a user record
What's your domain logic?"
You know users can have no address. So you need option 1. The AI suggests where to add the check and what to do when address is null. You implement the proper fix instead of a band-aid.
Example 3: The Performance Regression
Situation: After a deployment, a critical endpoint got 10x slower. No new errors. Just slow. The code change was small. You added one feature.
Without AI: You profile the function. Spend 3 hours analyzing flame graphs. Eventually notice your new code is making a database query in a loop. Classic N+1 problem. Fix it. But the time cost is high.
With AI:
"This endpoint got 10x slower after this change:
[paste diff]
Old code didn't make any new external calls. New code must be the issue. Analyze the new code and suggest what could cause a 10x slowdown."
AI's response: "The new code loops through results and makes a database query on each iteration. This is an N+1 query problem. You're making 100 queries instead of 1. Fix: Load all related data in a single query before the loop."
You implement the batch query. Endpoint goes back to baseline. Thirty minutes instead of three hours.
The Debugging Prompt Template
Over time, you'll develop a standard prompt for debugging. Here's one that works:
"I'm debugging [brief description of issue].
Error message: [full error message]
Stack trace: [full stack trace]
What I know:
- This happens [when/under what conditions]
- It started [when, what changed]
- Environment: [where it happens]
- Related code: [paste relevant code]
What are the 3-5 most likely causes? For each, what should I check?"
The AI gives you a prioritized list of hypotheses. You test them. Most of the time you'll find it in the top two.
When AI Can't Help (And What to Do)
AI gets stuck when:
1. The bug is domain-specific. "This calculation is wrong" without understanding the business logic. You need to explain what the calculation should be. Then AI can spot the logic error.
2. The bug is architectural. "These two services are out of sync" requires understanding how they're supposed to stay in sync. You need to explain the contract. Then AI can spot the violation.
3. The bug is in unfamiliar code. Legacy system with no documentation. AI can analyze the code but won't understand intent. You need to explain what the code is supposed to do. Then AI can spot the bug.
What to do: Provide context. The more you explain about what the system should be doing and the constraints it operates under, the better AI can help. A prompt that includes "this is an e-commerce platform that needs to handle 10,000 orders per minute" is more useful than one that's just a stack trace.
Key Insight
AI transforms debugging from "spend hours reading code hoping to find the bug" to "generate hypotheses systematically and test them." This turns debugging into a structured activity instead of a guess-and-check session.
Building Debugging Discipline
The best debugging teams combine AI assistance with disciplined practices:
1. Log Strategically
When something breaks, logs are your primary tool. Log:
- Entry/exit of critical functions
- Parameter values
- State changes
- Error conditions (before throwing, log what happened)
When you ask AI to help, provide relevant logs. The AI can see patterns in logs that humans miss.
2. Create Reproducible Test Cases
If the bug is intermittent, create a test that reliably reproduces it. If you can't reproduce it locally, ask the AI: "This happens intermittently in production but never in my test environment. What environmental factors might matter?"
3. Track Changes Carefully
When debugging a regression, know exactly what changed. A diff is invaluable. "What could cause a 10x slowdown with this change?" is much easier to answer than "something is slow."
4. Use Monitoring and Metrics
Dashboards that show latency, error rates, resource usage, help you spot patterns. When asking AI for help, include what the metrics show: "Latency spiked to 10s, error rate went from 0.1% to 2%, database connections are at 95% of pool."
What to Do Monday Morning
- The next time you encounter a confusing error, don't start random code reading. Instead, collect the error message, stack trace, and context. Ask your AI tool to analyze it and suggest hypotheses. Test the hypotheses. See how much faster you find the bug.
- Create a debugging prompt template for your team. Document what information is most useful (error message, stack trace, logs, code, context). Make it a ritual that everyone follows. Share findings in a team Slack channel so others learn.
- When someone on your team gets stuck debugging something, have them ask AI for hypotheses before they dive into 6 hours of code reading. Help them learn the structured approach.
- Next regression or intermittent bug your team faces, measure how long it takes with AI assistance versus without. Track it. You'll see the time savings compound.
FAQ
Q: Can AI find all bugs?
A: No. AI is good at pattern recognition and mechanical analysis. It's bad at understanding context and domain logic. Use AI to generate hypotheses and suggest fixes. Use humans to evaluate whether the fix is right for your domain.
Q: What if I don't have good logs?
A: Add them. Good logs are a prerequisite for efficient debugging. Ask AI: "What should I log in this code to make bugs easier to find?" then implement it. Future bugs will be faster to find.
Q: How much context should I provide?
A: More is better, up to a reasonable limit. Include: error message, stack trace, relevant code, what was happening, when it started, environmental context. Don't include irrelevant code or lengthy logs, stick to what's relevant to the problem.
Q: Should I trust the AI's suggested fix?
A: No. Verify it makes sense. Test it. For critical systems, have a human review it. The AI is a tool that proposes solutions; you verify they're correct for your context.
Q: What if the bug is in external library code?
A: Ask AI: "This error is in [library]. What could I be doing wrong to trigger this?" Often the bug isn't in the library. You're using it incorrectly. AI can spot usage issues. If it's truly a library bug, AI can help you craft a workaround.
On This Page
Watch the Lecture
The Debugging Timewarp
The Debugging Workflow
Real Debugging Examples
The Debugging Prompt Template
When AI Can't Help
Building Debugging Discipline
What to Do Monday Morning
FAQ
Chapter Details
Part of
Skill.re