The Architecture of Modern LLM Serving
Pre-training is a one-time cost; serving compounds every day. A first-principles walk through why decoding is memory-bound, where the 128 KB-per-token KV cache comes from, and how PagedAttention, continuous batching and RadixAttention cut GPU bills by 6–7x.
By Asmita Jha and Sandeep Upadhyay
In the lifecycle of deploying large language models, the spotlight often falls on pre-training. We hear about multi-million-dollar training clusters, weeks of compute runs, and astronomical frontier bills. But when transitioning from research to production, an economic reality emerges: pre-training is a one-time capital expenditure (CapEx), whereas model serving is a compounding operational expenditure (OpEx).
If an enterprise deploys an 8B parameter model to serve 1 million active users making 20 queries a day at 500 tokens each, that single system processes 10 billion tokens every single day. Handled naively, that deployment demands around 90 NVIDIA H100 GPUs, translating to an infrastructure bill topping $2,000,000 per year ($0.54 per million tokens).
By redesigning how GPUs handle memory and token generation, modern inference engines like vLLM and SGLang slash those requirements down to ~15 GPUs and roughly $330,000 per year ($0.09 per million tokens), a 6x to 7x reduction.
To understand how this optimization is possible, we need to examine the mechanics of LLM inference: the memory-bound nature of autoregression, the mathematics of the Key-Value (KV) cache, and the architectural innovations behind PagedAttention and RadixAttention.
1. The Core Bottleneck: Compute vs. Memory Bandwidth
To see why simple inference servers collapse under load, consider what happens when a model generates a token. We will use LLaMA 3.1 8B running on an NVIDIA H100 GPU as our baseline.
Model Weight Storage
In half-precision (FP16 or BF16), each parameter occupies 16 bits (2 bytes).
The Next-Token Generation Dilemma
Generating a token requires a forward pass across all 32 transformer layers. In basic transformer math, a forward pass requires approximately 2 floating-point operations (FLOPs) per parameter (one multiply, one accumulate):
Now, compare the GPU compute capability against its high-bandwidth memory (HBM) transfer speed:
- H100 Tensor Core Compute:
- H100 Memory Bandwidth (VRAM to SRAM):
- On-Chip SRAM Capacity:
Calculating the respective time components reveals a massive imbalance:
-
Pure Computation Time:
-
Memory Transfer Time: Because the 16 GB of weights cannot fit into the 30 MB on-chip SRAM, weights must be streamed slice-by-slice from VRAM across the memory bus:
The data movement takes 300 times longer than the mathematical calculations. When serving a single user, the GPU spends 99.7% of its time idling, waiting for weights to travel across the memory bus. Next-token generation is strictly memory-bandwidth bound.
2. Prefill vs. Decoding: A Tale of Two Phases
Every LLM generation request consists of two distinct stages:
The Prefill Phase (Prompt Processing)
- Execution: All prompt tokens are known in advance. The engine performs matrix-matrix multiplications () across the entire input sequence simultaneously in parallel.
- Hardware Profile: Compute-bound. The GPU cores are fully saturated, executing operations rapidly.
- Impact: Dictates the Time to First Token (TTFT).
The Decoding Phase (Autoregressive Generation)
- Execution: Generates text sequentially, one token at a time. Token cannot be computed until token is sampled, appended to the context, and passed through the model.
- Hardware Profile: Memory-bound. Each individual token step requires sweeping all 16 GB of weights through SRAM.
- Impact: Dictates Inter-Token Latency (ITL) and perceived output streaming speed.
3. Demystifying the KV Cache: The 128 KB/Token Equation
In the attention mechanism:
During decoding, queries () are only needed for the single active token being predicted. However, the attention context vector requires computing dot products against the Keys () and taking a weighted sum across the Values () of all past tokens. To avoid recalculating past keys and values at every single step, they are stored in GPU VRAM as the KV Cache.
How 128 KB per Token is Calculated
For LLaMA 3.1 8B, the architecture parameters are:
- Number of Layers ():
- KV Heads per Layer (): (utilizing Grouped Query Attention / GQA, where multiple query heads share a KV head)
- Head Dimension (): dimensions per vector
- Precision (): bytes (FP16/BF16)
The formula per token across all layers is:
Plugging in the parameters:
Concurrency Implications
-
For a typical conversation context of 2,500 tokens, one user’s KV cache requires:
-
On an 80 GB H100 GPU:
- 16 GB is reserved for model weights.
- ~10 GB is set aside for runtime overhead and buffers.
- ~54 GB remains for KV cache storage.
-
Under naive serving, 54 GB of VRAM caps physical concurrency at roughly 160 active streams before running out of memory.
4. Serving Optimization Frameworks: vLLM and SGLang
Modern serving optimization frameworks sit between raw hardware CUDA kernels and client-facing APIs. They solve the memory and scheduling limitations through three key innovations:
Pillar 1: PagedAttention (Solving Memory Fragmentation)
In naive setups, servers must allocate contiguous blocks of VRAM based on a model’s maximum possible sequence length. Because response lengths are inherently unpredictable, 40% to 50%+ of GPU VRAM is wasted on unused allocations and memory fragmentation.
PagedAttention applies the principles of OS virtual memory to the KV cache:
- It breaks each sequence’s KV cache into non-contiguous physical memory blocks called pages (e.g., 16 tokens per block, occupying ).
- Physical pages are allocated dynamically as tokens are generated.
- Only the active 16-token page can be partially empty at any given time.
- Result: GPU memory utilization surges to 96%, allowing concurrent user batch sizes to scale from 16 up to 128–256 on a single card.
max_len.Pillar 2: Continuous Batching (Iteration-Level Scheduling)
Traditional static batching waits for the slowest request in a batch to complete before accepting new prompts.
- Continuous batching operates at the token iteration level.
- As soon as a request emits an end-of-sequence token (
<EOS>), its memory pages are instantly reclaimed, and a new request’s prefill or decode iteration is injected into the next forward pass. - This keeps GPU compute cores consistently saturated.
<EOS>, so the same 12 passes complete 10 requests instead of 8, with no idle slots.Pillar 3: RadixAttention & Prefix Caching (SGLang)
In agentic workflows, multi-turn chats, or coding evaluations, requests frequently share identical prompt structures: long system instructions, few-shot examples, or prior conversational context.
- Rather than discarding prompt states and recalculating identical Key-Value vectors in the prefill phase, SGLang retains KV cache segments in a Radix tree.
- New requests match incoming token prefixes against the tree and reuse pre-computed KV tensors directly from VRAM, bypassing redundant prefill compute entirely.
5. Summary: Naive Serving vs. Optimized Engine Economics
| Dimension | Naive Deployment | vLLM / SGLang Optimized |
|---|---|---|
| KV Cache Allocation | Contiguous (40%–50% fragmentation waste) | Paged non-contiguous blocks (96% utilization) |
| Batching Mechanism | Static request-level batching | Dynamic continuous (iteration-level) batching |
| System Prompt Handling | Recomputed on every prompt | Retained via Prefix Caching / RadixAttention |
| Concurrent Batch Size | 16 users | 128 to 256 users |
| Single H100 Throughput | ~1,300 tokens/sec | ~8,000 tokens/sec |
| H100s Needed for 10B Tokens/Day | ~90 GPUs | ~15 GPUs |
| Cost per Million Tokens | $0.54 | $0.09 |
| Annual GPU Cloud Bill | ~$2,000,000 / year | ~$330,000 / year |
Conclusion
Understanding LLM serving requires recognizing that the primary challenge shifts once training concludes. In production, serving is fundamentally constrained by memory bandwidth during decoding and memory capacity within the KV cache.
By restructuring how memory is indexed (PagedAttention), how iterations are scheduled (Continuous Batching), and how shared state is preserved (RadixAttention), engines like vLLM and SGLang convert idle memory transfer cycles into high-density throughput, making enterprise-scale LLM deployment economically sustainable.
- #llm
- #inference
- #vllm
- #sglang
- #gpu
- #kv-cache
Written by