A chat model does not produce a complete answer in one pass. It first reads the prompt, then predicts the next token, adds that token to its context, and repeats. That loop is the heart of transformer inference.
The two phases have different work patterns. Prefill processes the input prompt in parallel. Decode generates output one token at a time. Between them, the KV cache keeps attention data from earlier positions so the model can reuse it on each step.
From prompt to response
Text is split into tokens: pieces that may represent a whole word, part of a word, punctuation, or other symbols. The model turns those tokens into vectors and passes them through a stack of transformer layers. At each layer, attention uses query, key, and value representations to relate the current position to its context.
For an incoming request, inference is commonly organized into prefill and decode. The boundary matters operationally: prefill contributes to the time before the first token, while decode contributes to the time between streamed tokens.
1. Prefill processes the prompt
During prefill, the model evaluates the prompt positions together through each transformer layer. This parallel work produces the representation needed to predict the first response token. The layer's key and value vectors for prompt positions are also written into the KV cache.
Longer prompts generally mean more prompt processing and a larger initial cache. The exact latency depends on the model, accelerator, numerical precision, batching, and serving stack. There is no single prompt length or prefill speed that applies to every deployment.
2. The KV cache keeps context ready
For each layer and token position, the model can retain the attention key and value vectors it has already computed. On later decode steps, it calculates new values for the newest token and reuses cached values for earlier positions instead of recomputing them from scratch.
The cache is not the model's full hidden state or a transcript of the conversation. It is runtime attention data associated with the current sequence. Its footprint depends on architecture choices such as layer count, attention heads, token count, and data format.
3. Decode generates and streams tokens
Once prefill is complete, the model selects a next token from its output distribution. That token is appended to the sequence; the model computes its new layer values, updates the cache, and predicts again. Sampling settings and stopping rules determine how the sequence continues and when generation ends.
Because each output step depends on the previous one, decode is sequential. Serving systems can stream each available token to the application as it is produced, so a user may see a response before the full answer is finished. Time to first token and the pace of later tokens are separate user experience measures.
Explore the inference loop
Adjust the prompt and output lengths, then step through decode. The diagram is illustrative: it shows the changing token count and cache length, not measured model performance.
Interactive inference flow
Move from prompt processing to a growing KV cache and streamed output.
Swipe sideways to explore the full architecture diagram.
Simplified flow · the cache contains per-layer attention keys and values for earlier sequence positions.
Ready. The prompt is cached; run a decode step to add an output token.
Illustrative only. Real memory use, latency and throughput vary with model architecture, hardware, precision, context length, batching and serving configuration.
What this means for enterprise serving
The distinction between prompt processing and token generation helps teams reason about service behavior. A request with a large context can have a different bottleneck from a short prompt that produces a long answer. Capacity planning should consider both the incoming and generated tokens, plus the number of requests active at once.
| Serving concern | What to consider |
|---|---|
| First-token latency | Prompt size, prefill throughput, queueing, and the time needed to schedule the request. |
| Streaming pace | Decode speed, memory bandwidth, batching policy, and contention with other requests. |
| Cache capacity | Context length, active sequence count, model dimensions, cache precision, and eviction policy. |
| Quality and cost | Model choice, prompt design, output limits, sampling settings, and task-specific evaluation. |
These are connected decisions. Batching may improve hardware utilization while changing how requests wait; longer contexts can preserve useful information while using more cache. Teams should measure first-token latency, inter-token latency, quality, and utilization on representative workloads before choosing a serving configuration.
The short version
Prefill reads the prompt in parallel. The KV cache retains prior attention data. Autoregressive decode reuses that context to predict the next token, then repeats—allowing applications to stream the response as it grows.