How to debug an AI agent in production: a 6-step incident workflow

2026-11-30来源 dev.toEnglish

Production debugging starts when the user-visible result is wrong, incomplete, unsafe, or too slow—not when a model request returns an error. The job is to protect users first, reconstruct the path, identify the mechanism, and verify that the fix changes the outcome without creating a new failure.

At a glance

This guide is for on-call engineers and incident responders. It helps you move from symptom to mechanism to proof so you can leave with a contained incident, a reproducible hypothesis, and a fix that has a verification plan.

Before you start What to define What good looks like
A user report, alert, or representative failed run Impact, trace boundary, comparison case, owner, and stop condition Users are protected while the team explains the failure and proves the change

The debugging loop

Use Stabilize → Reconstruct → Compare → Correct → Verify:

  1. Stabilize: protect users with pause, fallback, rollback, or a narrower cohort.
  2. Reconstruct: follow the parent task through evidence, decisions, tools, retries, and state.
  3. Compare: contrast the failing path with a healthy run from the same workflow and release.
  4. Correct: address the mechanism in code, data, prompt, policy, dependency, or operations.
  5. Verify: replay the case, test neighbors, and watch the production signal that exposed it.

Decision rule: Do not change the prompt while the failure boundary is still unknown. First preserve the path that proves what happened.

1. Stabilize users before diagnosis

Choose the smallest reversible action that limits harm. Pause a high-consequence action, roll back a release, narrow the affected cohort, route to human review, or activate a known-safe fallback. Record the start time, impact, owner, affected workflow, and decision so diagnosis does not erase incident control.

Situation First containment Avoid
Active harmful action Pause the action boundary and require approval Repeating the same call to gather more logs
Release regression Roll back or narrow exposure Editing production prompts without a baseline
Dependency outage Use bounded fallback or degraded state Infinite retries that multiply load
Unclear impact Sample and classify representative runs Treating one vivid transcript as the distribution

The AI agent reliability guide covers bounded retries and terminal states. Containment is part of debugging because an uncontained investigation changes the evidence while the incident is still active.

2. Reconstruct the execution timeline

Open the parent task, not just the last model response. Preserve the user request or input class, expected outcome, release and prompt versions, retrieved evidence, model calls, tool requests and responses, validation, retries, approvals, timing, final state, and user effect. Redact secrets and use protected references for sensitive content.

Timeline question Evidence to open
What task was the system attempting? Parent run, input class, expected outcome
Which transition changed the path? Child spans, retrieval, tool, validator, policy, or retry events
What context was available? Source IDs, versions, tool results, permissions, prompt/model markers
What did the user receive or experience? Terminal status, response, fallback, action result, correction

The AI agent observability guide describes the evidence model. A trace is useful when it shortens the path from user effect to an owned hypothesis; collecting more unstructured logs is not the same as reconstructing a run.

The first useful debugging artifact is a timeline that connects the user effect to the transition that changed the path.

3. Compare failing and healthy runs

Find a known-good case from the same workflow, task class, and release whenever possible. Compare like with like: input conditions, retrieved sources, model and prompt versions, tool state, retry path, timing, policy result, and final outcome. If the healthy comparison is from an easier cohort or different release, say so and limit the claim.

Difference Candidate mechanism Next check
Required source absent Retrieval, indexing, filter, or freshness issue Inspect corpus version and source selection
Tool response differs Dependency state or contract change Replay with recorded request and validate schema
Same evidence, different decision Prompt, model, policy, or context interpretation Compare versions and criterion-level outcomes
Same path, slower result Retry, provider, queue, or critical-path latency Inspect child-span timing and deadlines
Action changed external state Missing authority or idempotency boundary Review precondition, approval, and postcondition

Avoid the attractive explanation that is easiest to edit. The RAG evaluation guide shows why missing or irrelevant evidence should be separated from a synthesis failure before changing generation behavior.

4. Classify the mechanism and choose the correction

Write the hypothesis as a mechanism: “the freshness check was absent,” “the tool contract accepted a partial result,” or “the retry classified a deterministic validation error as transient.” Then identify the smallest correction that changes that mechanism and the evidence that will show recurrence.

incident: renewal_guidance_wrong_commitment mechanism: stale_account_state_passed_as_current correction: require_freshness_check_before_drafting regression_case: renewal_state_older_than_request watch: blocked_action_rate_and_user_correction_rate owner: account-workflow 

This is an illustrative incident packet, not executable configuration. Keep observations separate from interpretation, name the owner, and record what would falsify the hypothesis. A correction may belong in code, data, retrieval, prompt, policy, dependency handling, or operations.

Correction rule: Prefer the change that makes the failure impossible or visible at its boundary. A prompt edit is insufficient when the system lacks a state or authority control.

5. Verify the fix before closing

Replay the original case with the corrected version, then test neighboring cases that could regress: empty input, ambiguous intent, stale state, partial tool result, duplicate action, long context, and a dependency timeout. Compare user outcome, failure class, latency, cost, safety, and escalation—not just whether the final response looks better.

After release, watch the signal that exposed the incident for a defined observation window. Preserve the candidate and baseline versions, cohort, sample rule, and rollback condition. If the result improves one dimension while harming another, record the trade-off and decide whether the exposure remains appropriate.

The AI agent testing guide explains how to turn the confirmed mechanism into the lowest useful durable test. The best incident review ends with a new test, contract, alert, runbook step, or explicit risk decision.

6. Turn the mechanism into a regression case

An incident becomes useful after the immediate correction, when the next change cannot quietly recreate it. Convert the confirmed mechanism into a case that preserves the condition that made the failure possible. Keep the case focused: one input condition, one expected user outcome, and one observable assertion.

Incident condition Expected behavior Evidence to assert
Retrieval returns no applicable policy Abstain, ask for more information, or route to review No unsupported commitment appears in the result
Account state is older than the workflow allows Refresh state before making a decision The stale timestamp is rejected or refreshed
A write request times out with an unknown result Reconcile before retrying The action is not duplicated
A tool returns a partial record Validate required fields and take a safe fallback Missing fields cannot pass as a complete result

The expected behavior should describe what the user or downstream system is allowed to observe. It is rarely useful to require the model to return one exact sentence. For a refusal, assert that the agent does not claim an action succeeded. For a tool call, assert that the arguments satisfy the contract and that authorization was checked. For a cost regression, assert a bounded call count or budget decision rather than a brittle token total.

Preserve a fair baseline

Save the input class, relevant source or state version, prompt and model versions, tool contract, and expected outcome. Use stable synthetic identifiers for private data. A case that depends on a moving production record is difficult to reproduce and may expose data that should not be copied into a test set.

Keep the original failing case unchanged. If you later improve the expected behavior or replace the case, record why; otherwise a test can become green simply because the team weakened the expectation. A passing score should mean that the workflow respected the required boundary, not that a judge liked the wording.

Regression rule: Keep one case that reproduces the incident and several neighboring cases that test the same boundary under different conditions.

Neighboring cases matter because a narrow fix can trade one failure for another. A retrieval filter may stop the wrong document but also hide the correct one. A stricter timeout may prevent a long retry loop but increase safe escalations. Include a healthy case, the original failure, and at least one plausible edge case before treating the correction as complete.

This is the first half of a longer guide. The rest covers keeping diagnostic evidence safe, rolling out a fix without hiding a new failure, and the handoff note another responder can use: How to Debug AI Agents in Production. I build Uncoil, an open source tracing tool for AI agents.

原文:https://www.a8q9.cn/en/article/how-to-debug-an-ai-agent-in-production-a-6-step-incident-workflow-88dbd6.html

相关文章