How is LLM inference optimised for serving at scale?

Learning path, stage 5 of 5: Shipping to production · question 7 of 10

Serving is limited by GPU memory and memory bandwidth more than by raw compute. The main techniques are: continuous batching to keep the GPU busy, PagedAttention for efficient KV-cache memory, quantisation to shrink weights, FlashAttention for faster attention kernels, speculative decoding to emit several tokens per expensive forward pass, prefix caching, and parallelism (tensor or pipeline) for models too big for one GPU.

The techniques

  • Continuous (in-flight) batching: add and remove sequences from the batch at every decode step, instead of waiting for the whole batch to finish.
  • PagedAttention (vLLM): stores the KV cache in fixed-size blocks, which cuts fragmentation and allows larger batches and prefix sharing.
  • Quantisation: INT8 or 4-bit weights (GPTQ, AWQ), sometimes a quantised KV cache. Less memory and faster, with a small quality cost.
  • FlashAttention: an IO-aware attention kernel that avoids building the full n×n matrix in slow memory.
  • Speculative decoding: a small draft model proposes k tokens, the big model checks them in one pass, and the accepted tokens are output. The result is identical to what the big model would have produced, just faster.
  • Prefix caching: reuse the KV cache for shared prompt prefixes across requests.
  • Separating prefill and decode onto different hardware pools.

Metrics

Time to first token (TTFT), time per output token (TPOT), throughput (tokens per second per GPU), and cost per million tokens. These trade off: larger batches raise throughput but also latency.

Sources

Code example

tsx
1// Speculative decoding, greedy version: draft k tokens cheaply, verify in one big pass.
2declare function draftNext(ctx: number[]): number; // small, fast model
3declare function targetArgmaxAll(ctx: number[], k: number): number[]; // big model, ONE forward pass:
4// returns the big model's own greedy choice at each of the last k+1 positions
5
6export function speculativeStep(ctx: number[], k = 4): number[] {
7 // 1. draft k tokens with the small model
8 const draft: number[] = [];
9 for (let i = 0; i < k; i++) draft.push(draftNext([...ctx, ...draft]));
10
11 // 2. verify all k positions with a single big-model pass
12 const target = targetArgmaxAll([...ctx, ...draft], k);
13
14 // 3. accept the longest matching prefix, then take the big model's token at the first mismatch
15 const accepted: number[] = [];
16 for (let i = 0; i < k; i++) {
17 if (draft[i] === target[i]) accepted.push(draft[i]);
18 else { accepted.push(target[i]); return accepted; }
19 }
20 accepted.push(target[k]); // all matched: one bonus token
21 return accepted; // 1..k+1 tokens for one expensive pass
22}

To run it: follow the one-time setup guide, add npm i @anthropic-ai/sdk zod @opentelemetry/api vitest, save the code as a .ts file and run npx tsx file.ts. Anything marked declare is a part you supply.

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the AI Agents & LLMs set.

Open in study app