SproutStackSproutStack home

Latency Optimization

5 min readBeginner-friendlyFree to read

Read a little, play a little. No scary maths, and no rush.

Share:

Users feel the first token, not the last

A 10-second answer that starts instantly feels fast; a 3-second answer after dead silence feels broken. The metric that matters most is TTFT — time to first token. Everything in this chapter serves it: get something on screen immediately, then let the rest stream in behind.

Stream from the first token

Don't wait for the full answer — send tokens over SSE (server-sent events) as they're generated, rendering word by word like ChatGPT does. Perceived latency collapses even though total time is identical. Streaming is the single highest-leverage latency win and it's nearly free to implement.

Stop recomputing: the KV-cache

Generating token #500 requires re-reading tokens 1–499 — unless you cache their keys and values (KV-cache), turning each step from "reread everything" into "read one new token." The first token (prefill) stays slow; every token after gets dramatically cheaper. Long conversations live or die by this cache, which is also why providers charge for cached vs fresh input tokens differently.

Draft fast, verify big

Speculative decoding pairs a tiny draft model (fast, sloppy) with your big model (slow, careful): the small one proposes 5 tokens, the big one verifies them in one pass, keeping the ones it agrees with. When drafts are usually right — and they are — you get big-model quality near small-model speed.

The practical checklist

Short prompts (every extra input token slows prefill), smaller models for triage with big-model escalation, vector search colocated with generation (cross-region retrieval murders TTFT), and retrieval running in parallel with prompt setup — never sequential when avoidable.

A worked example: trimming a RAG response

A trace of one request, measured to the first visible token:

  • Embed the question: 50 ms
  • Vector search, in another region: 180 ms
  • Rerank: 120 ms
  • Build the prompt: 20 ms
  • Model time to first token: 900 ms

Total: about 1.27 seconds of blank screen. Three changes, each aimed at the trace:

  • Move vector search into the same region as everything else: 180 ms becomes about 20 ms.
  • Shorten a bloated system prompt, so the model has less input to process before its first token: 900 ms becomes about 650 ms.
  • Stream the answer, so the first words appear the moment they exist.

The first token now arrives in about 0.86 seconds. Nothing was guessed: each fix came from a number in the trace.

Percentiles, not averages

An average hides the slow requests users actually complain about. Track the median (p50) for a typical request, and p95 and p99 for the slowest 5% and 1%. A p50 of 0.8 seconds with a p99 of 9 seconds means one request in a hundred feels broken, and the average will never tell you.

Prompt caching

Many providers cache the beginning of a prompt that repeats across requests, such as a long system prompt or a fixed set of documents. Cached input is processed faster and usually billed at a lower rate. To benefit, put stable content first and the parts that change, like the user's question, at the end.

Remember this

  • Optimize TTFT first, total time second. Stream always.
  • Cache keys/values, draft-then-verify, keep retrieval close and parallel.

Check your understanding

5 questions · first-try answers earn the most XP

1. Why does the chapter prioritise time-to-first-token over total generation time?
2. What does the KV-cache save during generation?
3. How does speculative decoding keep large-model quality at lower cost?
4. Why should retrieval run in parallel with prompt setup rather than after it?
5. Why does keeping vector search in the same region as generation matter?

My notes

Saved in this browser. Highlight a line above and save it, or write it in your own words.

Nothing saved yet. Your highlights will live here.

References

Finished reading?

Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.