How would you design a system to serve a 7B-parameter large language model (LLM) chat service at scale? For a 32-layer model with hidden size 4,096 in 16-bit floating point (fp16, 14 GB of weights), what are the key–value (KV) cache memory per token, the memory for a 4,096-token sequence, roughly how many full-length sequences fit on an 80 GB GPU (graphics processing unit), and what is the serving cost per million output tokens if a GPU costs USD 2 per hour and sustains 2,500 output tokens/s?

ML system design, Case studies
Level L5 Expert. Length Long. Nature Th+Mat Both.

What's being asked

Whether you understand the memory-bandwidth and KV-cache bottlenecks of autoregressive LLM serving and can turn hardware arithmetic into a high-throughput, low-latency serving architecture.

Key idea

Chat serving splits into compute-bound prefill (processing the prompt) and memory-bandwidth-bound decode (reading weights and cached keys and values, K and V, for each output token). Using continuous (iteration-level) batching and PagedAttention (paging the KV cache into fixed-size blocks) removes memory fragmentation and boosts concurrency: each token takes 2×32×4,096×2 B=524,288 B=0.5 MiB (219 bytes), a 4,096-token sequence takes 2 GiB (231 bytes, ≈2.15 GB), about 30 full-length sequences fit on an 80 GB GPU, and 2,500 output tokens/s at USD 2/h yields about USD 0.22 per million tokens.

Steps

  1. Clarify requirements and scale: define the workload for a 7B-parameter chat model (for example, average 1,000 prompt tokens and 300 output tokens, max context 4,096 tokens), target QPS (queries per second), streaming token delivery, and strict latency targets for TTFT (time to first token, say under 500 ms) and TPOT (time per output token, say under 30–50 ms for smooth reading speed).
  2. Define the ML and system metrics: offline, verify output quality parity (perplexity and benchmark accuracy) whenever applying quantisation or speculative decoding. Online, track TTFT (driven by queueing plus prefill), TPOT (driven by decode step latency), end-to-end latency, throughput in output tokens/s per GPU (graphics processing unit), KV-cache utilisation, and cost per million tokens.
  3. Characterise workload data and KV-cache behaviour: chat traffic has variable sequence lengths, bursty arrivals, and shared system prompts across requests. As covered in earlier KV-cache fundamentals, autoregressive decoding caches past K and V tensors so they are not recomputed, making decode memory-bandwidth-bound rather than compute-bound because every generated token streams all model weights and active KV entries from GPU memory.
  4. Compute the KV-cache memory and concurrency budget: for 32 layers, hidden dimension d=4,096 and fp16 (2 bytes per element), one token needs 2 (K and V)×32×4,096×2 B=524,288 B=0.5 MiB (1 MiB=1,048,576 B). A full 4,096-token sequence therefore requires 4,096×0.5 MiB=2,048 MiB=2 GiB≈2.15 GB (1 GiB=230 B). On an 80 GB GPU holding 14 GB of fp16 weights, 80−14=66 GB remain; 66/2.147≈30.7, so roughly 29–30 full-length sequences fit concurrently after reserving about 2 GB for activations and runtime buffers (and proportionally more, over 100, at typical 1,024-token conversations).
  5. Design the serving engine from naive to production-grade: replace static request-level batching (which stalls finished requests until the longest sequence ends and wastes KV memory by pre-allocating max length) with continuous (iteration-level) batching, adding new requests at every decoding step. Pair this with PagedAttention, which divides each sequence's KV cache into non-contiguous fixed-size blocks (for instance, 16 tokens per block) via a block table, eliminating internal and external fragmentation and enabling copy-on-write prefix sharing for common system prompts. Improve further with 8-bit floating point (FP8) or 8-bit integer (INT8) weight/KV quantisation or grouped-query attention if retraining is allowed.
  6. Plan serving architecture, scheduling and cost: separate or schedule prefill and decode carefully so a long prompt prefill does not stall active decode batches and spike TPOT. Use chunked prefill (splitting long prompts across steps) or disaggregated prefill and decode GPU pools behind a prefix-aware router, plus preemption (swapping KV blocks to host memory or recomputing) when memory fills. Compute unit serving cost: a GPU at USD 2/h sustaining 2,500 output tokens/s produces 2,500×3,600=9,000,000 tokens/h, costing USD 2/9≈USD 0.22 per million output tokens (at 100% utilisation; real deployments add headroom for peak traffic).
  7. Evaluate and launch: replay production-like chat traces with realistic prompt and output length distributions under load to measure p95 and p99 TTFT and TPOT across arrival rates, verify zero numerical regression from quantisation or custom attention kernels, then launch behind shadow traffic and a progressive canary rollout.
  8. Monitor, iterate and address interviewer trade-offs: alert on 99th-percentile (p99) TTFT and TPOT, request queue depth, preemption/swap rate, and KV-cache pressure. Be ready to discuss key trade-offs: larger batch sizes raise throughput and lower cost per token but worsen TPOT; smaller PagedAttention blocks reduce internal waste at the cost of extra kernel indexing overhead; and swapping KV blocks to host memory saves recompute FLOPs (floating-point operations) when host–GPU bus bandwidth is ample, whereas recomputation wins for short prefixes.

Common mistakes

How long your answer should be

About 7–9 minutes: derive the 0.5 MiB/token and 2 GiB/sequence KV figures, give ~30 concurrent sequences and USD 0.22/1M tokens, then explain continuous batching, PagedAttention and prefill–decode scheduling.

Read this

Previous in ML system design: How would you design a visual search system that finds products looking like an uploaded photo? Assuming 500 images/s per GPU (graphics processing unit) and USD 2 per GPU-hour, how much GPU time and cost does it take to re-embed a 500-million-image catalogue when upgrading the embedding model?
Next in ML system design: How would you plan the infrastructure for training a 70B-parameter language model on 1.4 trillion tokens? Using C≈6ND FLOPs (floating-point operations, for N parameters and D tokens) and 16 bytes per parameter for mixed-precision AdamW model states, how much model-state memory does it need, how long does it take on 1,024 GPUs (graphics processing units, 312 TFLOP/s peak each at 40% model FLOPs utilisation, MFU), what data-pipeline throughput is required, and how do you handle parallelism, checkpointing and failure recovery?