Sub topic 16
Serving-side techniques for self-hosted models
This chapter assumes you have read the earlier chapters on latency anatomy, prefill versus decoding, and model selection. It also assumes basic Python and a general idea of what a GPU does. If you call a hosted API, the provider applies these techniques for you. They matter when you run the model yourself.
Why serving matters
Self-hosting moves the cost from per-token prices to GPU time. A GPU costs the same per hour whether it is busy or idle. Your goal is to push more tokens through the same hardware without slowing individual users too much.
Decoding is the bottleneck. Each new token needs the GPU to read the model weights from memory, and this is limited by memory bandwidth, not by arithmetic. Most serving techniques raise the number of useful tokens produced per weight read.
Continuous batching
Static batching waits for a group of requests and runs them together until the longest one finishes. Short requests then sit idle while they wait.
Continuous batching schedules work at each decoding step. When a request finishes, a waiting request takes its slot immediately. Throughput rises because the GPU stays full. Engines such as vLLM and TGI do this by default.
KV cache and paged memory
During decoding, the model stores the attention keys and values of earlier tokens, called the KV cache, so it does not recompute them. The cache grows with sequence length and with the number of concurrent requests. It often limits how many requests fit on one GPU.
PagedAttention, used by vLLM, stores the cache in small fixed-size blocks instead of one large contiguous region. This reduces wasted memory and lets more requests run at once. Many engines also support prefix caching. Requests that share a prompt prefix reuse the same cached blocks, which is the self-hosted version of provider prompt caching.
Quantization
Quantization stores weights in fewer bits, for example 8-bit or 4-bit instead of 16-bit. The model uses less memory and reads fewer bytes per token, so decoding is faster and a larger model may fit on one GPU. The cost is a possible quality drop, which varies by model and method.
Always test a quantized model on your evaluation set before switching. A change that looks harmless on average can hurt specific tasks.
Speculative decoding
A small draft model proposes several tokens. The large model then checks them in one pass and accepts the ones it would have produced itself. When the draft is often right, you get several tokens per large-model pass. The output matches what the large model would have generated. The gain depends on how well the draft model predicts the large one, and it shrinks when the GPU is already saturated with large batches.
Trade-offs
| Technique | Main benefit | Main risk |
|---|---|---|
| Continuous batching | Higher throughput | Higher latency per request under heavy load |
| Paged KV cache and prefix caching | More concurrent requests | Little gain when prompts share no prefix |
| Quantization | Less memory, faster decoding | Quality loss |
| Speculative decoding | Lower latency | Gain depends on draft accuracy |
Measuring the effect
Use the timing wrapper from the measurement chapter against your own server. Compare time to first token and total time before and after each change, one change at a time.
import time
def time_stream(chunks):
"""Measure time to first chunk and total time for any iterable of streamed chunks."""
start = time.perf_counter()
first = None
count = 0
for _ in chunks:
if first is None:
first = time.perf_counter() - start
count += 1
total = time.perf_counter() - start
return {"ttft_s": first, "total_s": total, "chunks": count}Exercise
Your self-hosted server runs out of GPU memory when many users connect at once, even though the model weights fit easily. Which part of the system is most likely using the memory, and name two techniques from this chapter that help.
Show answer
The KV cache grows with each concurrent request and sequence length. Paged KV cache management (such as PagedAttention) reduces wasted memory, and quantization frees memory by shrinking the weights, leaving more room for the cache. Prefix caching also helps when requests share a prompt prefix.
Exercise
A quantized model doubles your throughput. What must you do before deploying it?
Show answer
Run it on your quality evaluation set and compare the results with the original model, because quantization can reduce quality on some tasks.