Skip to content
mentoor

Topic 2 of 5 · 5 min read

Measuring time to first token and p95 latency

Goal: Compute p50 and p95 from a latency table and say which metric a streaming chat and a background job should be budgeted on.

Measure latency as a distribution, split into the parts a user feels: time to first token, then tokens per second, then total time. One average number hides the slow requests that generate the complaints.

Check yourself · 1 of 3

What does time to first token measure?

Choose one answer

The metrics

  • Time to first token (TTFT): from sending the request to the first streamed token. It includes queueing, the model reading your prompt, and any reasoning before output. Long prompts and long reasoning raise it.
  • Output speed: tokens per second (or inter-token latency) after the first token. Long answers cost more time here.
  • End-to-end latency: the whole request, roughly TTFT plus output tokens divided by output speed.
  • Percentiles: p50 is the median. p95 is the value 95 percent of requests beat. p99 is the slow tail. Report p50 and p95 at least.

Which one you budget depends on the feature. A streaming chat is judged on TTFT, because reading starts at the first token. A background job or a JSON response parsed in code is judged on end-to-end latency.

How to measure

  1. Time on the client and the server. Log a start time, the first-token time and the end time on every request, plus input and output token counts.
  2. Time every step of an agent run. Each model call, tool call and retrieval gets its own span. In a six-step run the slowest step is rarely the one you expect.
  3. Compute percentiles over enough requests, per feature and per model, not across the whole app.
  4. Plot over time. A p95 that drifts upward often points to growing prompts.

Before and after

Weak: "Average latency is 2.1 seconds, we are fine."

Better: ten requests took 1.2, 1.3, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 2.0 and 8.2 seconds. The average is 2.2, but p50 is 1.55 and the slowest request took more than five times the median. With a target of p95 under 3 seconds, this feature fails, and the average never showed it.

import numpy as np
lat = [1.2,1.3,1.3,1.4,1.5,1.6,1.7,1.8,2.0,8.2]
print(np.percentile(lat, [50, 95]))

What goes wrong

  • Averaging percentiles across servers or days. Compute them from the raw samples, or from histograms that can be merged.
  • Measuring only the model call. Retrieval, tool calls, queueing and your own code add time the user also waits through.
  • Testing with short prompts. TTFT grows with input length, so test with real ones.
  • Too few samples. A p99 from 50 requests is one or two requests.

Examples

Reading a latency table

A table of 20 requests sorted by total time: the 10th and 11th values are 1.5 and 1.6 seconds, so p50 is about 1.55 s. The 19th value (95 percent of 20) is 3.9 seconds and the 20th is 9.4 seconds, so p95 is about 3.9 s.

If the budget says p95 under 3 seconds, the feature fails, even though the median looks healthy. The two slowest requests, at 3.9 and 9.4 seconds, are the ones to open in the trace viewer.

An agent run broken into spans

A support agent takes 14 seconds. Its spans: plan call 2.1 s, search tool 1.2 s, rerank 0.4 s, answer call 3.0 s, policy lookup tool 7.0 s, final check 0.3 s.

Swapping to a faster model would save little. The policy lookup is half the run, so that is the fix, and without per-step timing nobody would see it.

Key points

  • Time to first token covers queueing, prompt processing and any reasoning before output, and is the metric a streaming reader feels first.
  • End-to-end latency is roughly time to first token plus output tokens divided by tokens per second, so long answers cost time even on a fast model.
  • Averages hide the slow tail; report p50 and p95, and p99 when the volume is high enough to make it meaningful.
  • Budget a streaming chat on time to first token and a background or parsed response on end-to-end latency.
  • Record a span for every model call and tool call in an agent run, because the slowest step is often not the model.

Common mistakes

  • Reporting average latency as the headline number, which hides the slowest requests.
  • Computing a percentile of percentiles instead of from raw samples or mergeable histograms.
  • Budgeting a streaming chat on total time, when users judge it on the first token.
  • Timing only the model API call and ignoring retrieval, tools, queues and post-processing.

Check yourself · 2 of 3

Ten requests took 1.2, 1.3, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 2.0 and 8.2 seconds. Why is the average of 2.2 seconds misleading?

Choose one answer

Check yourself · 3 of 3

An agent run takes 14 seconds. Spans show the model calls total 5 seconds and one tool call takes 7 seconds. What should you optimise first?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic