Skip to content
AgenticAIWiki
  1. Learn
  2. Part 13
  3. 67. Observability and tracing
  4. 67.9

Lesson 67.9 · 9 of 9 · 5 min read · Reviewed 24 Nov 2026

Time to first token and other latency metrics

By the end, you can

  • Define time to first token, time per output token and end-to-end latency.
  • Measure each one correctly and know what moves it.

“How fast is it?” has several answers for an LLMLarge language model (LLM): A neural network trained on large amounts of text to predict the next token, which lets it generate and transform language.. A reply that starts in half a second and then streamsStreaming: Receiving a response as a series of server-sent events while it is generated, instead of waiting for the whole reply. for ten seconds feels very different from one that shows nothing for five seconds and then appears at once.

The metrics

Metric What it measures Who feels it
Queue time Waiting in your own queue or limiter before the call starts Everyone, at busy times
Time to first token (TTFT) From sending the request to the first piece of output People watching a streaming UI
Time per output token (inter-token latency) Average gap between output tokens after the first People reading a long answer
Output tokens per second The same thing turned around: 1 / time per token Same
End-to-end latency From request to the last token Agents, batch jobs, anything that needs the whole answer
Agent run time All model and tool spans of one run Users waiting for a task to finish

A rough formula

end-to-end ≈ queue time + TTFT + (output tokens - 1) x time per output token

Worked example with made-up numbers: queue 0.2 s, TTFTTime to first token (TTFT): How long a user waits between sending a request and seeing the first token of the reply. 0.8 s, 400 output tokensOutput tokens: The tokens the model generates in its reply, including any tool calls and thinking. at 50 tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. per second.

0.2 + 0.8 + 399 x 0.02 ≈ 9.0 s

The person sees text after about 1 second, so it feels quick. A batch job that needs the whole answer waits about 9 seconds. Halve the answer length and the total drops to about 5 seconds; TTFT doesn’t change.

What moves each number

Change TTFT Time per token Total
Longer input (big documents, long history) Up: the model must read it all first Little change Up
Prompt caching of a long prefix Down Little change Down
Longer output No change No change Up
Bigger, slower model Usually up Usually up Up
Far-away region or busy provider Up Little change Up
Extended thinking or reasoning before the answer Up, if you count to the first visible text Little change Up

Measure it correctly

  • Measure on the client side, from just before you send the request. Server-side numbers miss network and your own queue.
  • Count to the first content, not the first event. A stream usually opens with a start event before any text. In Claude’s streaming API, message_start arrives first and text comes in content_block_delta events; time to the first text delta.
  • Record output token counts with the timing, so you can compute time per token: (total - TTFT) / (output tokens - 1).
  • Use percentiles (67.2). Slice by feature, model and input size, because a long-document feature will always have a higher TTFT.

OpenTelemetryOpenTelemetry: An open standard and toolkit for collecting traces, metrics and logs from software.’s GenAI metrics conventions include names for operation duration and time to first token, so the same chart works across providers.

When to use which

  • Chat UI: set a target for p95 TTFT. Streaming makes long answers acceptable.
  • AgentsAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done.: TTFT matters less than run time and steps per run. Each step pays TTFT again, so fewer, larger steps can be faster.
  • Batch: only throughputThroughput: How much work a system completes per unit of time, such as requests per second or tokens per minute. and total time matter.

Common mistakes

  • Reporting one “latencyLatency: The time from sending a request until you get the response, or until the first token arrives when streaming.” number that mixes TTFT and length.
  • Measuring TTFT to the first stream event, which looks great and means nothing.
  • Blaming the model when the time is spent in your own queue.

Key points

  • Time to first token (TTFT) is how long until the first piece of the answer arrives; it is what a person feels in a streaming UI.
  • Time per output token sets how fast the rest streams; end-to-end latency is roughly queue time + TTFT + output tokens x time per token.
  • Long inputs mostly raise TTFT; long outputs mostly raise total time. Prompt caching and shorter prompts help the first; shorter answers help the second.
  • Measure on the client, to the first content event, and chart percentiles by feature.
Hands-on labRuns in the Playground

Measure TTFT from stream events

Compute time to first token, time per output token and end-to-end latency correctly from a stream.

You will have: A metrics function that prints TTFT, tokens per second and total latency for one streamed reply.

Steps

  1. Run the starter: TTFT looks like 0.05 s because it times the first event.
  2. Fix ttft() to use the first text delta.
  3. Compute time per output token from the deltas.
  4. Add the queue time to get what the user really waits.
  5. Change the number of output tokens and see which metric moves.

Done when

  • TTFT prints 0.80 s, not 0.05 s.
  • Throughput prints 50 tokens/s.
  • End-to-end including queue prints 9.0 s, matching the lesson's formula.

Open in the Playground No API key needed.

Starter code (Python)
# Measure TTFT, time per output token and end-to-end latency from stream events.
QUEUE_SECONDS = 0.2  # time spent in your own queue before the request was sent

# Simulated stream: (seconds since the request was sent, event type, text)
EVENTS = [(0.05, "message_start", ""), (0.06, "content_block_start", "")]
for i in range(400):
    EVENTS.append((round(0.80 + i * 0.02, 3), "content_block_delta", "tok"))
EVENTS += [(8.79, "content_block_stop", ""), (8.79, "message_delta", ""), (8.79, "message_stop", "")]


def first_event(events):
    return events[0][0]


def ttft(events):
    """Time to the first event that carries text, not the first event."""
    # TODO: return the time of the first content_block_delta that has text.
    # This placeholder times the first event, which looks great and means nothing.
    return events[0][0]


def metrics(events):
    deltas = [t for t, kind, text in events if kind == "content_block_delta" and text]
    first = ttft(events)
    # TODO: time per output token = (last delta - TTFT) / (number of deltas - 1)
    per_token = 1.0
    total = events[-1][0]
    return first, per_token, len(deltas), total


first, per_token, n, total = metrics(EVENTS)
print(f"first event: {first_event(EVENTS):.2f} s (not TTFT)")
print(f"TTFT (first text): {first:.2f} s")
print(f"time per output token: {per_token:.3f} s ({1 / per_token:.0f} tokens/s) over {n} tokens")
print(f"end-to-end incl. queue: {QUEUE_SECONDS + total:.1f} s")
print(f"what the user feels before text: {QUEUE_SECONDS + first:.1f} s")
Show a solution
# Measure TTFT, time per output token and end-to-end latency from stream events.
QUEUE_SECONDS = 0.2  # time spent in your own queue before the request was sent

# Simulated stream: (seconds since the request was sent, event type, text)
EVENTS = [(0.05, "message_start", ""), (0.06, "content_block_start", "")]
for i in range(400):
    EVENTS.append((round(0.80 + i * 0.02, 3), "content_block_delta", "tok"))
EVENTS += [(8.79, "content_block_stop", ""), (8.79, "message_delta", ""), (8.79, "message_stop", "")]


def first_event(events):
    return events[0][0]


def ttft(events):
    """Time to the first event that carries text, not the first event."""
    return next(t for t, kind, text in events if kind == "content_block_delta" and text)


def metrics(events):
    deltas = [t for t, kind, text in events if kind == "content_block_delta" and text]
    first = ttft(events)
    per_token = (deltas[-1] - first) / (len(deltas) - 1)
    total = events[-1][0]
    return first, per_token, len(deltas), total


first, per_token, n, total = metrics(EVENTS)
print(f"first event: {first_event(EVENTS):.2f} s (not TTFT)")
print(f"TTFT (first text): {first:.2f} s")
print(f"time per output token: {per_token:.3f} s ({1 / per_token:.0f} tokens/s) over {n} tokens")
print(f"end-to-end incl. queue: {QUEUE_SECONDS + total:.1f} s")
print(f"what the user feels before text: {QUEUE_SECONDS + first:.1f} s")
Quick checkUsers say the chat feels slow to start, but once text appears it streams quickly. Which metric should you look at first?
How sure are you?
Pick an answer and how sure you are

Finished the chapter? Take the chapter check.