Example

Compute TTFT, TPOT and throughput from a request trace

Aggregate two requests into percentiles and a token rate, then check the denominator that makes the throughput number mean something.

Latency reports are easy to make incomparable. Time to first token (TTFT) is not inter-token latency; time per output token (TPOT) is not end-to-end latency; and throughput needs a denominator. Two services can report the same “tokens per second” while measuring different intervals, token counts or concurrency.

This example aggregates two request records. It computes TTFT p50 and p99, TPOT p50, end-to-end p50, and a token rate from total output tokens over the sum of per-request end-to-end times. The numbers are small and deterministic. The important part is the definition: the throughput denominator here is additive request time, not wall-clock time under concurrency. It is a per-trace rate, not a serving capacity.

Run it

From the canonical repository at commit f958967d70992d7b09d9c75000bd8c543f1644d4:

python examples/ch36/metrics.py --json

Python 3.11 or later, standard library only. No model, network, GPU or credentials. It finishes immediately.

Implementation

percentile linearly interpolates between sorted values. aggregate computes the percentiles, sums the token counts, and divides output tokens by the sum of end-to-end times. render_report formats the same numbers for a human.

def percentile(values: List[float], p: float) -> float:
    if not values:
        return 0.0
    if p < 0 or p > 100:
        raise ValueError("percentile must be between 0 and 100")
    ordered = sorted(values)
    if len(ordered) == 1:
        return ordered[0]
    rank = (p / 100.0) * (len(ordered) - 1)
    low = int(rank)
    high = min(low + 1, len(ordered) - 1)
    frac = rank - low
    return ordered[low] * (1 - frac) + ordered[high] * frac


def aggregate(metrics: List[RequestMetrics]) -> dict:
    ttfts = [m.ttft for m in metrics]
    tpots = [m.tpot for m in metrics]
    e2es = [m.e2e for m in metrics]
    total_output = sum(m.output_tokens for m in metrics)
    total_prompt = sum(m.prompt_tokens for m in metrics)
    total_time = sum(m.e2e for m in metrics)
    return {
        "count": len(metrics),
        "ttft_p50": percentile(ttfts, 50),
        "ttft_p99": percentile(ttfts, 99),
        "tpot_p50": percentile(tpots, 50),
        "e2e_p50": percentile(e2es, 50),
        "throughput_tokens_per_second": (total_output / total_time) if total_time > 0 else 0.0,
        "total_prompt_tokens": total_prompt,
        "total_output_tokens": total_output,
    }

Expected outcome

Observed with the command above (exit 0):

{
  "aggregate": {
    "count": 2,
    "ttft_p50": 0.15000000000000002,
    "ttft_p99": 0.199,
    "tpot_p50": 0.025,
    "e2e_p50": 0.6,
    "throughput_tokens_per_second": 41.66666666666667,
    "total_prompt_tokens": 22,
    "total_output_tokens": 50
  },
  "report": "count: 2\nTTFT p50: 0.150000\nTTFT p99: 0.199000\nTPOT p50: 0.025000\nE2E p50: 0.600000\nthroughput tok/s: 41.67"
}

The two TTFTs are 0.1 and 0.2, so the interpolated p50 is 0.15 and p99 is 0.199. The two TPOTs are 0.02 and 0.03, so p50 is 0.025. The two end-to-end times are 0.5 and 0.7, so p50 is 0.6. Throughput is 50 / (0.5 + 0.7) = 41.67, not 50 / max(0.5, 0.7) and not a wall-clock rate for concurrent requests.

Mechanism and limitations

Documented: percentile rejects a value outside 0-100 and returns 0.0 for an empty list. It interpolates linearly between order statistics. aggregate sums output and prompt tokens and divides output tokens by the sum of end-to-end times. The tests check a single-value percentile, bounds, aggregation, report rendering, the demo, JSON serialisation and the empty percentile.

Observed: the command exited 0 with the aggregate and report above. The targeted tests.test_ch36_metrics is part of the canonical suite; no independent test count is claimed here.

Not shown: a p99 that is meaningful with two samples; wall-clock throughput under concurrency; queueing delay; a real clock; or a model. A percentile from two observations is a description of those two observations, not a tail estimate. The throughput denominator is additive time, which is not how a concurrent server elapses time.

Challenge this result

Add a third request with a TTFT of 1.0 and an end-to-end time of 2.0. Predict the new p50 and p99, then compute them by hand. Add a second concurrent worker and explain why the aggregate’s additive denominator would be wrong for a wall-clock throughput measurement. Finally, define the exact numerator and denominator you would require before comparing two engines’ tokens-per-second numbers.

Source and verification

Canonical source: examples/ch36/metrics.py at commit f958967d70992d7b09d9c75000bd8c543f1644d4. Tests: tests/test_ch36_metrics.py . Chapter: Latency, Throughput and Time to First Token . The run above is a fresh local execution on Windows 11 with Python 3.13.15; it uses a fixed request trace, not a live service.

Understand this example

Copy this prompt into your AI tool. No code runs here.

Apply this example

Copy this prompt into your AI tool. No code runs here.