In a production RAG system with a 2-second end to end latency budget for non-streaming responses, which step typically dominates and where should the optimization effort go?
In a 2s RAG budget, LLM generation eats 1.2-1.8s; embed, retrieve, rerank, and augment together fit in 200-500ms. Optimize generation, not the retrieval stack.
Imagine a 2-second relay race with five runners. Four of them sprint their legs in a fraction of a second each: looking up the question's meaning, searching the library, reordering the best books, and gluing the pages into a packet. Then the fifth runner has to read the packet out loud, word by word, before anyone gets the answer. That last runner takes more than a full second on his own. If you want the race to finish faster, there's no point yelling at the four fast runners. You speed up the one who reads out loud: hand him a shorter packet, swap him for a quicker reader, or let him start speaking the moment he has the first word. In a RAG system the slow reader is the language model generating tokens one at a time.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3 min: enumerate the five steps with latencies, explain why generation is autoregressive and dominant, then list the generation side optimization levers in priority order.
| RAG step | Typical latency | Scales with | Optimization headroom |
|---|---|---|---|
| Query embedding | 50-100ms | Fixed (one forward pass) | Low |
| Vector retrieval (ANN) | 50-100ms | Barely (HNSW is log ish) | Low |
| Reranking (cross-encoder) | 100-200ms | Top-k candidate count | Low-medium |
| Prompt assembly | <10ms | Negligible | None |
| LLM generation | 1.2-1.8s | Output tokens + prompt size | High |
Real products, models, and research that use this idea.
- Perplexity streams its answer token by token so users read the response forming while generation is still in flight, hiding the 1s+ generation cost.
- OpenAI's Assistants and Responses APIs default to server sent event streaming precisely because non-streaming RAG answers feel slow on generation.
- Anthropic's Claude API exposes prompt caching so the static RAG prompt prefix skips re-prefill, cutting time to first token on repeated queries.
What an interviewer would ask next. Try answering before peeking at the approach.
QIf you must keep total wall clock under 2s and the answer is genuinely long, what do you change first?
QHow does the latency picture change when you move from managed APIs to self-hosted generation?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Assuming the vector database is the bottleneck. Well-tuned ANN retrieval is sub-100ms even at 10M docs; token by token generation is the part measured in seconds.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.