Compute TTFT, TPOT and throughput from request timestamps

Serving dashboards report three numbers. Time to first token (TTFT) is how long the user waits for anything. Time per output token (TPOT) is the gap between later tokens. Throughput is output tokens per second.

Each request is a dict with "sent" (time the request arrived) and "tokens" (list of times each output token was emitted, at least 2). All in seconds.

Write latency_metrics(requests) returning a dict:
- "ttft": mean over requests of tokens[0] - sent
- "tpot": mean over requests of (tokens[-1] - tokens[0]) / (len(tokens) - 1)
- "throughput": total output tokens / (latest token time minus earliest sent time)

Signaturedef latency_metrics(requests)

Understand the idea

LLM serving is judged on three numbers. Time to first token (TTFT) is how long a user waits before anything appears; it covers queueing and prefill. Time per output token (TPOT) is the average gap between later tokens; it reflects the decode loop. Throughput is total output tokens per second across all requests. Like a restaurant: how long until the first dish arrives, then how quickly the rest follow. Worked example: request 1 sent at 0, tokens at [0.5, 0.7, 0.9]: TTFT = 0.5, TPOT = (0.9 - 0.5) / 2 = 0.2. Request 2 sent at 1.0, tokens at [1.3, 1.4]: TTFT = 0.3, TPOT = 0.1 / 1 = 0.1. Mean TTFT = 0.4, mean TPOT = 0.15. Throughput = 5 tokens / (1.4 - 0) = 3.571 tokens per second. Rule: ttft = mean(tokens[0] - sent); tpot = mean((tokens[-1] - tokens[0]) / (len - 1)); throughput = total tokens / (latest token time - earliest sent time). Inference dashboards in vLLM-style servers report these.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 2.

  • Sample 1: two requestslatency_metrics([{'sent': 0.0, 'tokens': [0.5, 0.6, 0.7, 0.8]}, {'sent': 0.2, 'tokens': [1.0, 1.2, 1.4]}])
  • Sample 2: one slow prefilllatency_metrics([{'sent': 1.0, 'tokens': [3.0, 3.05]}])