Skip to main content
AI Relay
TutorialsLatencyTTFTPerformance Testing

How to Measure AI API Latency and Time to First Token (TTFT)

2026-11-225 min readAI Relay Ranking Editorial

Why measure latency yourself

The "millisecond response" on a relay's landing page is usually a best-case number. What your product needs to know is how long it takes from your server sending a request to receiving the first token, and whether that jitters at peak hours. You can only learn that with real traffic.


Two metrics to separate

  • TTFT (time to first token): from sending the request to the first output token; it drives perceived speed
  • Total time: from sending the request to the end of the stream; it depends on output length and generation speed

Chat applications care most about TTFT; batch jobs care about total time and throughput.


Measuring TTFT with Python

Most relays expose an OpenAI-compatible API, so the script only needs your base_url, api_key and model:

import time, statistics
from openai import OpenAI

client = OpenAI(base_url="https://YOUR_RELAY/v1", api_key="YOUR_KEY")

def measure(model="gpt-4o", prompt="Introduce yourself in one sentence"):
    start = time.perf_counter()
    first = None
    stream = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": prompt}],
        stream=True,
        max_tokens=64,
    )
    for chunk in stream:
        if first is None and chunk.choices and chunk.choices[0].delta.content:
            first = time.perf_counter() - start
    total = time.perf_counter() - start
    return first, total

samples = [measure() for _ in range(30)]
ttft = sorted(s[0] for s in samples if s[0] is not None)
print("TTFT p50: %.0f ms" % (statistics.median(ttft) * 1000))
print("TTFT p95: %.0f ms" % (ttft[int(len(ttft) * 0.95) - 1] * 1000))

Making results trustworthy

  1. Sample many times: dozens of runs, and look at p50 and p95 rather than a single call
  2. Test across the day: morning peak, evening peak and late night
  3. Hold variables fixed: same model, same prompt, same max_tokens
  4. Test from the target environment: your real servers, not a laptop
  5. Record failures: timeouts, 429s and 5xx matter as much as latency

Cross-checking with this site

Latency figures on the rankings are estimates for relative comparison; your real experience depends on network path and time of day. Try the script on two or three candidates before integrating, and check availability and scores on the rankings.

Related Articles

Want to find the most reliable relay?

Explore our overall and category rankings across uptime, speed, and payment options

View Rankings