AI agents · OpenClaw · self-hosting · automation

Quick Answer

How to Measure Time to First Token for LLM APIs (2026)

Published:

The short answer

Stream the response, timestamp the request, and timestamp the first chunk that carries visible text — that gap is time to first token (TTFT). Then compute tokens per second from first text chunk to last, using the provider’s own usage count. Run at least 30 requests per configuration and report p50 and p95. The script below works against OpenAI and any OpenAI-compatible endpoint (Groq, Cerebras, Fireworks); the Claude section shows which event to watch instead.

What to measure

MetricStartStopWhy it matters
TTFT (visible)just before the HTTP callfirst chunk with non-empty textWhat the user waits for
TTFT (any)samefirst chunk of any content, including reasoningShows queueing + prefill alone
Output tokens/sfirst text chunklast text chunkGeneration speed, independent of prompt size
Total latencyjust before the callstream endWhat a batch job or tool call waits for

Step 1 — a streaming timer for OpenAI-compatible APIs

import os, time, statistics
from openai import OpenAI

ENDPOINTS = {
    "groq":     ("https://api.groq.com/openai/v1", "GROQ_API_KEY", "openai/gpt-oss-120b"),
    "cerebras": ("https://api.cerebras.ai/v1",     "CEREBRAS_API_KEY", "gpt-oss-120b"),
}

def one_run(client, model, prompt):
    t0 = time.perf_counter()
    first_any = first_text = last_text = None
    usage = None
    stream = client.chat.completions.create(
        model=model, stream=True,
        messages=[{"role": "user", "content": prompt}],
        stream_options={"include_usage": True},
    )
    for chunk in stream:
        now = time.perf_counter()
        if chunk.usage:
            usage = chunk.usage
        if not chunk.choices:
            continue
        delta = chunk.choices[0].delta
        reasoning = getattr(delta, "reasoning", None) or getattr(delta, "reasoning_content", None)
        if (delta.content or reasoning) and first_any is None:
            first_any = now
        if delta.content:
            first_text = first_text or now
            last_text = now
    out = usage.completion_tokens if usage else 0
    gen = (last_text - first_text) if first_text and last_text and last_text > first_text else None
    return {
        "ttft_any": (first_any - t0) if first_any else None,
        "ttft_text": (first_text - t0) if first_text else None,
        "tps": out / gen if gen else None,
    }

Run it in a loop, drop the first (cold connection) result, and print statistics.median() and the 95th percentile for each metric. Reasoning field names differ by provider, so check one raw chunk first.

Step 2 — the right event for each API

  • OpenAI Responses API: the stream emits response.created first; time the first response.output_text.delta event for visible text and stop at response.completed.
  • Claude Messages API: the stream opens with message_start and may send ping events; reasoning arrives as content_block_delta with a thinking_delta, visible text as content_block_delta with a text_delta, and the stream ends with message_stop. Time the first text_delta.
  • Chat Completions (OpenAI, Groq, Cerebras, Fireworks): the first chunk often carries only the assistant role. Wait for a non-empty delta.content.

Step 3 — design the test so the numbers mean something

  1. Bucket by prompt size. Test at least three sizes your app actually sends — for example 1K, 32K and 128K input tokens. TTFT grows with prompt length because the provider must prefill it; a provider that wins at 1K can lose at 128K.
  2. Separate cold and warm cache. Send the same long prefix twice. The second request shows what prompt caching saves; for agent loops that is the number that matters (see how to maximize prompt cache hit rate).
  3. Fix the reasoning effort. Cerebras sets Qwen 3.8 27B reasoning to high by default and gpt-oss-120b to medium; compare providers at the same setting or you are measuring thinking time.
  4. Run from your production region, at your peak hour, for at least 30 requests per cell. Network round trip from another continent can add more than the model does.
  5. Log 429s and retries as failures, not as slow successes. A fast endpoint that rate-limits you is not fast.
  6. Pin max output tokens so long and short answers do not skew total latency.

Step 4 — read the results

  • High TTFT at small prompts → queueing or a cold connection; try a priority tier or another region.
  • TTFT that scales with prompt size → prefill cost; shorten the prompt or improve cache hits.
  • Big gap between “TTFT any” and “TTFT visible” → reasoning; lower the effort for interactive turns.
  • Low tokens per second → model size or provider hardware; see the vendor numbers in our low latency inference providers comparison and, for agent loops, inference providers for coding agents with low TTFT.

For ready-made load-testing tools (GuideLLM, NVIDIA AIPerf, inference-perf), see best LLM latency benchmarking tools.

Last verified: November 23, 2026.

Sources