Make Answers Feel Fast

In Find Where the Wait Goes, tool rounds and the other calls before the stream took 78 percent of the slowest answers’ time, mostly for beds questions about one hut, then the next.

You’ll cut those rounds by looking up beds before the model call, then try shorter answers and the fast role, keeping only what the numbers and evals support. Start with the baseline.

Measure the First Words and the Last

A hiker waits twice: for the first words and for the whole answer. The first wait is the time to first token, from the request’s start to the first text the stream sends. This script measures it on the server, a moment before the hiker sees it.

Streaming shortens only the first wait. The model generates no faster, so an answer your code needs whole, such as JSON, gains nothing.

Save scripts/first-token.js. Copy readJsonLines() and percentile() from scripts/wait-split.js, then add:

Language: JavaScript Filename: scripts/first-token.js
const spans = readJsonLines(process.argv[3]);
const answers = [];
let notStreamed = 0;
for (const entry of readJsonLines(process.argv[2])) {
  if (entry.route !== "/ask" || entry.status !== 200) continue;
  const mine = spans.filter((s) => s.traceId === entry.requestId);
  const stream = mine.find((s) => s.name === "streamAnswer");
  if (!stream) {
    notStreamed++; // a cached or latest-reports answer
    continue;
  }
  const model = mine.find((s) => s.name === "model");
  const started = Date.parse(entry.time) - entry.durationMs;
  const firstText = Date.parse(stream.start) + stream.firstChunkMs;
  answers.push({
    total: entry.durationMs,
    firstToken: firstText - started,
    beds: model.asked.topics.includes("beds"),
  });
}

function report(label, list) {
  const at = (key) => [50, 95].map((p) => {
    const ms = percentile(list.map((a) => a[key]), p);
    return `p${p} ${(ms / 1000).toFixed(1)} s`;
  }).join(", ");
  console.log(`${label} (${list.length}): ` +
    `first token ${at("firstToken")}; total ${at("total")}`);
}
report("All", answers);
report("Beds", answers.filter((a) => a.beds));
console.log(`Not streamed, left out: ${notStreamed}`);

The log’s time marks the response’s end, so subtracting durationMs gives its start.

Measure the app twice as Cut the Cost per Question left it, with at most 12 tool rounds, prompt v5, and low-stakes routing. Run the server in one terminal and replay from another. Then stop it with Ctrl+C and repeat with the tag before-b:

Language: Text
$ npm start | tee before-a.log
$ node scripts/replay-day.js --tag=before-a
$ node scripts/first-token.js before-a.log traces/before-a.jsonl
All (100): first token p50 2.5 s, p95 8.2 s; total p50 3.4 s, p95 9.0 s
Beds (30): first token p50 4.3 s, p95 9.7 s; total p50 5.0 s, p95 11.0 s
Not streamed, left out: 0
$ node scripts/first-token.js before-b.log traces/before-b.jsonl
All (100): first token p50 2.5 s, p95 7.5 s; total p50 3.3 s, p95 8.4 s
Beds (30): first token p50 4.0 s, p95 9.7 s; total p50 4.7 s, p95 11.0 s
Not streamed, left out: 0

Between runs, the day’s p50s moved by 0.1 seconds at most, those for beds by 0.3, and the p95s by 0.7, so a change must beat the matching spread. Record the evals too: npm run evals -- --route --runs=5 passed 135 of 135, and two runs of npm run evals -- --beds --runs=5 passed 54 of 55 each.

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.