## The inference stack

*Production and serving*

*Last edited: 2 November 2026*

Before the model answers a chat message, the request reaches an inference server, often through a gateway, and the server uses libraries and a driver to run the model on the GPU. Each level has its own job and knows little about the others.

**In plain words:** A map of seven levels. At the top level you see the application and the API; deeper down, the same request appears as tokens, tables of numbers, hardware commands and individual multiplications.

When an answer arrives slowly or the server runs out of memory, the API alone doesn’t reveal the cause. Dividing the stack into levels shows which component to inspect.

The example on this page is a Kubernetes cluster running the vLLM inference server, PyTorch and NVIDIA H100 GPUs, with a model of 70 billion parameters and 80 layers.

*Interactive widget on the page: Go down the stack level by level. Tap an element to jump to its step.*

### Level 1. System

- **History.** The application sends the whole history and keeps the connection open until the final event. Between messages the history lives in the application, and an idle chat holds no assigned KV cache blocks. With a stateful API such as OpenAI’s `previous_response_id` the provider stores the history, and earlier input is still billed on every turn.
- **Response.** Text arrives in fragments as tokens are picked. When the application runs one of its tools, it sends the result in a new request that passes through all the levels again (see “The agent loop”).

### Level 2. Cluster

- **The request.** The gateway checks the key and limits, and the router picks one replica. A plain Kubernetes Service balances connections rather than requests and doesn’t look at the prompt. Each replica has its own queue, so requests can wait at one while another idles. A smarter router (llm-d, NVIDIA Dynamo, vLLM Router) tries to reach a replica that still holds the KV cache for the start of this prompt. It picks one in two ways: by cache state, comparing the prompt’s tokens, or by a session key, if the client sends one. It also weighs load. Both only steer the request: the replica itself checks whether its cache applies, by comparing tokens (level 3).
- **Why a replica has four GPUs.** The example model has 70 billion parameters, 140 GB of weights, and one H100 has 80 GB of memory. At 16 bits per parameter the weights fit on two H100s, but that leaves almost nothing for the KV cache. In this example three GPUs won’t work: each layer has 8 KV heads (level 4), and each GPU must get the same number of whole heads (level 7), so a replica has four. Quantisation or a GPU with more memory changes this (see “Quantisation”). Level 7 explains how one copy is split across GPUs. Three replicas are three independent copies, each with its own KV cache.
- **Placement.** Before requests arrive, Kubernetes places the pod on a node with free GPUs. The pod asks for `nvidia.com/gpu: 4`, and the cluster scheduler sees only a count. On the node the NVIDIA Device Plugin prefers well-linked GPUs but can’t guarantee them. A guarantee comes from the node’s design, or from a Dynamic Resource Allocation claim that selects devices by attributes.
- **Several nodes.** A model too large for one node runs as a group of pods forming one replica. LeaderWorkerSet manages the group, Ray can coordinate the workers, and gang scheduling, when enabled, starts the pods together or not at all, because without any one of them the model can’t compute a token.

### Level 3. Server

- **Start.** Before its first request, vLLM loads the weights, measures the memory used by working buffers and CUDA Graphs, and gives the rest of its GPU memory budget (92% in this example, set through `gpu_memory_utilization`) to the KV cache. In the example four H100s give 320 GB, and about 140 GB remain after weights and buffers.
- **Arrival.** The API process applies the chat template, tokenises the messages and puts the request in the replica’s queue.
- **Model pass.** A model pass is one run of all the layers for the whole batch, a group of conversations processed together. Before each pass the scheduler allocates the blocks running conversations need, then admits waiting requests while free blocks remain and neither limit has been reached (`max_num_seqs`, `max_num_batched_tokens`). The rest wait in the queue. A batch holds only requests being computed: open chats can far outnumber conversations in the batch, because an idle chat holds no blocks. The mechanism is in “Continuous batching”.
- **Prefill and decode.** Prefill computes the prompt’s KV cache, sometimes across several passes, and the last one allows the first output token to be picked. Decode processes the last picked token to pick the next. One batch can mix decode of running conversations with a prefill chunk of a new one. Such a pass takes much longer than decode alone, so a new long prompt can stall every stream in the batch, and `max_num_batched_tokens` caps the chunk. Large deployments can avoid this by running prefill and decode in separate GPU pools (see “Why the GPU is idle”).
- **Blocks.** The KV cache manager divides this memory into blocks of 16 tokens, 5.2 MB each in the example. It hands them to a conversation as it grows and keeps its block table, so one conversation’s blocks can sit anywhere in memory (PagedAttention). When generation finishes, the blocks are no longer assigned to the request.
- **Prefix cache.** In plain vLLM the blocks stay in GPU memory after the answer but no longer belong to the conversation. The next request carries the whole history, and vLLM finds its blocks by a hash of the tokens, without a conversation ID. vLLM skips prefill computation for matching full blocks, and the least recently used blocks are overwritten first. That’s why the next turn of a chat can be cheap (see “Prompt caching”). Nothing guarantees how long a block survives: a busy replica overwrites it, and without a smarter router the next turn may land on another replica and compute the prompt from scratch.
- **Offloading the cache.** Systems with caches lasting minutes or hours can move blocks from GPU memory to RAM or disk and bring them back on the next turn, for example with LMCache or NVIDIA KVBM. A conversation being computed still needs its cache in GPU memory, so the gain is that blocks return faster than prefill would compute them. This helps when many conversations together exceed GPU memory. It doesn’t help one 500,000-token session: that is 164 GB in the example, more than this replica’s 140 GB of KV cache, so it needs a larger replica or a model with a smaller cache per token. DeepSeek documents a disk cache. OpenAI says it may keep encrypted KV tensors in GPU-local storage; Anthropic says only that they are held in memory for the cache lifetime. Neither describes the mechanism.
- **Running out.** When free blocks run out mid-generation, the scheduler pauses a conversation, usually the most recently admitted, and frees its blocks. On resume vLLM V1 recomputes the missing cache by default, and the user sees the stream stall.
- **Streaming.** The sampler picks tokens, the engine process checks for an end token and the length limit, and the API process sends text as SSE events. An incomplete UTF-8 character waits for the next token. If generation isn’t finished, the conversation stays in the loop. vLLM usually doesn’t wait for the picked token: the scheduler plans the next pass while the current one runs, with a placeholder for that token, so the GPU doesn’t sit idle.
- **Reasoning.** To the server these are ordinary tokens, and a parser separates them from the answer. Higher reasoning effort gives no extra GPU: the conversation holds its blocks and batch slot for longer (see “Reasoning models”).

### Level 4. Model

- **Shared weights.** In decode one pass gives every conversation in the batch one new token. The weights are read once per pass for the whole batch, so eight conversations read the weights as often as one. That is the point of a batch. Each conversation’s KV cache, though, is separate and read in every pass, so a larger batch can increase the time between tokens in each stream. Memory sets only the upper limit on conversations at once.
- **One conversation’s cache.** In this example, 64 attention heads compare each token’s query with keys from that token and earlier tokens in parallel. Grouped-query attention lets them share 8 sets of keys and values, the 8 KV heads. Every layer keeps the key and value of every token for each KV head. In the example: 2 (key and value) × 80 layers × 8 KV heads × 128 numbers (8192 / 64 heads) × 2 bytes = 0.33 MB per token, 10.5 GB for 32,000 tokens. If each of the 64 heads had its own keys and values it would be 84 GB, so this replica’s cache would hold only one such conversation. The formula and data for real models are in “The generation loop and KV cache”.
- **How many conversations.** 140 GB of cache is about 430,000 tokens: 13 conversations of 32,000 tokens computed at the same moment, if they share nothing, or about 100 of 4,000. Conversations with the same prompt start share its full blocks, across tenants too. A faster answer can reveal that another tenant sent the same prompt prefix, so the gateway can give each tenant a secret, unguessable `cache_salt` value, which turns this sharing off (see “Prompt caching”). KV cache capacity works like a database connection pool, except that a conversation holds its place for the whole answer, seconds rather than milliseconds, and uses more memory as its context grows. If the replica can sustain 100 conversations at once and an answer takes 10 s on average, it completes about 10 answers per second (illustrative). So a replica serves far more chat users, who read between messages, than agents, which send the next request right after running a tool. Extra requests wait in the queue.

### Level 5. Libraries

- **Execution.** Model layers become launches of small GPU programs: matrix multiplication from cuBLAS, attention from FlashAttention. Compilation can fuse operations, and a CUDA Graph replays recorded work with one call, so the CPU doesn’t submit each program separately. The GPU still runs them all.
- **Memory.** The KV cache is a PyTorch tensor prepared at start. PyTorch’s allocator keeps the memory it got from the driver, so nvidia-smi shows the reservation, not current use. The block assignments from level 3 happen inside this tensor, without reallocating GPU memory.
- **Other hardware.** Levels 5–7 depend on the hardware: AMD GPUs use ROCm, Macs use Metal and TPUs use the XLA compiler.

### Level 6. Driver

- **Resources.** The driver in the host kernel creates GPU channels and maps memory into the process at start and on allocations. Reusing a KV block needs no new mapping.
- **Submitting work.** After that launching a GPU program is a write to a command buffer and to the doorbell, with no system call. That’s why repeated launches don’t load the Linux kernel.
- **Image and host.** vLLM, PyTorch, cuBLAS and the CUDA runtime come from the container image; the kernel module and usually `libcuda.so` come from the host through the NVIDIA Container Toolkit. The host driver usually has to be new enough for the CUDA version in the image.

### Level 7. GPU

- **Tensor parallelism.** It splits one layer across GPUs: each weight matrix is cut into four parts, and each GPU holds 2 of the 8 KV heads. A conversation of 32,000 tokens therefore takes about 2.6 GB on each of the four GPUs. No GPU has a layer’s full result, so they must compute it together.
- **Why fast links matter.** In the classic layout GPUs add up partial results (all-reduce) twice in every layer, after attention and after the MLP, 160 times per pass with 80 layers, and all four wait for each exchange. In decode one exchange is small, 8 conversations of 1 token × 8192 numbers × 2 bytes = 131 KB, so latency matters. Across a 32,000-token prefill the tensors in these exchanges total 32,000 × 8192 numbers × 2 bytes × 160 = about 84 GB, so bandwidth matters. Traffic on the links also depends on the reduction algorithm. Illustrative numbers.
- **NVLink and the network.** H100 NVLink gives 900 GB/s in total, about 450 GB/s each way. A 400 Gb/s network port (InfiniBand or RoCE) gives 50 GB/s each way, even with RDMA writing straight into GPU memory. For this prefill, a ring all-reduce across four GPUs needs each GPU to send and receive about 126 GB. At full link utilisation, moving this data alone takes at least 2.5 s on such a port and 0.28 s on NVLink. Actual transfer time depends on the algorithm, topology and overhead. In decode, the latency of 160 small exchanges per pass matters, so GPUs sharing layers usually stay inside one NVLink domain, the set of GPUs joined by that link.
- **Pipeline parallelism.** It splits the model into groups of layers, one per node, each with its own weights and cache. Between nodes, adjacent stages pass the result once, while GPUs within a node still exchange partial results. So the stages can run on separate nodes, though network bandwidth and latency still matter. The price is latency, because a token passes the stages in turn, although several batches at different stages can raise throughput. vLLM’s rule: tensor parallelism equal to the GPUs in a node, pipeline parallelism equal to the number of nodes.

### Check yourself

**Question:** How does a request reach the GPU, and what does each level do?

**Short answer:** The application sends the conversation history. The gateway checks access, and the router picks a replica. The server tokenises messages, schedules model passes and allocates memory. A worker launches the model’s computations through GPU libraries and the driver. The GPU computes, the sampler picks a token, and the server streams text. Model passes repeat until a stop condition is met. Reasoning has no separate level: it is ordinary tokens the model generates.

### Follow-up questions

- **Does CUDA know the model is reasoning?** No. CUDA launches GPU programs that compute on tensors. Whether the next tokens are reasoning comes from the prompt, the tokens so far and the weights, and separating them from the answer is the job of a server-side parser.
- **A container reports a driver that is too old, although the host has the CUDA Toolkit. What do you check?** The Toolkit doesn’t matter here. What counts is the host driver, <code>libcuda.so</code> and the kernel module, and the version of the CUDA libraries in the image. Update the driver, use an image built for an older CUDA, or use a matching <code>cuda-compat</code> package, if the GPU and driver are supported.
- **Why doesn’t launching a GPU program need a system call?** When resources are created, the Linux kernel maps the command buffers and doorbell into the process. After that the user-space driver writes commands to memory and notifies the GPU with a doorbell write, so the CPU doesn’t switch to kernel mode for every operation. NVIDIA publishes the command format and doorbell in open-gpu-doc and open-gpu-kernel-modules, including for the H100, while research describes how the closed-source libcuda.so turns CUDA calls into those commands.
- **Your model doesn’t fit on one GPU and throughput is too low. What do you do?** Check quantisation or a GPU with more memory first. Otherwise tensor parallelism splits one copy’s weights, KV cache and computation across the GPUs of one node. Across nodes it is split into groups of layers by pipeline parallelism. Replicas add independent copies for concurrent requests. Measure the throughput of the configurations you’re considering.

### Sources

- [vLLM: Architecture Overview](https://docs.vllm.ai/en/latest/design/arch_overview/)
- [vLLM: Parallelism and Scaling](https://docs.vllm.ai/en/latest/serving/parallelism_scaling/)
- [NVIDIA: Hopper Architecture In-Depth](https://developer.nvidia.com/blog/nvidia-hopper-architecture-in-depth/)
- [Yan, Karlin, Grant: Revealing NVIDIA Closed-Source Driver Command Streams (2026)](https://arxiv.org/abs/2604.26889)

Interactive page: https://howaiworks.dev/inference-stack/
