Skip to content

Agent Replay

Agent Replay lets you load any past execution trace, step through it, inspect each step's input/output/attributes, fork at any point, modify the prompt or input, and rerun from that point. This is the SDK's unique debugging feature — no other framework offers fork-and-rerun.

Start from the mental model

Where a replay has to hold explains replay one boundary at a time — what the trace carries, what recorded mode fixes, what tools do, the miss, what a rerun can't touch, the saved test — with a real run behind every claim.

Fidelity guarantees

For per-mode behavior (live vs recorded vs deterministic), what's captured, what isn't, and per-provider seed support, see Fidelity Guarantees.

Production failure → regression test

The full capture → analyze → fix → save → verify loop, with a deliberately broken tool to demonstrate against, ships as examples/regression-from-trace/. Use it as the canonical pattern when a customer reports the agent did something wrong.

Why Replay?

When an agent fails in production — hallucinates, calls the wrong tool, gives a bad answer — you need to understand why and test a fix without burning new production traffic. Replay lets you:

  1. Load the exact trace of the failure
  2. Step through to find the problematic step
  3. Fork at that step (records where the divergence will be measured)
  4. Change the prompt, input, tools, or state
  5. Rerun the agent end-to-end with your modifications applied
  6. Compare the original vs fixed result, scored by LLMJudge or exact_match

v1 limitation — full rerun, not mid-trace resume

The v1 arerun() re-executes the agent from the top with the modifications applied. The fork step records the divergence anchor used by compare(), not a resume point. True mid-trace resume (replaying messages up to a step, then continuing) is a v2 concern that requires a stable on-span message-history shape across providers. For most debug workflows the full rerun is what you want anyway — the modified prompt / tool override applies for the whole run, not just the suffix.

The Local UI surfaces this in the Fork and rerun with modifications dialog (renamed in v1.14.1 from "Rerun from this step", which overpromised partial replay). The Tool tab now requires a tool name alongside the JSON response so the override is wired through with_tool_override for real — pre-v1.14.1 it was silently dropped for agent reruns. Since 1.89.0, when the trace recorded the agent's tools, the Tool tab lists each one with Block / Stub / Live instead; every tool starts blocked, and the name field is only the fallback for a trace with no tool list.

Fork-and-rerun dialog: the Tools tab lists lookup_order as blocked, Live disabled because the UI process has no implementation, and the "Rerun with modifications" button

Where Do Trace IDs Come From?

Every agent.run() automatically generates a trace. The trace ID is returned in the result:

result = agent.run("Help me with my order")
print(result.trace_id)  # "b6acf1ef2c2779bbc2fcf80802ae0534"

You can also browse past traces programmatically or via CLI:

from fastaiagent.trace import TraceStore
for t in TraceStore().list_traces(last_hours=24):
    print(f"{t.trace_id}  {t.name}  {t.status}")
fastaiagent traces list --last-hours 24

Loading a Replay

From Local Trace Storage

from fastaiagent.trace.replay import Replay

# Load by trace ID (from result.trace_id or fastaiagent traces list)
replay = Replay.load("b6acf1ef2c2779bbc2fcf80802ae0534")

From a TraceData Object

from fastaiagent.trace.storage import TraceStore

store = TraceStore()
trace_data = store.get_trace("b6acf1ef2c2779bbc2fcf80802ae0534")
replay = Replay(trace_data)

Viewing the Summary

print(replay.summary())

Output:

Trace: b6acf1ef2c2779bbc2fcf80802ae0534
Name: agent.support-bot
Status: OK
Spans: 4
Duration: 2025-01-15T10:30:00Z → 2025-01-15T10:30:02.5Z

Steps:
  [0] agent.run
  [1] llm.chat_completion
  [2] tool.search_docs
  [3] llm.chat_completion

Stepping Through

All Steps

steps = replay.step_through()
for step in steps:
    print(f"Step {step.step}: {step.span_name}")
    if step.attributes:
        print(f"  Attributes: {step.attributes}")

Specific Step

steps = replay.steps()
print(f"Total steps: {len(steps)}")

# Inspect a specific step
step = replay.inspect(2)
print(f"Name: {step.span_name}")
print(f"Span ID: {step.span_id}")
print(f"Timestamp: {step.timestamp}")
print(f"Attributes: {step.attributes}")

ReplayStep

Field Type Description
step int Step index (0-based)
span_name str What happened (e.g., "llm.chat_completion", "tool.search")
span_id str Unique span identifier
input dict Input to this step
output dict Output from this step
attributes dict OTel attributes (model, tokens, tool name, etc.)
timestamp str When this step executed

Forking and Rerunning

The core debugging workflow — fork at the problem step, fix, rerun:

# Step 3 is where the LLM hallucinated
forked = replay.fork_at(step=3)

# Modify what you want to fix
forked.modify_prompt("Always cite the exact policy section number. Never guess.")
forked.modify_input({"query": "refund policy section 4.2"})

# Rerun from that point
result = forked.rerun()
print(f"New output: {result.new_output}")
print(f"Steps executed: {result.steps_executed}")

How rerun() reconstructs the agent

ForkedReplay.rerun() is a real re-execution, not a stub. Under the hood it:

  1. Finds the root agent.{name} span in the loaded trace.
  2. Reads the Agent Reconstruction Attributes off that span — agent.config, agent.tools, agent.guardrails, agent.llm.config, agent.system_prompt, agent.input — and decodes them into the canonical Agent.from_dict payload.
  3. Applies your modify_prompt / modify_config / modify_input modifications on top of that payload.
  4. Builds the tool plan (1.89.0): your overrides and stubs, the tools you allowed (with_tool_policy), and a blocked stand-in for every other tool. An allowed function tool is the callable registered in this process's ToolRegistry; tools created with FunctionTool(name=..., fn=...) or @tool auto-register at construction. See Fidelity Guarantees.
  5. Builds a new Agent via Agent.from_dict() with that plan and calls agent.arun(new_input) inside a replay provenance scope. A new trace is emitted for the rerun, marked fastaiagent.source = "replay" and kept local, and its trace_id is returned on ReplayResult.trace_id alongside tool_dispositions, blocked_tool_calls and exported.

v1 scope. The rerun re-executes the agent from the top with modifications applied — it does not literally resume the LLM loop at fork_point with prior messages pre-seeded. fork_point is still recorded and used by compare() to mark where divergence logically happened, so downstream tooling sees your intent. Mid-trace resume (replaying captured messages up to fork_point then continuing from there) requires a stable per-provider message-history representation and is planned as a follow-up.

What you need for rerun() to work:

  • The SDK is at a version that captures agent.* reconstruction attributes (0.1.7+). Traces from older versions can still be loaded and stepped through, but rerun() will fail to reconstruct the agent — re-run the agent once on the new SDK to capture a rerun-capable trace.
  • For a tool to really run, you supply it (with_tool_override / with_tool_stub) or allow it (with_tool_policy("live", allow=[...])). An allowed function tool must be registered in the ToolRegistry of the replay process; importing the module that defines it is usually enough. Without either, the tool is blocked and the rerun still completes.
  • FASTAIAGENT_TRACE_PAYLOADS=0 was not set when the original trace was captured, if you want the resolved system prompt to round-trip. When payloads were off, modifications still work but the reconstructed agent uses whatever prompt your code supplies via modify_prompt().

Available Modifications

Method What it changes
modify_prompt(new_prompt) System prompt for the LLM call at the fork point
modify_input(new_input) Input data passed to the step
modify_config(**kwargs) Agent/LLM configuration (temperature, max_tokens, etc.)
with_determinism(mode, on_miss=) How the model is called: live, recorded, deterministic
with_tool_override(name, fn_or_tool) Replace one tool; never registered globally
with_tool_stub(name, returns=) Replace one tool with a stub returning returns (1.89.0)
with_tools([...]) Replace every tool
with_tool_policy(policy, allow=) block (default), error or live for tools you did not supply; allow names tools that really run (1.89.0)
with_export(True) Export the rerun's trace; off by default (1.89.0)
with_trace_db(path) Write the rerun's trace to another local.db (1.89.0)

Forking with modified state is not done here. trace/replay.py is a read-only inspect/diff surface; trace-based state counterfactuals are the Enterprise Replay engine's job. To branch a run from a saved step with a modified state, use the SDK's checkpoint-fork primitive — Chain.afork(...) / Agent.afork(...). (ForkedReplay.modify_state() raises NotImplementedError pointing there.)

Methods return self for chaining:

result = (
    replay.fork_at(step=3)
    .modify_prompt("Be more precise.")
    .modify_input({"context": "additional context"})
    .modify_config(temperature=0.2)
    .rerun()
)

Comparing Results

After rerunning, compare the original and fixed execution:

forked = replay.fork_at(step=3)
forked.modify_prompt("New instructions")
result = forked.rerun()

comparison = forked.compare(result)
print(f"Original steps: {len(comparison.original_steps)}")
print(f"Diverged at step: {comparison.diverged_at}")

ComparisonResult

Field Type Description
original_steps list[ReplayStep] Steps from the original trace
new_steps list[ReplayStep] Steps from the rerun (if available)
diverged_at int \| None Index into original_steps of the first aligned turn that differs
new_diverged_at int \| None The same divergence, as an index into new_steps (1.89.0)
aligned list[tuple[int \| None, int \| None]] (original, new) index pairs of the turns compared: the root and its direct children, minus memory.* (1.89.0)
skipped_steps / new_skipped_steps list[int] Steps left out of the alignment (1.89.0)
tool_diffs list[ToolDiff] Aligned tool calls whose status or result changed, with the rerun's disposition (1.89.0)
tools_diverged_at int \| None Index into original_steps of the first of those (1.89.0)
compare_status "ok" \| "rerun_failed" rerun_failed when the rerun's trace couldn't be loaded

ReplayResult

Field Type Description
original_output Any Output from the original execution
new_output Any Output from the rerun
steps_executed int Number of steps executed in the rerun
trace_id str \| None Trace ID of the rerun
source_trace_id str \| None Trace ID of the trace it replays (1.89.0)
determinism str The determinism mode it ran under (1.89.0)
tool_dispositions dict[str, str] Per tool: blocked, live, stub, override or no_function (1.89.0)
blocked_tool_calls dict[str, int] Calls the rerun blocked, per tool. The answer does not show their effect (1.89.0)
exported bool Whether the rerun's trace will be exported. False by default (1.89.0)
trace_db_path str \| None The store it was written to, when with_trace_db was used (1.89.0)

ReplayResult also exposes save_as_test() for turning a verified fix into a regression case.

From a Rerun to a Regression Test

Once a forked.rerun() confirms the fix, append the case to a JSONL dataset that the eval suite will replay on every future run. This closes the loop: every production failure becomes a permanent test case, captured at the exact step where it broke.

Code path: ReplayResult.save_as_test()

from fastaiagent.eval import evaluate
from fastaiagent.trace.replay import Replay

# 1. Reproduce the failure
failure = agent.run("What is our refund policy?")

# 2. Fork, fix, rerun
replay = Replay.load(failure.trace_id)
rerun = (
    replay.fork_at(step=0)
    .modify_prompt("Answer in exactly one sentence. Refund policy: 30 days.")
    .rerun()
)

# 3. Save the corrected behavior as a regression case
rerun.save_as_test(
    "regression_tests.jsonl",
    input="What is our refund policy?",
    expected_output=str(rerun.new_output),
    source_trace_id=failure.trace_id,  # provenance back to the original bug
)

# 4. Re-run the suite — string scorer or LLM-as-judge, your choice
results = evaluate(
    agent_fn=lambda text: agent.run(text).output,
    dataset="regression_tests.jsonl",
    scorers=["exact_match"],  # or [LLMJudge(criteria="correctness")]
)

Saved JSONL schema

Each line is a self-contained record consumable by evaluate():

{
  "input": "What is our refund policy?",
  "expected_output": "Refund policy: 30 days, full refund.",
  "trace_id": "0aad0d1ef2ca859a0f1cc1a6aebb2bc7",
  "created_at": "2026-05-21T17:07:26.708546+00:00"
}
Field Source Used by evaluate()
input Argument you pass to save_as_test() Yes — fed to agent_fn
expected_output Argument you pass Yes — fed to the scorer
trace_id source_trace_id arg, else self.trace_id No — pure provenance
created_at UTC ISO-8601 when saved No — pure provenance

The provenance fields are written for humans — both the Local UI and the eval results page link them back to the originating failure trace.

Works with any scorer (including LLM-as-Judge)

Because the JSONL schema is just input/expected_output, the same dataset works with any scorer in the eval framework:

  • "exact_match", "contains" — fast deterministic string checks
  • LLMJudge(criteria="correctness") — semantic equivalence via LLM
  • Custom @scorer functions, trajectory scorers, safety scorers, RAG metrics

Pick the scorer that matches the kind of regression you're guarding against. A worked example covering both string match and LLM-judge: examples/62_replay_to_regression.py.

UI path: "Save as regression test" button

The same workflow is available from the Local UI's Replay page — after a rerun, the Save as regression test button calls POST /api/replay/forks/{fork_id}/save-as-test, which appends an identical JSONL record (same four fields) to .fastaiagent/datasets/regression_tests.jsonl by default. UI-saved and code-saved records are interchangeable.

Side-by-side comparison in the Local UI

Everything above is also available as a visual diff in the Local UI. After running fastaiagent ui, open:

http://127.0.0.1:7842/traces/<trace_id>/replay

Click any span in the timeline → Fork here → pick one of the four tabs (Prompt / Input / Tools / LLM params) → edit → Rerun from this step. The right pane fills with the forked run's spans as they arrive.

The dialog also sets the determinism mode, and on the Tools tab, what the rerun does with each of the agent's tools: Block (the default), Stub with a value you type, or Live. Live is disabled for a tool the UI process has no function for, and for a REST tool whose credentials were redacted from the trace. After the rerun, chips show what happened to each tool, a warning lists blocked calls, and a note says the rerun is local-only: not counted in Analytics and not exported.

Below, a Rerun complete card appears with three pieces:

  1. Side-by-side output cards — the original final output and the rerun's final output, rendered through JsonViewer.
  2. Step-by-step comparison grid — one row per step, with the original span name on the left and the rerun's on the right. Rows at or after comparison.diverged_at are highlighted with a left border bar and a "diverged at step N" badge at the top right.
  3. Expandable per-step diff — click the chevron on any row whose inputs or outputs differ and a split-view diff (powered by react-diff-viewer-continued) expands inline, showing exactly what changed on that step.

A Tool calls that changed panel lists tool_diffs: each aligned tool call whose status or result differs, with the rerun's disposition.

The diff respects your current theme (light/dark). Save as regression test appends the case to .fastaiagent/datasets/regression_tests.jsonl (1.89.0; before, it sat next to local.db), the directory the Datasets page reads.

Agent Replay side-by-side comparison

The replay page itself and the fork dialog are captured separately:

Clickable walkthrough

  1. From any trace on /traces, click Open in Replay (or navigate directly to /traces/<id>/replay).
  2. Click the span you want to branch from. The inspector on the right shows its inputs, outputs, attributes, and events.
  3. Click Fork here. Pick the tab for the field you want to change (Prompt / Input / Tools / LLM params).
  4. Edit the field. Click Rerun from this step. The UI fires three sequential requests behind the scenes: POST /api/replay/<id>/fork → PATCH /api/replay/forks/<fork_id> → POST /api/replay/forks/<fork_id>/rerun.
  5. When the rerun lands, the page auto-fetches the comparison and renders the card described above.
  6. Click any row's chevron to see the per-step diff. Click Save as regression test to append the case to .fastaiagent/datasets/regression_tests.jsonl.

End-to-end bug-fix walkthrough

A full scripted example — buggy prompt → fork → fix → rerun → compare → assert → save-as-regression → UI deep links — lives at examples/38_replay_comparison.py. Pair it with the Local UI page above for the visual side-by-side.

CLI Commands

# Show replay steps for a trace
fastaiagent replay show <trace_id>

# Inspect a specific step
fastaiagent replay inspect <trace_id> 3

Example:

$ fastaiagent replay show b6acf1ef2c2779bbc2fcf80802ae0534

Trace: b6acf1ef2c2779bbc2fcf80802ae0534
Name: agent.support-bot
Status: UNSET
Spans: 4
Duration: 2026-10-30T23:05:17.562451+00:00 → 2026-10-30T23:05:19.459172+00:00

Steps:
  [0] agent.support-bot
  [1] llm.openai.gpt-4o-mini
  [2] tool.search_docs
  [3] llm.openai.gpt-4o-mini

$ fastaiagent replay inspect b6acf1ef2c2779bbc2fcf80802ae0534 2

Step 2: tool.search_docs
  Timestamp: 2026-10-30T23:05:18.301122+00:00
  Attributes: {'fastaiagent.tool.name': 'search_docs', ...}

Pass the full 32-character trace id, as fastaiagent traces list prints it (since 1.95.0; it used to print a 12-character prefix that no command could look up). Status is the root span's OpenTelemetry status: UNSET for a run that finished without error, ERROR for one that failed.

Debugging Workflow

A typical debugging session:

from fastaiagent.trace import TraceStore
from fastaiagent.trace.replay import Replay

# 1. Find the failing trace
store = TraceStore()
traces = store.search("support-bot")
# Or use: fastaiagent traces list

# 2. Load and inspect
replay = Replay.load(traces[0].trace_id)
print(replay.summary())

# 3. Step through to find the problem
for step in replay.step_through():
    print(f"[{step.step}] {step.span_name}: {step.attributes}")
# Step 3: LLM hallucinated the refund policy ← found it

# 4. Fork and fix
forked = replay.fork_at(step=3)
forked.modify_prompt("Always cite exact policy section numbers from the docs.")

# 5. Rerun and verify
result = forked.rerun()
print(f"Fixed output: {result.new_output}")

# 6. Compare
comparison = forked.compare(result)
print(f"Diverged at step {comparison.diverged_at}")

Error Handling

from fastaiagent._internal.errors import ReplayError

try:
    step = replay.inspect(99)
except ReplayError as e:
    print(f"Error: {e}")  # "Step 99 out of range (0-3)"

try:
    forked = replay.fork_at(-1)
except ReplayError as e:
    print(f"Error: {e}")  # "Step -1 out of range (0-3)"

Platform Replay

When connected to the FastAIAgent Platform, you can pull any trace from the platform (including traces from other team members) and replay locally:

import fastaiagent as fa

fa.connect(api_key="fa_k_...", target="https://<label>-dev.api.fastaiagent.net", project="my-project")

# Pull a platform trace and replay locally
replay = Replay.from_platform(trace_id="tr-abc123")
replay.step_through()

# Fork from a platform trace
forked = replay.fork_at(step=3)
forked.modify_prompt("Updated system prompt")
result = forked.rerun()

Next Steps