Memory Budgeting

Topics Covered

Where GPU Memory Goes

The four main consumers

Weights

The KV cache

Activations and workspace

Runtime overheads

Sampling and output buffers

Memory on the CPU side

A full budget for one GPU

Why fixed costs matter more on small budgets

Seeing memory use in practice

Memory is the resource that most often limits an LLM deployment. It decides whether a model can be served at all on a given GPU, how many requests can run at once, how long their contexts can be, and therefore what each token costs. This lesson builds a complete memory budget for a serving replica: weights, KV cache, activations and workspace, engine overheads, and the headroom you need to stay stable. It then shows how to turn that budget into a maximum batch size, how engines manage memory at runtime and what happens when memory runs out. This first section lists everything that occupies GPU memory during serving.

The four main consumers

During inference, GPU memory holds four kinds of data. The weights of the model are loaded once and stay resident. The KV cache holds keys and values for every token of every active sequence and grows and shrinks as requests come and go. Activations and temporary buffers hold intermediate results during each forward pass and are reused from step to step. And the runtime itself, the CUDA context, communication library buffers, CUDA graphs and allocator overheads, takes a fixed amount that is easy to forget. A budget that accounts for all four predicts how a replica will behave; one that counts only weights produces deployments that run out of memory under load.

Weights

Weight memory is the number of parameters times the bytes per parameter. An 8B model takes about 16 gigabytes in 16 bit precision, 8 gigabytes in 8 bit and about 4 to 5 gigabytes in 4 bit, depending on the overhead of scale factors and which layers stay in higher precision. A 70B model takes 140, 70 and about 38 gigabytes. With tensor parallelism, each GPU holds a share: a 70B model in 16 bit split across four GPUs takes about 35 gigabytes per GPU. Embedding and output layers are sometimes replicated rather than split, adding a few gigabytes per GPU for models with large vocabularies.

For mixture of experts models, all experts must be resident even though only a few are used per token. A model with 671 billion total parameters needs about 671 gigabytes in 8 bit, even though its active parameters per token are only 37 billion. This is why mixture of experts models are cheap in compute per token but expensive in memory, and why they are usually served across many GPUs with expert parallelism.

The KV cache

The KV cache takes whatever memory remains after the other consumers, and its size decides concurrency. Per token, it takes 2 times layers times key value heads times head dimension times bytes per element. For a model with 32 layers, 8 key value heads and head dimension 128 in 16 bit, that is 128 kibibytes per token; a 70B model with 80 layers and the same head layout takes 320 kibibytes per token. A single 32,000 token conversation on the 70B model takes about 10 gigabytes. Unlike weights, the cache scales with traffic, which is why it dominates memory planning for busy services with long contexts.

Weights load first, then the runtime and workspace take their share. Whatever is left becomes the KV cache, which decides how many requests can run.

Activations and workspace

During a forward pass, intermediate tensors hold the outputs of attention, the feed forward hidden layer and other operations before they are combined into the residual stream. For decode with a moderate batch, these are small, megabytes to a few hundred megabytes. For prefill of long prompts, they can be large: the feed forward hidden layer for a chunk of 8,192 tokens of a model with hidden size 8,192 and intermediate size 28,672 in 16 bit takes about 470 megabytes per layer, though it is freed after each layer. Engines profile the peak activation memory for the largest batch they will run and reserve it. Chunked prefill, covered in lesson thirteen, caps this peak by limiting the tokens processed in one step.

Runtime overheads

The CUDA context and libraries take several hundred megabytes per GPU before any model loads. NCCL communication buffers for tensor parallelism take more. CUDA graphs, which engines capture for many batch sizes to cut launch overhead, each hold their own memory for intermediate buffers, adding up to a gigabyte or more. Memory allocators fragment over time, leaving free memory in pieces too small to use. Together these overheads commonly take 2 to 5 gigabytes per GPU, which matters most on smaller GPUs and when the budget is tight.

Sampling and output buffers

Each decode step produces logits over the full vocabulary for every sequence in the batch. With a vocabulary of 128,000 tokens in 32 bit and a batch of 256, that is about 130 megabytes per step, small but not negligible. Features such as returning log probabilities, beam search or parallel sampling multiply these buffers. Structured output with grammar constraints keeps additional masks. These are minor next to weights and cache but belong in a careful budget.

Memory on the CPU side

GPU memory is not the only memory a serving system uses. Host memory holds the tokenizer, request queues, the weights while they load, and optionally a CPU tier of the KV cache for offloaded or swapped sequences. Systems that offload prefix caches to CPU memory or local SSD can keep far more reusable context than fits on the GPU, at the cost of transfer time when a cached prefix is needed. Budgeting host memory matters less often but can surprise teams that run many replicas per server.

A full budget for one GPU

Put the pieces together for an 8B model in bfloat16 on one 80 gigabyte H100. The weights take about 16 gigabytes. The CUDA context, libraries and allocator reserve take about 1 gigabyte, CUDA graphs captured for a range of batch sizes take about 1 more, and the activation reservation for a maximum of 8,192 batched tokens takes about 2. With a utilization setting of 0.9, the engine may use 72 gigabytes, so the cache gets 72 minus 20, or about 52 gigabytes. The remaining 8 gigabytes outside the engine budget absorb fragmentation and anything else on the GPU. Of the 80 gigabytes on the card, roughly two thirds end up as cache, and that two thirds sets everything about concurrency.

Now repeat the exercise for the same model on a 24 gigabyte GPU. The weights take 16, overheads and activations about 3, and 0.9 of 24 is 21.6, leaving under 3 gigabytes of cache, about 20,000 tokens. The model runs, but only a handful of conversations fit at once, and cost per token is several times higher than on the larger GPU. Quantizing the weights to 4 bit frees about 11 gigabytes, which quadruples the cache. On small GPUs, quantization is often the difference between a usable deployment and a demo.

Why fixed costs matter more on small budgets

Fixed overheads are the same in absolute terms whatever the GPU, so they take a larger share of small budgets. Three gigabytes of runtime overhead is under 4 percent of an 80 gigabyte card and over 12 percent of a 24 gigabyte one. The same is true when a model is split across many GPUs: each GPU pays the runtime overhead and, with tensor parallelism, may also replicate some layers, so eight GPUs do not give eight times the cache of one. When comparing layouts, count overheads per GPU rather than per replica.

Seeing memory use in practice

The nvidia-smi tool reports memory used per GPU and per process, but with an engine running it typically shows the GPU as nearly full, because the engine allocates its whole cache pool at startup whether or not requests are using it. That number therefore says little about load. The useful view comes from the engine itself: its metrics report how many cache blocks are in use by running sequences, how many hold reusable prefixes and how many are free. Learning to read memory through engine metrics rather than device totals avoids a common false alarm, where a new replica looks full before it has served a single request.

The cache is the residual

Weights, runtime overhead and workspace are roughly fixed for a deployment. The KV cache gets everything that is left. Every gigabyte saved elsewhere, through quantization or a smaller workspace, becomes cache, and cache becomes concurrency and throughput.