HOW-TO · DEV

How to use an AI assistant to analyze a stack trace and pinpoint the root cause of a bug

intermediate15 minBy Fredoline Eruo
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES

Stack trace from an error (application crash, test failure, or runtime exception), AI coding assistant with access to the relevant codebase

What this does

A stack trace contains the call chain leading to an error, including file paths, line numbers, function names, and error messages. Reading a stack trace from the bottom up reveals the origin of the failure, while reading from the top shows the execution path that propagated it. An AI assistant can accelerate this process by identifying the root cause line, ignoring irrelevant library frames, and suggesting a fix based on the error type and surrounding code context.

Steps

  1. Capture the complete stack trace as plain text. Include the full output including the exception type, message, and all frames. Partial stack traces make it difficult for the AI to identify the root cause correctly.

  2. Identify the project-specific frames in the stack trace. These are the lines referencing files within the repository (typically the first non-library frames from the bottom). The bottommost application frame is the root cause location.

  3. Paste the entire stack trace into the AI assistant along with the relevant source files or the specific error message. Provide context by describing what the application was doing when the error occurred.

  4. Ask the AI assistant to identify the root cause. Use a query such as "What is the root cause of this stack trace and which line triggered the error?"

  5. Review the AI response and confirm it correctly identifies the bottommost application frame in the stack trace. This is the line where the original error was thrown.

  6. Ask the AI to trace the call chain: "Walk through the call stack from the error origin to the entry point." This confirms understanding of the execution flow and validates the AI's interpretation.

  7. Request specific fix suggestions: "What change to the code on line X of file Y would prevent this error?"

  8. If the error is caused by a null or undefined value, ask the AI to identify where the value should be validated or defaulted.

  9. Apply the suggested fix locally and reproduce the scenario to verify the error is resolved.

  10. If the error persists, provide the updated stack trace to the AI and repeat the analysis with the new information.

Verification

Run the application or test that previously produced the stack trace. The error should not recur. If the fix introduces a new error, a new stack trace should be analyzed following the same process.

Common failures

  • Providing only the error message without the stack trace: The error message alone (e.g., "TypeError: undefined is not a function") does not provide the location context needed for root cause analysis. Always provide the full stack trace.
  • AI confuses library frames with application frames: Library frames should be mostly ignored when diagnosing application bugs. If the AI focuses on library internals, explicitly instruct it to identify only frames from the repository and to ignore third-party package frames.
  • Stack trace from production with minified code: Minified stack traces show obfuscated names and line numbers that do not correspond to source files. Ensure source maps are enabled in the build pipeline so that production errors produce readable traces.
  • Asynchronous stack traces missing async boundaries: In Node.js or browser JavaScript, async functions can cause stack traces to be split or incomplete. Ask the AI specifically about async boundaries if the error appears to span multiple event loop ticks.
  • AI proposes a fix that masks the error rather than resolving it: Using a try-catch with an empty catch block suppresses the error but does not fix the underlying cause. Verify the fix addresses the root condition, not just the symptom.

Related guides