The pipeline
Pick a model
small, open weights
Quantize
fewer bits per weight
Load on a GPU
weights + KV cache must fit
Inference server
batching, KV cache
HTTP API
streamed tokens
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
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.
- Gain: a quarter of the memory at 4 bits, and often faster generation, because reading weights from memory is usually the bottleneck.
- Cost: some loss of quality, which grows as the bits shrink. The thing to measure is how much, on my own prompts.
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.
| layer | The | cat | sat | on | the |
|---|---|---|---|---|---|
| 1 | K V | K V | K V | K V | K V |
| 2 | K V | K V | K V | K V | K V |
| 3 | K V | K V | K V | K V | K V |
| 4 | K V | K V | K V | K V | K V |
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
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:
- Time to first token: mostly the prefill, where the whole prompt is processed in one parallel pass.
- Tokens per second, per request and in total: the decode, one token at a time, limited by memory bandwidth.
- GPU memory use and how many requests run at once before latency climbs.
- Cost per million tokens on the GPU I rent.