In this lesson, you measured how long hikers wait for the assistant over a day of 200 questions, saw what an average hides, and reported the wait as percentiles of total time and of the time to the first words.
Key Points
- Every request’s log line holds its duration. The day in the video is the same 200 July questions you priced in Price Each Request From Its Usage, run on the stand-in model from the course’s exercises with every trace kept. The stand-in’s waits are scripted: the kinds of delay are real ones, but the numbers aren’t yours, and a real provider’s day is messier.
- The natural first number is an average, the same arithmetic as the cost script: add up the durations and divide by the count. The script counts completed answers, the
/asklines with status 200. Failures are counted as errors, as in Log One JSON Line per Request. On the video’s day, the average came to 2,404 ms. - The average describes no hiker’s wait. In a histogram with a bar for every quarter of a second, 141 requests took between 1.5 and 2 seconds, and the slowest took 12.6 seconds. The average fell in a bar with no requests in it, 168 of the 200 requests were faster than it, and the slow ones were far slower.
- Model calls always have a few slow requests, from a long answer, a retry after a rate limit, or an extra tool round. Those few pull the average up.
- A percentile counts along the sorted times. Sort the day’s times from fastest to slowest. The p50 is the time at the halfway point, the 100th of 200: half the requests took that long or less. The p95 is the 190th: 190 requests finished within it, and 10 took longer.
percentile()makes a sorted copy of the times with.toSorted(). The compare function,(a, b) => a - b, sorts them as numbers: without it, JavaScript compares them as text, and12587comes before1886. Functions You Can Pass Around covers sorting with a compare function.- Multiplying the count by the percentage and rounding up gives the position, and the minus one turns that position into an array index, which counts from zero.
- On the video’s day, total time had a p50 of 1,886 ms and a p95 of 5,670 ms. Half the hikers waited under two seconds. The p95 is three times the p50, and one in 20 waited even longer.
- The time to first token is the other wait: how long the hiker waits for the first words. A
streamAnswerspan’sfirstChunkMscounts from the start of the answer call, after retrieval and the tool round, so it’s only the model’s part. - The script pairs each span with its log line by the request ID. The line is written when the request finishes, so its
timeminus itsdurationMsis the start, and the first words went out at the span’sstartplus itsfirstChunkMs. The script keeps only the answers with a span that recorded a first chunk, so both numbers count the same answers: on the video’s day, all 200. - On the video’s day, the wait for the first words had a p50 of 1,178 ms and a p95 of 3,215 ms. That tail came mostly from rate-limit retries before the answer call, while long answers slowed only the total. The causes differ, so you report both.
- Report latency as percentiles: the p50 and the p95 of total time and of time to first token, never the average alone. With more traffic, add the p99 too. On a day of 200 requests, only two sit above it.
Final Code
Save this as scripts/latency.js. It reads the day’s request log and trace file, given as its two arguments: