Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Find Where the Wait Goes

In this lesson, you split the assistant’s streamed answers into the wait for the first words and the generation after them, and measured which part a recorded day’s slowest answers spent their time on.

Key Points

  • The question: in Cut the Cost per Question, you asked for shorter answers. Do they make the assistant faster too? The answer depends on what happens inside a model call.
  • The recorded day is the hundred questions from Cut the Cost per Question, replayed after your cuts with every timing kept. The slowest answer took 12.5 seconds. It made three tool rounds, checking hut after hut, the slow step your traces found. Then one more model call streamed a 40-token answer.
  • The replay used the app’s earlier cap of three tool rounds, before Cut the Cost per Question settled on 12, and the conclusion still holds: tool rounds lead the slowest answers’ time.
  • The first words split a model call in two. The answer’s span records when they arrived, in firstChunkMs. Before them is the wait: your request crosses the network and waits its turn, the model reads the prompt, and it may reason before writing. After them is generation: the answer streams in one token at a time.
  • The wait moves less than generation. The answer role’s waits that day sat near a median of 0.9 seconds, and most fell between 0.6 and 1.3 seconds (p5 to p95 over 70 answers).
  • Generation grows with the tokens. Three of the answer role’s answers each waited about 0.9 seconds. Their generation took three tenths of a second for 20 tokens, nearly a second for 58 and 1.2 seconds for 79.
  • At these sizes, a longer prompt barely moved the wait. You ran the Col de Fenêtre question five times each way. A random first line kept the prompt cache out, so every run read the whole prompt.
  • The whole reports block made the prompt about four times longer (424 to 1,741 input tokens). The wait went from 931 to 967 milliseconds, less than it drifts between runs. Most of the wait isn’t reading your prompt.
  • A longer answer took longer to generate. Asking for about two hundred words grew the answer from 82 to 201 tokens and generation from 1.4 to 4.0 seconds. Much larger prompts, the cache and reasoning can change that balance, so measure your own requests.
  • The script below weighs the two parts across a day. It reads the request log and the traces. For each answer, it keeps the total time and the wait, then the generation and the output tokens from the last model span, the call that streamed the answer.
  • Then it sums the slowest answers. It takes the answers slower than the p95 and gives each part’s share of their time. Last, it divides the answer role’s generation time by its output tokens.
  • On the recorded day, the wait was 15% of the slowest answers’ time. Five answers were slower than the 7.5-second p95. Generation took 7% of their time, and tool rounds and the other model calls before the answer took the remaining 78% (the script’s “Tool rounds and the rest”).
  • Generation came to about 16 milliseconds per output token on the answer role. That’s rough, because the output count includes a few hidden reasoning tokens the hiker never sees. Counting visible tokens only, it’s about 17.
  • These are limited numbers. The replay ran in about eighty seconds on one provider’s models, so it shows one provider’s load at one moment, and only five answers make the tail. Run the script on your own logs to see which part to cut on your provider.
  • Cut the bigger part. Halving the slowest answer’s 40 tokens saves about 0.3 seconds (20 tokens at about 16 milliseconds each). Its three tool rounds took almost eleven. Each round is a whole model call with its own wait.
  • The rule: split each model call at its first words. When generation leads, ask for shorter answers. When calls in a row lead, make fewer of them. So shorter answers help a little, but in the slowest answers, tool rounds lead.

Final Code

Save this as scripts/wait-split.js. It reads a day’s request log and trace file, given as its two arguments. readJsonLines() and percentile() are the same as in scripts/latency.js:

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

00:00 In Cut the Cost per Question, you asked for shorter answers. Do they make it faster too? Replay that lesson’s hundred questions with every timing kept. The slowest took 12.5 seconds.

00:14 It made three tool rounds, checking hut after hut, the slow step your traces found. Then one more model call streamed a forty-token answer. Look inside that call.

00:27 The answer’s span records when its first words arrived, which splits the call in two. Before them, your request crosses the network and waits its turn. The model reads the prompt, and may reason before writing.

00:41 That’s the wait. After them, the answer streams in one token at a time. That’s generation. Take three answers: a bed count, the Lauzet footbridge and the Col de Fenêtre.

00:56 Their waits sit near the day’s median, 0.9 seconds. Most fell between 0.6 and 1.3. Generation grows with the tokens. Twenty take three tenths of a second, fifty-eight nearly a second, seventy-nine 1.2.

01:14 Does a longer prompt lengthen the wait? Run the Col de Fenêtre question five times each way. A random first line skips the cache, so every run reads the whole prompt.

01:25 Trimmed to the reports it needs, the prompt is about four hundred tokens. The whole reports block makes it four times longer, and the wait moves from 931 to 967 milliseconds.

01:39 Now ask for about two hundred words. The answer grows from 82 to 201 tokens, and generation from 1.4 to 4.0 seconds.

01:50 At these sizes, the prompt moved the wait less than it drifts between runs. Most of the wait isn’t reading your prompt. But every output token adds to generation.

02:02 A script weighs the two parts across the day, from the request log and the traces.

02:08 For each answer, it keeps the total time and the wait. Then the generation, and the output tokens from the last model span, which streamed the answer. It takes the answers slower than the p95 and gives each part’s share.

02:25 Five answers sit above the day’s 7.5-second p95. The wait is fifteen percent of their time, generation seven, and tool rounds and other calls seventy-eight.

02:36 Per output token, the answer role’s generation takes about sixteen milliseconds. That’s rough. A few tokens were hidden reasoning, and it’s one provider’s load over eighty seconds.

02:49 Check your own logs.

02:52 Halving that forty-token answer saves about 0.3 seconds. Its three tool rounds took almost eleven. Each round is a whole model call, with its own wait. Split the time at the first words, then cut the bigger part.

03:08 When generation leads, ask for shorter answers. When calls in a row lead, make fewer of them. So shorter answers help a little, but in the slowest answers, tool rounds lead.

03:21 Next, in Make Answers Feel Fast, you take them out and measure again.

Become a Member to join the conversation.