How to Debug AI Agents in Production (2026)

Why AI agents fail without errors, and a step-by-step method to trace a bad run back to the first wrong step and fix it for good.

On this page
  1. Why AI agents are hard to debug
  2. The six ways agents fail in production
  3. What to capture before anything breaks
  4. Step by step: from a bad outcome to the root cause
  5. Catch failures before users report them
  6. When the cause is the model, not your code
  7. How Voight helps
  8. Checklist

An AI agent rarely crashes. It returns HTTP 200, writes a confident answer and moves on, while somewhere in the middle it picked the wrong tool, misread a tool result or looped on the same call twelve times. By the time a user reports the bad outcome, the step that caused it is buried under dozens of others.

This guide is a method for that situation: how to go from "the agent did something wrong" to the exact step that went wrong, why it happened, and how to make sure it does not happen again. It works for coding agents, autonomous agents and LLM apps with tool calling.

Why AI agents are hard to debug

Traditional services fail loudly: an exception, a 5xx, a stack trace that points at a line. Agents fail quietly, for four reasons:

  • The failure and its cause are far apart. The wrong answer appears in step 30; the cause is a tool result the model misread in step 7.
  • Every run is different. The same prompt can take a different path tomorrow, so "it works on my machine" means very little.
  • The model hides the error. A tool returns Error: rate limited and the model treats it as data, apologises, or invents a result instead of failing.
  • The cost is the symptom. Often the first signal is a bill: an agent that retries in a loop burns tokens long before anyone reads its output.

Logs written for request-response services capture the symptom. To find the cause you need the whole run, step by step.

The six ways agents fail in production

FailureWhat you seeWhere the cause usually is
Wrong tool or wrong argumentsA plausible answer built on the wrong dataThe tool description, or ambiguous context in the prompt
Silent tool failureThe agent "succeeds" but the result is wrongA tool output (error text, empty list) the model read as valid data
Loops and retriesLong runs, repeated calls, a cost spikeNo stop condition, or a tool that keeps returning the same error
Context problemsThe agent ignores facts it was givenTruncated, stale or conflicting context, or a prompt that drifted
Hung stepsA run that never finishesA tool call that started and never returned
Model changesBehaviour shifts after a model or version updateThe model, not your code: compare runs before and after

Most incidents are the first three. Knowing the category early tells you where to look.

What to capture before anything breaks

You cannot debug a run you did not record. For every step of every run, capture:

  • The input: the prompt or message the model saw (scrubbed of secrets and personal data).
  • The model and version that answered.
  • The tool call: tool name, arguments, and the raw output or error it returned.
  • Timing: when the step started and how long it took.
  • Tokens and cost, including cache reads and writes.
  • The outcome: success, failure, or pending.

Then group the steps. A trace is all the work the agent did for one task; a session is everything in one process lifetime (one Claude Code launch, one bot run). Add a user or tenant tag when the agent serves customers, so you can answer "which customer hit this?".

Step by step: from a bad outcome to the root cause

Step 1: Start from the outcome and find its trace

Take the bad result (a wrong answer, a broken commit, a failed task) and locate the trace that produced it. A trace id in your logs, the user tag or the time window is usually enough.

Step 2: Read the trace from the top, not from the bottom

The last step shows where the damage became visible. The first step that diverged from what a correct run would do is where the cause is. Walk the run in order and stop at the first decision you would not have made.

Step 3: Check the tool boundary

At that step, compare three things: what the model asked for (tool and arguments), what the tool returned, and what the model concluded from it. Most root causes live in that gap: an error string read as data, an empty result read as "nothing to do", arguments built from the wrong field.

Step 4: Compare with a good run

Find a successful run of the same task and put the two side by side. Where the paths split, look at what was different in the input: a longer context, a different file, a slower tool, a newer model.

Stepping through the recorded run is usually enough. Re-running the task to reproduce the failure costs tokens, can repeat side effects and may take a different path anyway; our guide on replaying an AI agent session covers when a re-run is worth it and how to make it safe.

Step 5: Classify the cause and fix it at the right layer

  • Prompt or context: make the instruction explicit, or remove the conflicting context.
  • Tool contract: return structured errors the model cannot mistake for data, and describe the tool more precisely.
  • Guardrail: add a stop condition, a maximum number of retries, or a check before an irreversible action.
  • Infrastructure: timeouts, rate limits and credentials are often the real cause behind "the model behaved strangely".

Step 6: Make the fix stick

Turn the failing run into a regression case, and keep watching the same failure pattern after the fix. A fix that holds for five quiet days is very different from one that held for an afternoon.

Catch failures before users report them

Reading traces one by one does not scale. Two habits change that:

  1. Group failures by pattern. Ten runs failing on the same tool with the same error are one problem, not ten. Grouping turns a stream of errors into a short list of issues, ordered by how often they happen.
  2. Watch each agent against its own baseline. A failure rate, a cost or a run length that is normal for one agent is an incident for another. Alert on deviation: failure rate up, cost per run up, steps that start and never finish.

When the cause is the model, not your code

Models change under agents more often than code does: a provider retires an alias, a default model moves to a newer version, or a fallback sends some requests to a different model. The symptom is a behaviour shift with no deploy on your side.

  • Record the exact model id on every step, as the provider returned it, not the alias you asked for.
  • Pin versions in production where the provider offers dated ids, and move to a new one on purpose.
  • Compare runs before and after the change on the same tasks: tool choice, step count, failure rate and cost per run.
  • Re-check your tool contracts after a model change. A newer model can read the same error string differently.

How Voight helps

Voight Observability is built for this loop. It records every prompt, tool call, decision, cost and error from coding agents, autonomous agents and LLM apps, and groups them into sessions and traces. On top of that:

  • Issues. Failures are grouped by pattern into an issue queue within about five minutes of the first failure. Each issue has a lifecycle: open, fix attempted, resolved, and verified automatically after five quiet days, or marked as recurred if it comes back.
  • Diagnose with AI. One click analyses the failed runs behind an issue and returns a root cause, a suggested fix, a severity and a confidence score. When the runs were captured with no prompt or response content (the most private setting), the diagnosis says so and works from tool names and timings only.
  • Fix with your agent. The diagnosis can be copied as a ready prompt for Claude Code, Cursor or Codex, and the issue is marked as "fix attempted" so you can see whether the fix held.
  • Privacy levels. Minimal (metadata only), Standard (content with secrets and personal data scrubbed on your machine before sending) or Full.

Anomaly alerts per agent. Every five minutes, each agent is checked against its own recent history. Four rules raise an alert, at low, medium or high severity depending on how far the agent is from normal:

RuleFires when
Error rate10% or more of the tool runs in the last hour failed, with at least five finished runs
Aborted tool calls20% or more of the tool calls in the last hour started and never finished, and at least twice the agent's usual rate
Cost spikeSpend in the last 24 hours is three times the daily average of the six days before, or more (and at least $1)
Event surgeThe last hour has three times the agent's usual hourly volume over seven days, or more

Today the error-rate and aborted-call rules run on Claude Code sessions, whose hooks report the start and the end of every tool call. Every other source (Cursor, Codex, wrapped OpenAI and Anthropic clients, the HTTP API) gets the cost and volume rules.

On the Pro and Enterprise plans, new alerts can also go to a Slack channel, connected from the dashboard with Add to Slack. An alert notifies once; while the condition lasts it updates in place instead of sending again.

To connect a coding agent:

Terminal
npx -y @voightxyz/sdk setup

For an LLM app, wrap your OpenAI or Anthropic client once; for anything else, send events to the HTTP API. The quickstart covers all four paths.

Checklist

  • Every step records input, model, tool call, output, timing, tokens and outcome.
  • Steps are grouped into traces (per task) and sessions (per process).
  • Tools return structured errors, never error text the model can read as data.
  • Agents have a stop condition and a retry limit.
  • Failures are grouped by pattern, not read one by one.
  • Each agent is watched against its own baseline.
  • Every fix is followed by a quiet period before it counts as verified.

See what your agents do. In one command.

Connect Claude Code, Codex or Cursor, or wrap your OpenAI and Anthropic clients. Traces show up in seconds.

npx -y @voightxyz/sdk setup