# Inference and Serving

> Decoding and sampling, the KV cache, quantization, inference serving, continuous batching and speculative decoding: how to run LLMs fast and cheaply, and how to see the trade-offs between latency, throughput and memory.

- URL: https://dmxsf.com/en/wiki/inference/
- [DMXSF](https://dmxsf.com/en/about/)
- Updated: 2026-11-18

## After this chapter you will be able to

- Say which part of the distribution temperature, top-k and top-p each change, and choose decoding parameters for different tasks.
- Compute the KV-cache memory of a model and explain why it decides how many requests can run at once.
- Compare INT8, INT4, GPTQ and AWQ, and read where the error comes from in a quantization experiment.
- Describe a serving system with TTFT, TPOT and throughput, and explain how continuous batching and paged attention raise hardware utilization.
- Explain why speculative decoding is lossless and what the speedup depends on.

If model endpoints are new to you, run [Your first inference service](https://dmxsf.com/en/wiki/inference/first-inference-service/) to follow one request on your own machine.

Train once, run inference millions of times. In real businesses the cost of inference is often far higher than the cost of training, so the content of this chapter largely decides whether an LLM application can be profitable.

The chapter follows the path of a request: first what the decoding step actually does, then the cache that makes it faster, then quantization that makes the model smaller, and finally the serving systems and speculative decoding that put many requests onto the same hardware. Between cache and serving stands [prefix caching and PD disaggregation](https://dmxsf.com/en/wiki/inference/prefix-caching/): the former makes repeated compute across requests happen only once, the latter gives the two differently-shaped stages what each needs. The roofline and bandwidth ideas from [Compute and Systems](https://dmxsf.com/en/wiki/systems/) are the key to every optimization here: decoding is bandwidth-bound, so almost every optimization reduces the bytes that must be moved for each token.

Related paper explainers: [vLLM and PagedAttention](https://dmxsf.com/en/blog/vllm-pagedattention/), [FlashAttention](https://dmxsf.com/en/blog/flashattention-fast-and-memory-efficient-exact-attention/) and [Speculative Decoding](https://dmxsf.com/en/blog/speculative-decoding-fast-inference-from-transformers/).

[Chapter labs on GitHub](https://github.com/dmxsf/dmxsf/tree/main/labs/inference)
