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 78/ 160
04. Evals

P95 Latency

Tail latency targets for TTFT and end-to-end response time under load.

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

In plain words

p95 latency is how long the slowest 1 in 20 requests takes; looking at it shows the bad experiences that averages hide.

Think of it like…

A bus service: the average journey is fine, but if one trip in twenty takes triple the time, riders remember the bad trips and start taking cars.

Where you’ve seen it

From 100,000 daily requests the team sees mean 3.0 s, p50 2.1 s, p95 6.8 s and p99 14.0 s, so 5,000 requests a day take longer than 6.8 s. Traces show why: the LLM call has TTFT 0.6 s and decodes at about 150 tokens per second, so a typical 420-token answer takes 0.6 + 420/150 = 3.4 s while a 1,200-token answer takes 0.6 + 8.0 = 8.6 s. Long answers, provider retries and a cold vector index explain most of the slowest requests. Actions: stream tokens (perceived wait drops to the 0.6 s TTFT), cap output length with a concise-answer instruction (average 420 to 300 tokens saves 0.8 s), cache a stable prompt prefix, run retrieval and a permissions check in parallel, and send the second attempt of a request after the 6.8 s mark (hedging). p95 falls from 6.8 s to 4.6 s against an SLO of 5 s.

Words you’ll hear

Percentile (p50, p95, p99)
The value below which that share of requests fall; p95 is the 95th.
Time to first token (TTFT)
Delay from sending the request to receiving the first output token.
Tail latency
The slowest requests, where timeouts and bad experiences concentrate.
SLO
A target such as p95 under 5 seconds measured over a window.

Visual diagram

Loading interactive diagram

Theory & deep dive

Measuring it properly

Record latency per request and per stage (queue wait, retrieval, each model call, tools) as histograms and compute percentiles from the histogram, not from averages. Percentiles don't add: the p95 of a pipeline isn't the sum of its stages' p95s, and averaging p95 values across servers is wrong; aggregate the underlying histograms or raw samples. For streaming responses track TTFT and inter-token latency separately from total time, and slice by input and output length, model, feature and cache hit, because a single combined number mixes different workloads. For agents, measure end-to-end task time and the number of steps, since the tail is dominated by runs with many steps or retries.

Set an SLO (for example p95 under 5 s and p99 under 10 s over 28 days) and alert on burn rate rather than single spikes. Remember that the tail grows with fan-out: if a request makes 5 independent calls and each has a 5% chance of being slow, the chance that at least one is slow is 1 − 0.95⁵ = 22.6%, which is why agents and multi-call pipelines have worse tails than any single call.

Decode-time arithmetic

With TTFT 0.6 s and 150 tokens/s: 300 tokens → 0.6 + 2.0 = 2.6 s; 420 → 3.4 s; 1,200 → 8.6 s. Cutting average output from 420 to 300 tokens saves 0.8 s per request without touching the model.

Key takeaway: Histograms per stage, TTFT separate from decode, SLOs on percentiles, and respect fan-out.

Reducing latency

Levers by impact: shorten outputs (concise-answer instructions, max-token limits, structured outputs with short fields); stream so users see text after TTFT; cache (prompt-prefix caching lowers TTFT and cost for long shared prefixes, and a semantic or exact response cache skips the call); route easy requests to a smaller, faster model; parallelise independent work (retrieval alongside moderation) and avoid serial chains of model calls; trim context (fewer, better chunks reduce prefill time); and use speculative decoding or faster serving where you control the model.

Tame the tail: set timeouts per stage with fallbacks (a smaller model or a cached answer), use retries with jittered backoff and a retry budget so retries don't cause storms, consider request hedging (send a duplicate after the p95 delay and use whichever finishes first, costing about 5% extra calls), keep warm connections and pre-warm indexes after deploys, and apply admission control or queues when providers rate-limit. Always weigh latency against quality and cost: reasoning models and bigger contexts improve answers but lengthen both median and tail, so set per-feature budgets rather than a single global target.

Hedging

If p95 is 6.8 s, issue a second identical request when the first hasn't returned by 6.8 s and take the earlier response. About 5% of requests are duplicated (+5% calls) and the p99 drops sharply because slow outliers are often provider-side queue delays that don't repeat.

Key takeaway: Shorten and stream, cache and route, parallelise, and bound the tail with timeouts, budgets and hedging.

How it works, step by step

Step 1 of 4
Engineering

1. Instrument stages

Record per-stage timings, TTFT, token counts and cache hits in histograms and traces.

retrieval · rerank · LLM · tools
Key numbers at this step
Slice
length and model

When to use it & trade-offs

What it is

p95 latency is the response time that 95% of requests beat: 5% are slower. Percentiles (p50, p95, p99) describe the tail, which is what unhappy users actually experience, while the mean hides it. LLM latency has an unusual shape: time to first token (TTFT) depends on input length, queueing and caching, and then each output token adds a roughly constant decode time, so response length drives total time, and multi-step agents and retries stretch the tail further. Managing p95 means measuring it per stage, setting an SLO, and attacking the biggest contributors.

Why it matters: Users perceive slowness in the worst moments, and a feature with a 2-second median and a 14-second p99 feels unreliable. Long-tail latency also causes timeouts, retries and cascading load. In LLM products latency trades directly with quality (bigger models, more reasoning, more retrieval) and cost, so engineers need to quantify the budget, find where time goes and choose techniques such as streaming, caching, routing and parallelism deliberately.

When to reach for it

  • Setting SLOs for AI features
  • Explaining a slow tail and choosing fixes
  • Budgeting latency across a multi-step pipeline

When to avoid or be careful

  • Reporting only the mean
  • Averaging percentiles across instances
  • Retrying without jitter and a budget

Latency techniques

Streaming
Best for: Perceived responsiveness in chat UIs

Strengths: Users see output after TTFT; no quality loss.

Watch out: Doesn't reduce total time or backend load.

Caching and prompt-prefix reuse
Best for: Repeated queries and long shared prompts

Strengths: Cuts both latency and cost.

Watch out: Needs stable prefixes; staleness risk for response caches.

Model routing
Best for: Mixed easy and hard requests

Strengths: Fast, cheap handling of simple cases.

Watch out: Routing errors can hurt quality.

Interview signals & numbers to know

When this topic comes up

  • “Some requests take 15 seconds”
  • “What's our p95?”
  • “Time to first token”
  • Agents with many steps
  • Provider rate limits and retries

Benchmarks & rules of thumb

2.1 / 6.8 / 14.0 s -
p50 / p95 / p99 in the example
0.6 + tokens ÷ 150 -
seconds for TTFT plus decode in the example
22.6% -
chance at least one of 5 calls is in its slowest 5%

Common mistakes & follow-ups

Common interview mistakes

  • Computing the mean latency and declaring the feature fast.
  • Adding stage p95s to estimate total p95.
  • Letting long outputs run unbounded.

Likely interviewer follow-ups

  • How would you reduce p95 latency of an LLM feature?
  • Why do multi-step agents have worse tail latency?
  • How is perceived latency different from actual latency?
Interview tip: Give a decode-time formula (TTFT + tokens ÷ rate). It shows you can predict latency and see that output length is a lever.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply P95 Latency with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
OpenTelemetry for LLMs
Next topic
Cost per Request