Break down the latency and cost budget of a RAG query. What do you optimise first?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Typical single-shot pipeline, per query:
| Stage | Latency | Cost |
|---|---|---|
| Query rewrite (small LLM) | ~100-300ms | ~$0.0001-0.001 |
| Query embedding | ~10-50ms | negligible |
| ANN + BM25 search | ~5-50ms | infra, amortized |
| Cross-encoder rerank (~100 docs) | ~50-300ms | ~$0.001-0.01 (API) |
| Context assembly | ~1ms | - |
| LLM generation | ~1-10s | ~$0.005-0.05+ |
Full worked answer
Typical single-shot pipeline, per query:
| Stage | Latency | Cost |
|---|---|---|
| Query rewrite (small LLM) | ~100-300ms | ~$0.0001-0.001 |
| Query embedding | ~10-50ms | negligible |
| ANN + BM25 search | ~5-50ms | infra, amortized |
| Cross-encoder rerank (~100 docs) | ~50-300ms | ~$0.001-0.01 (API) |
| Context assembly | ~1ms | - |
| LLM generation | ~1-10s | ~$0.005-0.05+ |
The punchline: generation dominates both budgets - usually 70-90% of latency and a similar share of cost. So optimise there first: (1) smaller/faster generator where quality allows, or a model router sending easy queries to the cheap model; (2) fewer, tighter chunks - input tokens are the cost driver, and trimming k from 20 to 8 chunks often improves quality (less distraction) while cutting cost; (3) streaming - time-to-first-token is what users perceive; streaming makes a 6s generation feel like 500ms; (4) prompt caching - structure prompts so the static system prompt and stable prefix are cache hits (cached input tokens are steeply discounted, commonly ~75-90% off on current models from the major providers, though the exact rate, cache-write surcharges and TTLs vary, so check the price sheet); with contextual/conversational reuse this is a major cost lever.
Pre-generation stages matter mainly for perceived latency stack-up before streaming starts: parallelize dense + BM25 (they're independent), run query rewrite concurrently with a provisional retrieval when feasible, use a small local embedder or cache query embeddings, and cap reranker candidates (100 → 50 halves that stage). Skip rewriting for queries that don't need it (a cheap classifier gates it).
Offline costs people forget: embedding the corpus (cheap per token - small API embedders have long cost cents per million tokens - but re-embedding 100M chunks on a model migration is a real bill), contextual enrichment (an LLM call per chunk - prompt caching cuts it dramatically), and index RAM (HNSW at high dimensionality is a standing hardware cost).
For agentic RAG, multiply the generation line by the number of loop iterations - which is why cost telemetry per answer and search budgets are mandatory there.
What a good answer covers
- The punchline: generation dominates both budgets - usually 70-90% of latency and a similar share of cost
- So optimise there first: (1) smaller/faster generator where quality allows, or a model router sending…
- Pre-generation stages matter mainly for perceived latency stack-up before streaming starts
- Skip rewriting for queries that don't need it (a cheap classifier gates it)
- Offline costs people forget
Key terms: LLM generation · ~1-10s · ~$0.005-0.05+ · generation dominates both budgets · smaller/faster generator · fewer, tighter chunks · streaming · prompt caching · embedding the corpus · contextual enrichment
In the glossary: ANN · BM25 · Cross-encoder · Prompt caching · HNSW · Agentic RAG
Interviewer follow-ups to expect
Your p95 is 4s and product wants 1.5s - walk through cuts in order. How does prompt caching interact with per-user ACL'd context?
The punchline: generation dominates both budgets - usually 70-90% of latency and a similar share of cost. So optimise there first: (1) smaller/faster generator where quality allows, or a model router sending easy queries to the cheap model; (2) fewer, tighter chunks - input tokens are the cost driver, and trimming k from 20 to 8 chunks often improves quality (less distraction) while cutting cost; (3) streaming - time-to-first-token is what users perceive; streaming makes a 6s generation feel like 500ms; (4) prompt caching - structure prompts so the static system prompt and stable prefix are cache hits (cached input tokens are steeply discounted, commonly ~75-90% off on current models from the major providers, though the exact rate, cache-write surcharges and TTLs vary, so check the price sheet); with contextual/conversational reuse this is a major cost lever.
At what QPS does self-hosting the reranker beat the API?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep