Pranit PatilMumbai IST
All notes

LLMOps · Updated 28 Nov 2026 · Study notes, in progress

Serving a small LLM: quantization, the KV cache and an API

Study notes on fitting a model in GPU memory, why the KV cache matters and what to measure.

The pipeline

  1. Pick a model

    small, open weights

  2. Quantize

    fewer bits per weight

  3. Load on a GPU

    weights + KV cache must fit

  4. Inference server

    batching, KV cache

  5. HTTP API

    streamed tokens

Fig 1 The five pieces of serving a model. Most of the engineering is in the middle two: fitting the model in GPU memory and keeping the GPU busy.

Memory is the first constraint

A model's weights take roughly parameters × bytes per parameter. For an 8 billion parameter model like Llama 3.1 8B, that is about 16 GB at 16-bit precision, before anything else is loaded.

  • FP16 / BF16 (2 bytes)16 GB
  • INT8 (1 byte)8 GB
  • 4-bit (half a byte)4 GB
Fig 2 Approximate weight memory for an 8B model at each precision. 4-bit formats also store a scale for each small group of weights, so real files are a bit larger than this.

What quantization does

Quantization stores each weight in fewer bits. Weights are split into small groups, each group gets a scale, and each weight becomes a small integer that is multiplied back by the scale when it is used. Methods such as GPTQ and AWQ choose those values carefully so the model's outputs change as little as possible, and GGUF is the file format llama.cpp uses for quantized models.

The KV cache

A model writes one token at a time, and each new token attends to every token before it. Inside attention, every token produces a key and a value at every layer. Those do not change once computed, so the server keeps them: that store is the KV cache. Without it, each new token would recompute keys and values for the entire conversation.

layerThecatsatonthe
1K VK VK VK VK V
2K VK VK VK VK V
3K VK VK VK VK V
4K VK VK VK VK V
cached, read again for every new tokencomputed once for the new token, then cached
Fig 3 The newest token, “the”, computes its own key and value at each layer and reads all the earlier ones from the cache.

The cache grows with every token, for every request in flight. Its size per token is 2 × layers × KV heads × head size × bytes. For Llama 3.1 8B that is 2 × 32 × 8 × 128 × 2 bytes, which is 128 KB per token. A single 8,000-token conversation needs about 1 GB.

16-bit

  • Weights16 GB
  • KV cache, about 8 long chats8 GB

4-bit

  • Weights5 GB
  • KV cache, about 19 long chats19 GB
Fig 4 A 24 GB GPU, roughly, before activations and runtime overhead. Shrinking the weights is mostly about leaving room for more conversations at once.

Managing the cache

Reserving one long block of memory per request wastes most of it, because you do not know in advance how long a reply will be. vLLM's PagedAttention borrows the idea of memory pages from operating systems: the cache is split into small fixed-size blocks that are handed out as a sequence grows and freed when it ends. That is what lets one GPU hold many conversations at once.

The other half is batching. Continuous batching adds new requests to the running batch as soon as others finish, instead of waiting for a whole batch to complete, so the GPU stays busy.

The API and what to measure

Servers like vLLM expose an OpenAI-compatible HTTP API, so existing clients work unchanged, and they stream tokens as they are generated. The numbers I plan to track:

Next noteWhy a connection pool was the biggest win on a 1 GB serverWhere a request's time actually went, and why a bigger pool made things slower