Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 87/ 160
05. AI System Design

Time to First Token

TTFT as the key UX metric for chat, and levers that improve it.

IntermediateTTFT · Queueing3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

TTFT is how long you wait before the AI starts answering; the shorter it is, the more responsive the app feels.

Think of it like…

Ordering at a café: the time until the barista acknowledges you and starts (TTFT) feels different from the time until the whole drink is done, and a long queue at the counter makes the first moment much worse than the prep itself.

Where you’ve seen it

A request with a 3,100-token prompt has TTFT = network and gateway 0.10 s + queue wait 0.11 s + prefill 0.39 s (3,100 tokens at 8,000 tokens per second) = 0.60 s. Prefix caching of the 2,400-token shared prefix cuts prefill to 700 tokens (0.09 s), so TTFT falls to about 0.30 s. Under heavy load the queue term dominates: if one GPU slot's service time is 0.39 s and utilisation rises from 50% to 90%, the average M/M/1 wait grows from 0.39 s to 3.5 s (wait ≈ ρ/(1 − ρ) × service time), so TTFT jumps from about 0.6 s to over 4 s without any change to the prompt. The team therefore tracks p95 TTFT, autoscales on queue depth rather than CPU, and sets a priority lane for interactive chat above batch jobs.

Words you’ll hear

Prefill
The pass over the whole prompt that builds the KV cache before the first output token.
Queue wait
Time a request waits for a free batch slot or GPU capacity.
Inter-token latency (ITL)
Time between successive output tokens after the first.
TTFT SLO
A target such as p95 TTFT under 1 second for interactive chat.

Visual diagram

Loading interactive diagram

Theory & deep dive

What determines TTFT

Prefill processes all prompt tokens in parallel and is compute-bound; its time is roughly prompt tokens divided by the engine's prefill throughput (for example 3,100 ÷ 8,000 ≈ 0.39 s for a large model on one GPU, shorter for smaller models or with tensor parallelism). That's why long system prompts, many retrieved chunks and long chat histories raise TTFT linearly, and why prefix caching and shorter prompts are the first levers. A bigger model has slower prefill, and quantisation or faster hardware reduce it. Multimodal inputs (images, audio) add encoder time before text prefill.

Queue wait depends on load. Serving engines batch requests; a new request waits for a slot, and, under chunked prefill, for earlier prefill work. Queueing theory says waiting time explodes as utilisation approaches 100%: for an M/M/1 approximation the mean wait is ρ/(1 − ρ) × S, so with S = 0.39 s it is 0.39 s at ρ = 0.5 but 3.5 s at ρ = 0.9. Other contributions are network and gateway overhead (TLS handshake, regional distance, authentication, retries), cold starts (a new replica loading tens of gigabytes of weights) and, on hosted APIs, provider-side queues, which you can't control but can route around with fallbacks.

Queue blow-up

Service time 0.39 s. Utilisation 50%: wait ≈ (0.5/0.5) × 0.39 = 0.39 s. Utilisation 90%: wait ≈ (0.9/0.1) × 0.39 = 3.51 s. TTFT rises from about 0.6 s to over 4 s with identical prompts, purely from load.

Key takeaway: TTFT is prefill (prompt length) plus queueing (load) plus overhead; queueing is what makes the tail explode.

Improving and measuring TTFT

Levers by effect: cut prompt tokens (rank and trim retrieved chunks, compress history, remove unused tool definitions); cache prefixes (provider prompt caching or engine prefix caching, with stable-first prompt layout); use a smaller or faster model for latency-critical paths (routing); add capacity and autoscale on queue depth or TTFT, not CPU; prioritise interactive traffic over batch in the scheduler; chunked prefill so long prompts don't stall others; keep warm replicas to avoid cold starts; reuse connections (HTTP/2, keep-alive) and place endpoints near users; and stream so the user sees the first token as soon as it exists. Avoid doing work before the model call that could run in parallel or after it (reranking, moderation, logging).

Measure TTFT per request in traces (time from request start to first chunk), as a histogram with p50, p95 and p99, sliced by prompt-length bucket, model, cache hit or miss and load level. Set an SLO (for example p95 TTFT < 1 s for chat) and alert on burn rate. Separate TTFT from inter-token latency and end-to-end latency: a change that lowers TTFT but increases ITL (larger batches) can make streaming feel choppy. For agents, the user-visible TTFT is for the *first visible output*; consider emitting a status message ("Searching your orders...") immediately while tools run, which makes the wait feel purposeful even when the model call comes later.

Prompt-length sensitivity

At 8,000 prefill tokens per second: 1,000 tokens = 0.125 s, 3,100 = 0.39 s, 12,000 = 1.5 s. Doubling retrieved context from 3,000 to 6,000 tokens adds about 0.375 s to TTFT before any queueing.

Key takeaway: Shorten and cache the prompt, prioritise interactive traffic, autoscale on queue signals and track p95 TTFT by segment.

How it works, step by step

Step 1 of 4
Engineering

1. Instrument TTFT

Record time to first chunk per request with prompt length, model, cache status and load tags.

histogram: p50 · p95 · p99
Key numbers at this step
Separate
TTFT, ITL, total

When to use it & trade-offs

What it is

Time to first token (TTFT) is the delay between sending a request and receiving the first generated token. It's the key responsiveness metric for chat and streaming UIs because it is when the user first sees a reaction. TTFT is the sum of network and gateway overhead, queue wait (waiting for a free slot or batch), prefill (processing the whole prompt, which grows with prompt length) and the first decode step. After TTFT, inter-token latency (time between tokens) governs how smoothly the rest streams.

Why it matters: Users judge responsiveness by the first sign of life: roughly 100 ms feels instant, about 1 second keeps flow, and several seconds feel broken. Because TTFT is dominated by prompt length and queueing, it responds to different levers than total latency, and it gets sharply worse under load. Knowing how TTFT is built lets you decide whether to shorten prompts, cache prefixes, add capacity or change the model.

When to reach for it

  • Chat and assistant UX targets
  • Capacity planning and autoscaling signals
  • Evaluating prompt-length changes

When to avoid or be careful

  • Autoscaling on CPU instead of queue depth
  • Reporting mean TTFT only
  • Ignoring queueing when utilisation is high

Latency metrics

TTFT
Best for: Responsiveness of chat and streaming UIs

Strengths: Maps to when users first see output.

Watch out: Ignores how long the full answer takes.

Inter-token latency
Best for: Smoothness of the streamed text

Strengths: Shows decode speed and batching effects.

Watch out: Not noticed if faster than reading speed.

End-to-end latency
Best for: Non-streaming API calls and total task time

Strengths: Complete picture for batch and agent tasks.

Watch out: Hides the first-response experience.

Interview signals & numbers to know

When this topic comes up

  • “The first word takes ages to appear”
  • “Latency spikes at peak hours”
  • Prefill and queueing
  • TTFT SLO
  • Chunked prefill

Benchmarks & rules of thumb

0.10 + 0.11 + 0.39 = 0.60 s -
TTFT components in the example
0.39 s → 3.5 s -
mean wait at 50% vs 90% utilisation (M/M/1)
~1 s -
interaction threshold for keeping a user's flow

Common mistakes & follow-ups

Common interview mistakes

  • Adding more retrieved chunks and not noticing prefill time grows linearly.
  • Running replicas at 90% utilisation and wondering why the tail is long.
  • Optimising decode speed while TTFT is the user-visible problem.

Likely interviewer follow-ups

  • What contributes to TTFT?
  • Why does TTFT explode under load?
  • How would you cut TTFT for a long system prompt?
Interview tip: Break TTFT into network, queue and prefill with numbers. It shows you diagnose latency by component instead of guessing.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Time to First Token with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Streaming Responses
Next topic
Throughput vs Latency