How to Measure Time to First Token for LLM APIs (2026)
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
| Metric | Start | Stop | Why it matters |
|---|---|---|---|
| TTFT (visible) | just before the HTTP call | first chunk with non-empty text | What the user waits for |
| TTFT (any) | same | first chunk of any content, including reasoning | Shows queueing + prefill alone |
| Output tokens/s | first text chunk | last text chunk | Generation speed, independent of prompt size |
| Total latency | just before the call | stream end | What 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.createdfirst; time the firstresponse.output_text.deltaevent for visible text and stop atresponse.completed. - Claude Messages API: the stream opens with
message_startand may sendpingevents; reasoning arrives ascontent_block_deltawith athinking_delta, visible text ascontent_block_deltawith atext_delta, and the stream ends withmessage_stop. Time the firsttext_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
- 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.
- 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).
- Fix the reasoning effort. Cerebras sets Qwen 3.8 27B reasoning to
highby default and gpt-oss-120b tomedium; compare providers at the same setting or you are measuring thinking time. - 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.
- Log 429s and retries as failures, not as slow successes. A fast endpoint that rate-limits you is not fast.
- 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.