Topic 220 / Specialist
LLM serving and inference infrastructure
Chapter 13.12 · LLM Serving and Inference Infrastructure
What and why
The serving stage of the general ML lifecycle (→ 219) assumes a prediction is a single bounded computation — call the model, get an answer. Serving a large language model breaks that assumption: generation is autoregressive (the model produces one token at a time, each depending on every token produced so far), so a single request is really a sequence of many small, dependent computations, and the memory needed to track that sequence grows with how much has been generated. This is why LLM serving needs its own infrastructure patterns rather than reusing generic model serving directly. Builds on: → 219 (AI/ML system architecture — the serving stage, specialized here).
Diagram
request ─► tokenize ─► token 1 (update KV cache) ─► token 2 (update KV cache) ─► ...
│ │
└────── stream each token back to client as produced ──────►
How it works
- Autoregressive generation: each output token is produced by a forward pass that conditions on every previous token — prompt plus everything generated so far — meaning producing N tokens means N dependent forward passes, not one.
- KV-cache memory pressure: to avoid recomputing attention over the whole sequence at every new token, the model caches intermediate results per token generated so far; this cache grows with sequence length and with the number of concurrent requests being served, and is frequently the resource that runs out before raw compute does.
- Continuous batching: because requests arrive at different times and finish after different numbers of tokens, an efficient server doesn't wait to form a fixed batch — it adds new requests into an in-flight batch and removes finished ones continuously, keeping hardware busy instead of idle between batch boundaries.
- Streaming token responses: rather than waiting for the full sequence, each token is streamed to the client as soon as it's produced — why time-to-first-token (how long until the first token arrives) is tracked separately from tokens-per-second (how fast generation proceeds once started): a user perceives responsiveness from the first, completion speed from the second.
- Quantization represents model weights at lower numeric precision than they were trained in, trading a small amount of output quality for significantly less memory use and often faster inference — a lever available specifically because these models are large relative to typical serving hardware memory.
- Model routing sends different requests to different model sizes or instances — a smaller, cheaper model for simple requests, a larger one only when needed — because the cost per token of the largest model is usually markedly higher than a smaller one, and not every request needs the largest model's capability.
- Cost per token scales with output length (more tokens generated, more compute spent) and with context length (prompt size processed), unlike typical request-based serving where cost is roughly per request — capacity planning and pricing both have to account for this directly, not just requests-per-second.
Practical example
(illustrative). A request with a 200-token prompt and a 300-token expected response requires processing roughly 200 tokens of context before the first output token is produced (affecting time-to-first-token), then 300 more dependent forward passes to complete generation (affecting tokens-per-second and KV-cache growth). Doubling the number of concurrent requests being served roughly doubles the KV-cache memory needed at any instant, even though the compute per individual token stays the same — why KV-cache memory, not raw compute throughput, is frequently what limits how many concurrent requests one server can serve.
When to use it
- Use when: a product generates variable-length text from a large model and must manage latency, concurrency, and cost at the token level.
- Avoid when: a smaller, task-specific model (classification, extraction) does the job without autoregressive generation's overhead — if the output isn't free-form generated text, this serving pattern may be unnecessary machinery.
- Failure scenarios: KV-cache memory exhausting available hardware memory under concurrent load, forcing request rejection or risky eviction; measuring only average latency and missing that time-to-first-token, not tokens-per-second, is what a user actually experiences as fast or slow; routing every request to the largest, most expensive model by default when a smaller model would serve most of them.
- Simpler alternative: a smaller, fine-tuned task-specific model served with the general ML serving pattern (→ 219) when free-form generation isn't actually required.
Practice
(Numeric kind — drills; 15–20 min, no lab services). Given 3 illustrative requests with stated prompt and expected-output token counts, and a stated tokens-per-second generation rate, compute each request's expected time-to-first-token and total completion time. Then compute how total KV-cache memory demand changes if all 3 run concurrently versus sequentially. Done when you can state, in one sentence, which resource (compute or KV-cache memory) is the binding constraint in the concurrent case.
Key takeaway
LLM serving is bounded by sequential, dependent generation and growing KV-cache memory, not just raw compute — continuous batching, streaming, and routing exist to manage that. Done when you can: explain why KV-cache memory, not compute, is often the binding constraint. Used next in → 221 (RAG's generation step runs on this serving infrastructure).
References
None — this topic makes no external claims.
Source directory