Model serving
Running language models in production so they answer many requests quickly and cheaply.
Model serving is the work of running a language model in production so it answers many requests with low latency and reasonable cost. It covers loading weights onto accelerators, scheduling incoming requests, managing memory for each active conversation, and streaming tokens back. Specialized inference servers handle most of this, but their behavior shapes the latency and price of every product built on top.
Two numbers matter most to users. Time to first token is how long they wait before anything appears, and it is dominated by processing the prompt. Inter-token latency, or output speed, is how fast the rest streams in, and it is dominated by how quickly the hardware can read weights and cached state from memory. Long prompts hurt the first; large models and busy servers hurt the second.
Operators care about throughput: total tokens per second across all users on a machine. Throughput and per-user latency pull against each other. Batching more requests together uses the hardware better but can slow each one, so servers balance them with careful scheduling, memory management for attention state, and techniques such as prefix caching, which reuses work when many requests share the same opening text.
When you call a hosted model, these mechanics still show up in your bill and your user experience: cheaper cached input tokens, slower responses at peak times, and higher prices for long context. Keeping prompts stable at the front and short overall is the simplest serving optimization available to an application.
KV cache
Stored attention keys and values for tokens already processed, so generation does not recompute them.
The KV cache stores the attention keys and values for every token a model has already processed. When generating the next token, the model only computes keys and values for that one new token and reuses the stored ones for everything before it. Without the cache, each step would reprocess the whole sequence, and generation would get slower with every token.
The cost is memory. The cache grows with the number of layers, the attention width, the sequence length, and the number of concurrent requests, and for long contexts it can rival or exceed the size of the weights. Much of serving engineering is about managing it: allocating it in pages to avoid waste, sharing it across requests with a common prefix, compressing it, and using attention variants that store fewer keys and values.
Prefill and decode
The two phases of generation: processing the whole prompt in parallel, then producing output one token at a time.
Generation happens in two phases. Prefill processes the entire prompt in one parallel pass, computing attention state for every input token and filling the KV cache. It keeps the hardware's arithmetic units busy, so it is compute-bound, and its duration sets the time to first token. Long prompts mean long prefill.
Decode then produces output one token per step. Each step does relatively little arithmetic but must read all the model weights and the growing cache from memory, so it is bound by memory bandwidth, and its speed sets how fast text streams. Because the two phases stress hardware differently, some serving systems run them on separate machines, and schedulers interleave them so long prompts do not stall other users' streams.
Continuous batching
A serving technique where requests join and leave a batch at every generation step instead of waiting for each other.
Continuous batching, also called in-flight batching, schedules a serving batch one generation step at a time. With simple static batching, a group of requests starts together and the batch runs until the longest one finishes, leaving slots idle as shorter requests complete. With continuous batching, finished requests leave immediately and waiting requests take their place at the next step.
Because decode steps are limited by memory bandwidth, adding more requests to each step is nearly free up to a point, so keeping the batch full raises total throughput dramatically and reduces queueing. It is standard in modern inference servers, and it works hand in hand with careful KV cache management, since every active request needs its own cache space.