Skip to content

LLM Inference Serving Architecture: Batching, KV Cache and GPU Autoscaling

How LLM serving works from request to last token: prefill and decode, KV cache math, continuous batching, routing, autoscaling and the newer techniques.

· Founder & developer

Published

Users and agents call an API gateway and an inference router, which sends requests to two running vLLM pods in a GPU node pool; a third pod is pending. Prometheus scrapes the pods and feeds an HPA via KEDA that adds replicas. Model weights load from storage at startup.

You put Llama 3.1 8B behind a small Python web server on an H100, and the demo works. Then fifty people use it at once. Requests queue, the first token takes seconds to appear, and the GPU dashboard shows 100% busy the whole time. Another GPU would help less than you expect, because the server barely uses the one it has.

An LLM request generates its answer one token at a time and holds GPU memory that grows with every token. The serving stack decides how many requests share a GPU, which replica gets each one, and when to add replicas.

This post walks through that stack, from KV cache math to batching, routing and autoscaling, with a diagram of it on Kubernetes.

Prefill and decode

An LLM answers in two phases with very different costs:

  • Prefill runs the whole prompt in one pass, stores the attention keys and values of every prompt token, and produces the first output token. It is limited by GPU compute and grows with prompt length.
  • Decode generates the rest, one token per step. Each step reads all the weights from GPU memory, so it is limited by memory bandwidth.

Llama 3.1 8B in BF16 is about 16 GB of weights, and an H100 SXM reads memory at 3.35 TB/s, so one decode step takes at least 4.8 ms. One request at a time caps you near 200 tokens per second with most of the compute idle. Decode 64 sequences in the same step and the weights are still read once. Batching is the main lever.

The latency metrics

Track each phase separately:

  • Time to first token (TTFT): queueing plus prefill.
  • Inter-token latency (ITL), or time per output token (TPOT) averaged per request: the streaming speed.
  • End-to-end latency: TTFT + TPOT × (output tokens − 1). A 500-token answer with a 0.3 s TTFT and a 25 ms TPOT takes about 12.8 seconds.

Bigger batches raise throughput and slow each stream, so set TTFT and TPOT targets per use case and measure goodput, the requests per second that meet them. vLLM exports TTFT and ITL as Prometheus histograms; the observability architecture diagram shows where they go.

KV cache memory math

The KV cache keeps the keys and values of every token a sequence has seen. Its size per token depends only on the model:

bytes per token = 2 (K and V) × layers × KV heads × head dimension × bytes per value

Llama 3.1 8B has 32 layers, 8 KV heads (grouped-query attention, GQA) and a head dimension of 128. In BF16 that is 2 × 32 × 8 × 128 × 2 = 131,072 bytes, or 128 KiB per token.

Model (BF16) Layers KV heads Per token 8,192 tokens 128K tokens
Llama 3.1 8B 32 8 128 KiB 1 GiB 16 GiB
Llama 3.1 70B 80 8 320 KiB 2.5 GiB 40 GiB
8B without GQA (32 KV heads) 32 32 512 KiB 4 GiB 64 GiB

On one 80 GB H100, vLLM 0.33 uses up to 92% of GPU memory by default (--gpu-memory-utilization), about 73 GB. Weights take about 16 GB and activations a few more, leaving roughly 50 GB of KV cache: about 400,000 tokens, or 50 requests that each fill an 8K context. vLLM logs the real KV cache size and maximum concurrency at startup.

Set --max-model-len to what your product needs. When the KV cache is full, new requests wait, however idle the GPU looks.

Continuous batching and PagedAttention

Static batching runs a group of requests until the longest finishes, so short answers wait for long ones. Orca (OSDI 2022) introduced iteration-level scheduling, now called continuous batching: the scheduler revisits the batch at every decode step, admits waiting requests as soon as memory allows and releases finished ones at once.

Earlier servers reserved KV space for each request's maximum length and wasted much of it. The PagedAttention paper (SOSP 2023), which introduced vLLM, borrows paging from operating systems: the KV cache is split into fixed blocks (16 tokens by default in vLLM) that a block table maps to each sequence. Waste drops to near zero, requests can share a common prefix, and the paper reports 2 to 4 times the throughput of FasterTransformer and Orca at the same latency.

Chunked prefill splits long prompts so they share steps with ongoing decodes instead of stalling every stream. When the cache is full, vLLM preempts a request and recomputes it later; watch vllm:num_preemptions.

Serving engines compared

Engine Maintained by Strengths Status, November 2026
vLLM vLLM project PagedAttention, prefix caching on by default, broad hardware support Active; 0.33.0 released 2 November
SGLang LMSYS, a non-profit RadixAttention prefix reuse, agentic workloads Active; 0.5.23 released 30 October
TensorRT-LLM NVIDIA, Apache 2.0 Custom kernels, speculative decoding, disaggregated serving Active; NVIDIA GPUs only
Hugging Face TGI Hugging Face Simple Docker deployment Maintenance mode; Hugging Face recommends vLLM or SGLang

Routing: least loaded and prefix aware

A classic load balancer assumes requests cost about the same. LLM requests do not: one carries a 50-token question, the next a 50,000-token document. Inference routers score replicas on three signals:

  • Load: fewest waiting requests and most free KV cache.
  • Prefix affinity: requests that share a prefix (a system prompt, a conversation history, the steps of an AI agent's tool-calling loop, the same RAG documents) go to the replica that already caches those KV blocks, which skips that part of the prefill. vLLM reports vllm:prefix_cache_hits and vllm:prefix_cache_queries.
  • Adapters: prefer a replica that already has the request's LoRA adapter loaded.

Affinity and balance conflict: one popular system prompt can pin most traffic on one replica, so good routers weigh both.

On Kubernetes, the Gateway API Inference Extension standardizes this: an InferencePool groups the model server pods, and the gateway asks an endpoint picker which pod gets each request. At the time of writing (November 2026), the project is GA and its full endpoint picker is developed in llm-d. An LLM gateway in front still owns keys, quotas and provider fallback.

Autoscaling and cold starts

CPU utilization says nothing about an inference server, and GPU utilization little more: nvidia-smi reports the share of time in which any kernel ran, so a batch of 2 and a batch of 200 can both read 100%. Scale on saturation instead:

  • vllm:num_requests_waiting: requests that keep waiting mean the replica is full.
  • vllm:kv_cache_usage_perc (1 means 100%): sustained values above about 0.8 leave little headroom.
  • p95 TTFT against your target.

Feed them to the HPA through KEDA's Prometheus scaler or the Prometheus Adapter, and scale down slowly, since a removed replica takes its warm prefix cache with it. The Kubernetes autoscaling diagram shows how these loops interact.

Cold starts set the floor on reaction time: a new replica may wait for a GPU node, pull an image of several gigabytes, then load the weights and warm up. At 1 GB/s, 16 GB of weights take 16 seconds; the roughly 140 GB of Llama 3.1 70B take over two minutes. Keep warm minimum replicas, cache weights on node-local NVMe, pre-pull images, give the startup probe a generous timeout, and let streams finish on scale-down.

Quantization and newer techniques

Technique Gain Cost
FP8 or INT4 weights (AWQ, GPTQ) Half or a quarter of the weight memory, faster decode FP8 needs hardware support such as H100; INT4 risks more accuracy
FP8 KV cache (--kv-cache-dtype fp8) Twice the tokens per GB Possible accuracy loss; test long prompts
Speculative decoding A draft proposes tokens, the model verifies them in one pass: lower ITL, same outputs Gains shrink at large batch sizes
Disaggregated prefill and decode Separate prefill and decode replicas, so TTFT and ITL are tuned separately KV transfer over a fast network; no throughput gain by itself

Together, FP8 weights and an FP8 KV cache take the 8B replica from roughly 400,000 to about 900,000 cached tokens. The speculative decoding paper showed 2 to 3 times faster T5-XXL inference with identical outputs; gains are largest at low concurrency, where decode leaves compute idle. vLLM 0.33 still marks disaggregated prefill as experimental, and its docs say it does not improve throughput.

The LLM serving stack diagram

Users and agents call an API gateway and an inference router, which sends requests to two running vLLM pods in a GPU node pool; a third pod is pending. Prometheus scrapes the pods and feeds an HPA via KEDA that adds replicas. Model weights load from storage at startup.

17 components · 8 connections

Open in Archboard
Diagram contents
  • GPU node pool · vLLM · 1× H100 80 GB per pod
  • Users and agents
  • API gateway
  • Inference router
  • vLLM replica 1
  • vLLM replica 2
  • vLLM replica 3
  • HPA via KEDA
  • Prometheus
  • Llama 3.1 8B weights
  • HTTPS, SSE
  • within quota
  • cached prefix
  • least loaded
  • scrape /metrics
  • queue depth, KV use
  • replicas 2 → 3
  • weights at startup
An LLM serving stack on Kubernetes. An API gateway and an inference router pick a vLLM pod by cached prefix and load; each pod batches requests over a paged KV cache, and the autoscaler adds replicas on queue depth and KV cache use, not GPU utilization.

How to read it:

  • Solid blue edges are the request path: clients stream over HTTPS with server-sent events, the gateway checks quotas, and the router picks a pod by cached prefix or by load.
  • The GPU node pool runs one vLLM pod per H100. Replica 3 is Pending and gets no traffic until its node and weights are ready.
  • Dashed amber edges are the scaling loop: Prometheus scrapes queue depth and KV cache use, and KEDA drives the HPA from 2 to 3 replicas.
  • The dashed violet edge loads the weights at startup, and the Markdown block holds the sizing math.

Drawing LLM serving diagrams clearly

  • Label replica groups with model, precision, GPU and KV budget, such as "Llama 3.1 8B, BF16, 1× H100 80 GB, 400K tokens", so reviewers can check the math.
  • Show the routing decision and its inputs, not just an arrow into a pool.
  • Separate three flows: requests solid, scaling metrics and weight loading dashed.
  • Leave out kernels, tokenizers and most pods.
  • Avoid one box called "LLM", an autoscaler wired to CPU, and missing weight sources.

The MLOps architecture diagram covers how models reach the registry.

Build it in Archboard

  1. Sign in with Google or GitHub and create a diagram.
  2. Add a Person from the C4 Model pack, an API Gateway from the Cloud pack and an Endpoint from the ML pack named Inference router, with the route /v1/chat/completions. Connect them left to right.
  3. Add a Frame from the Core pack for the GPU node pool with three Pod nodes from the Kubernetes pack inside. Set the third pod's phase to Pending.
  4. Connect the router to the first two pods (cached prefix, least loaded) and add a Core Text note beside each pod.
  5. Below the pool, add Metrics from the DevOps pack, an HPA from the Kubernetes pack (2 to 8 replicas) and a Model from the ML pack for the weights, joined by dashed edges.
  6. Add a Markdown block from the Core pack with the sizing numbers.

If your vLLM Deployment and HPA exist as manifests, the Kubernetes YAML to diagram tool turns them into a first draft.

The example uses 11 diagram nodes, within the Free plan.

FAQ

What is the difference between prefill and decode? Prefill processes the prompt in one compute-bound pass and produces the first token. Decode then generates one token per step, rereading the weights each time, so memory bandwidth limits it.

How much GPU memory does the KV cache need? Per token: 2 × layers × KV heads × head dimension × bytes per value. That is 128 KiB for Llama 3.1 8B in BF16, or 1 GiB per 8,192 tokens.

What is continuous batching? Scheduling that updates the batch at every decode step: finished requests leave and waiting ones join at once, so short requests never wait for long ones.

Should I use vLLM, SGLang or TensorRT-LLM? vLLM is a common default with broad hardware support, SGLang is strong at prefix reuse, and TensorRT-LLM targets NVIDIA GPUs. Benchmark with your own traffic.

How do you autoscale LLM inference on Kubernetes? Scale with KEDA or the HPA on the engine's queue depth and KV cache use, not on CPU or GPU utilization, and keep warm replicas, because cold starts can take minutes.

Start free — one seat, no credit card.

Related posts

← Back to blog

Open a canvas in a minute

Sign in with Google or GitHub. Free for a single seat. Upgrade when the team joins.