Skip to content
mentoor

Topic 4 of 5 · 5 min read

Replaying nondeterministic multi-agent runs

Goal: Set up a replay of a failed run from recorded tool results and decide whether a fix holds across several reruns.

Replay a failed run by feeding recorded tool results back in, change one thing, and judge the fix over several reruns, not one. Models are not deterministic and tools return live data, so running the same input again often gives a different run.

Check yourself · 1 of 3

What does a full stub replay of a failed run test?

Choose one answer

Why it matters

A bug seen once may never appear again if you rerun it live. The order of tool calls changes, a search returns new pages, and a worker takes a different path. A fix that passes one lucky rerun proves little. You need a frozen copy of what went wrong and a way to measure whether a change helps.

What to record

For every span that touches the outside world, store enough to reproduce its output: tool name, arguments and the exact result; the model name and settings; the prompt sent. Store the model reply too, so you can replay without calling the model at all. Respect the privacy rules in the last topic when you do.

Three levels of replay

  1. Full stub replay. Tools and models return recorded values. It is cheap, fast and fully repeatable. It checks your orchestration code, parsing and handoffs, not model quality.
  2. Tool-stub replay. Tools return recorded results, but the models are called live. This keeps the world fixed and lets you test a prompt change.
  3. Live rerun. Everything runs for real. It checks end-to-end behaviour, but is noisy.

Before and after

Before: "I changed the analyst prompt and tried again. It worked." After:

recorded = load_trace("run-8841.json")

def stub_tool(name, args):
    return recorded.tool_result(name, args)

results = []
for seed in range(10):
    out = run_pipeline(task=recorded.task, tool=stub_tool, seed=seed)
    results.append(out.total == recorded.expected_total)

print(sum(results), "of", len(results), "correct")

Run the same loop on the old prompt and the new prompt, with the same recorded tools. Say the old prompt gets 3 of 10 and the new one 9 of 10. That is a signal; one pass would not have been.

What goes wrong

  • Tool stubs that do not match. If the new prompt makes a different call, the recorder has no stored result. Return a clear error, not a made-up value.
  • Too few reruns. Three tries cannot separate a 50 percent fix from a 90 percent fix.
  • Changing two things. Prompt and model together hide which one helped.
  • Recorded data going stale. A replay of a six-month-old trace may test a world that no longer exists.

Examples

Comparing two prompts

The analyst prompt used to say "estimate if data is missing". The new one says "report missing data and stop". Replayed ten times each against the recorded empty sales.query result, the old prompt invents a total in 8 runs and the new one raises the missing-data warning in all 10. The comparison is the evidence, not any single run.

The unmatched stub

def stub_tool(name, args):
    try:
        return recorded.tool_result(name, args)
    except KeyError:
        raise ReplayMiss(f"no recorded result for {name} {args}")

After a prompt change the worker calls sales.query with a different region. The replay stops with a clear miss instead of returning a fake answer.

Key points

  • Record tool names, arguments, results, prompts and settings for every span that touches the outside world.
  • Full stub replay checks orchestration; tool-stub replay with live models checks prompt changes.
  • Rerun a fix several times against the same recorded tools and compare pass rates before and after.
  • A stub with no recorded result for a new call must fail loudly, not invent data.
  • Change one thing at a time so the result can be attributed.

Common mistakes

  • Declaring a fix after one successful rerun of a nondeterministic pipeline.
  • Returning default values from a stub when no recording matches.
  • Replaying only the failing agent and ignoring the handoffs around it.
  • Changing the prompt and the model in the same experiment.

Check yourself · 2 of 3

You changed an agent prompt and it worked once on rerun. What should you do before trusting the fix?

Choose one answer

Check yourself · 3 of 3

During replay, an agent calls a tool with arguments that were never recorded. What should the stub do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic