Replay a Call From Its Trace

In Find the Step That Went Wrong, you read the Forcella answer’s trace and found its first wrong step: retrieval handed the model a report six days older than the wardens’ closure. So far, retrieval is only a suspect.

In this lesson, you’ll replay the recorded model call to confirm the cause, turn the hiker’s question into an eval case, count how often it passes before and after the fix, and decide whether you can close the hiker’s report. Start by sending the model exactly what it got the first time.

Replay the Recorded Call

To test a suspect, send the model the same input again. Asking the question again on the page won’t do that, because the request goes through retrieval over today’s reports and may not take the hiker’s path. The model span holds the request body exactly as sent, with its model, instructions, and input, so send that instead.

Add one function to model/index.js, next to the others:

Language: JavaScript Filename: model/index.js
export function replay(body) {
  return provider.sendStream(body);
}

Without a trace argument, sendStream() writes no span, so replays stay out of your traces. A replay is faithful only while the recorded model ID still answers as it did. A pinned snapshot, a fixed version of the model, does until the provider retires it.

The script that reads the trace is plumbing, so take it as given. npm run replay -- <trace file> <trace ID> [runs] [report ID] sends the trace’s one streamed model request, the one whose answer the hiker read. It sends that request as recorded, five times by default, because one answer proves little. A report ID changes one step: that report’s line goes before the first report line, as if retrieval had returned it.

The script counts answers that say “closed” but not “not closed”, a rough count for this one question, and saves every full answer in replay-answers.txt.

Language: JavaScript Filename: scripts/replay.js
import { readFileSync, writeFileSync } from "node:fs";
import { replay } from "../model/index.js";

const [file, traceId, runs = "5", addId] = process.argv.slice(2);
const read = (path) => readFileSync(path, "utf8");
const spans = read(file).trim().split("\n")
  .map((line) => JSON.parse(line))
  .filter((s) => s.traceId === traceId);
const streamed = spans.filter((s) =>
  s.name === "model" && s.request.stream);
if (streamed.length !== 1) {
  throw new Error(`${streamed.length} streamed model spans`);
}
let body = JSON.stringify(streamed[0].request);

if (addId) {
  const reports = JSON.parse(read("data/reports.json"));
  const r = reports.find((report) => report.id === addId);
  const line = `Report ${r.date}, ${r.source}: ${r.text}\n`;
  const at = body.search(/Report \d{4}-\d\d-\d\d, /);
  if (at === -1) throw new Error("No report line in input");
  const escaped = JSON.stringify(line).slice(1, -1);
  body = body.slice(0, at) + escaped + body.slice(at);
}

const request = JSON.parse(body);
const answers = [];
let closed = 0;
for (let run = 1; run <= Number(runs); run++) {
  let text = "";
  for await (const chunk of replay(request)) text += chunk;
  answers.push(`--- Run ${run}\n${text}\n`);
  const negated = /not closed/i.test(text);
  if (/closed/i.test(text) && !negated) closed++;
  const start = text.replace(/\s+/g, " ").slice(0, 50);
  console.log(`${run}: ${start}...`);
}
writeFileSync("replay-answers.txt", answers.join("\n"));
console.log(`Says closed in ${closed} of ${runs} runs`);
console.log("Full answers saved in replay-answers.txt");

Add "replay": "node --env-file=.env scripts/replay.js" to the scripts in package.json. A replay sends whatever personal data the trace still holds to the provider again, so replay only the traces you must until Keeping Personal Data Out, later in this course, shows you how to redact them.

The saved answers can repeat that data, so add replay-answers.txt to .gitignore and delete the file when you’re done. Then replay your Forcella trace unchanged, with its file and its ID:

Language: Shell
$ npm run replay -- traces/2026-12-02.jsonl 4ac285fd-5f9a-4dd5-9ca9-a4caa2cdd8d0 5

> [email protected] replay
> node --env-file=.env scripts/replay.js traces/2026-12-02.jsonl ...

1: Latest report: **2026-07-03** says the **Forcella ...
2: The latest report is from 2026-07-03: the Forcella...
3: The most recent report I have is from **2026-07-03...
4: Latest report I have is from **2026-07-03**: the *...
5: The latest report I have is from **2026-07-03**: t...
Says closed in 0 of 5 runs
Full answers saved in replay-answers.txt

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.