Skip to content

Failure Analysis ​

Agent Replay's analysis engine runs deterministic detectors over recorded traces to identify why agents fail. No LLM calls required — results come back in milliseconds.

How it works ​

bash
agent-replay analyze latest

The engine:

  1. Loads the recorded run and its steps from the local database
  2. Runs all registered detectors against the trace
  3. Aggregates findings by severity and confidence
  4. Suggests a replay point (the step to focus on)

Deterministic detectors ​

Ten built-in detectors cover the most common failure patterns:

DetectorSeverityWhat it catches
hallucinated_tool_argumentHIGHAgent invents non-existent parameters
ignored_tool_resultHIGHAgent ignores correct tool output
unsafe_write_actionHIGHDestructive action without confirmation
permission_mismatchHIGHUnauthorized action attempts
final_answer_conflictHIGHConclusion contradicts evidence
bad_retrievalMEDIUMRetrieved context is irrelevant
stale_memoryMEDIUMActing on outdated memory
loop_detectedMEDIUMRepeating the same action
context_pollutionLOWContext grows without value
excessive_retryLOWRetrying without changing approach

See 10 Agent Failure Patterns for detailed descriptions and examples.

LLM-assisted detectors ​

For deeper analysis, plug in a model of your choice:

python
from agent_replay.analysis.llm import LLMFailureClassifier, LLMRootCauseDetector

# Any callable that takes a prompt string and returns a string
def my_llm(prompt: str) -> str:
    return openai.chat(model="gpt-4", messages=[{"role": "user", "content": prompt}])

classifier = LLMFailureClassifier(llm=my_llm)
root_cause = LLMRootCauseDetector(llm=my_llm)

LLM detectors are optional and provider-agnostic.

Custom detectors ​

Register your own detector in a few lines:

python
from agent_replay.analysis import register_detector, Finding, taxonomy

@register_detector("token_budget")
def token_budget(run, steps):
    total = sum(s.usage.get("input_tokens", 0) + s.usage.get("output_tokens", 0)
                for s in steps if s.usage)
    if total > 100_000:
        return [Finding(
            taxonomy.EXCESSIVE_RETRY,
            "token_budget",
            f"Run used {total:,} tokens — check for unnecessary retries.",
        )]
    return []

Load custom detectors via CLI:

bash
agent-replay analyze latest --plugin my_detectors

Cross-run statistics ​

bash
agent-replay stats

Aggregates failure patterns across all recorded runs: which detectors fire most often, which agents fail most, and trending severity over time.

CI integration ​

Use JSON output for automated quality gates:

bash
agent-replay analyze latest --format json | jq '.severity'

A non-zero exit code when high-severity findings are detected makes it easy to integrate into CI pipelines.

Released under the Apache 2.0 License.