Skip to main content
← Home

The Inference Engine

Serving at scale - 11 chapters

Explore the infrastructure layer that makes LLMs fast and efficient: batching, KV caching, PagedAttention, FlashAttention, speculative decoding, and production serving.

01
Two very different phases

Prefill vs Decode

Inference has two phases. Prefill processes prompt tokens together; decode generates tokens autoregressively. Their bottlenecks depend on sequence length, batch size, model, and hardware: decode is often bandwidth-bound at small batches, while large prefills are compute-intensive.

In prefill, the model processes the prompt in one or more forward passes and builds the initial KV cache. In decode, each step computes the next token and reuses cached keys and values; the new query attends to the cached prefix. At low batch sizes, reading weights and KV state often limits decode throughput, while batching can improve weight reuse and shift the bottleneck. Some serving systems separate prefill and decode onto different worker pools, but transfer overhead and workload shape determine whether that helps. vLLM overview: vLLM online serving docs — disaggregated prefill (experimental): disaggregated prefill docs

Prompt length512 tokens
Prefill
All 512 tokens at once
GPU util
85%
Mem BW
30%
Illustrative time: 10ms
Often compute-bound
Decode
one by one
1 token per forward pass
GPU util
15%
Mem BW
90%
Per token: 25ms
Often bandwidth-bound

The GPU is a supercomputer that spends most of decode waiting for memory reads. This is why memory bandwidth (GB/s) matters more than FLOPS for token generation, and why batching multiple requests together helps - it amortizes the memory reads.

Interview Lab · What happens when a long prompt interrupts decoding?
1 / 11

Now try it yourself

forwardpass.dev

An interactive educational project visualizing how LLM inference, training, and deployment work - from raw text to generated response.

Further reading

  • "Attention Is All You Need" - Vaswani et al., 2017
  • "Language Models are Few-Shot Learners" - Brown et al., 2020
  • "The Illustrated Transformer" - Jay Alammar
  • "Neural Networks: Zero to Hero" - Andrej Karpathy

Built with

  • Next.js + TypeScript
  • Framer Motion
  • Tailwind CSS
  • js-tiktoken
Everything runs in your browser - no data is sent to any server.