What is AI prefill?

Prefill is the inference phase where a language model processes all prompt tokens in parallel in one pass, writes each layer's keys and values to the KV cache and produces the first output token, all before generation begins.

It differs from the decode phase in workload: prefill handles all prompt tokens at once, so it is compute-bound and uses the GPU heavily, while decoding produces one token per step and is limited by memory bandwidth. The two phases therefore need different optimisations.

Topic
Inference and performance
Also known as
Prefill phase, prompt processing

How AI prefill works

  1. All prompt tokens enter the model at once; self-attention between them is computed together in each layer.
  2. Each layer's key and value vectors are written to the KV cache.
  3. The first answer token comes from the last position's output; the time until then is most of the time to first token.
  4. The decode phase then begins, producing remaining tokens one by one using the cache.

In more detail

Prefill-decode disaggregation

A major production architecture for high-throughput serving is Prefill-Decode (PD) disaggregation (e.g., DistServe, Splitwise and disaggregated modes in vLLM and SGLang). Because prefill is compute-bound (large matrix-matrix multiplications) while decode is memory-bandwidth-bound, running them on separate GPU pools and transferring the KV cache over fast interconnects (NVLink/RDMA) largely removes decode stalls and lets teams scale TTFT and inter-token latency independently.

Linear versus quadratic cost and the real role of FlashAttention

In terms of complexity, feed-forward projections scale linearly O(n) with prompt length n, whereas standard self-attention scales quadratically O(n²) in compute and (without IO-aware kernels) in memory traffic. FlashAttention does not eliminate the O(n²) arithmetic complexity of attention, but by tiling computations in fast on-chip SRAM it avoids materialising the full attention matrix in HBM, dramatically accelerating prefill and cutting memory usage to O(n).

Image and audio token load in multimodal models

For multimodal LLMs (vision-language or audio-language models), prefill also includes encoding images, video frames or audio clips through modality encoders and projecting them into token embeddings, which can easily add hundreds or thousands of visual tokens and shift prefill bottlenecks toward vision encoder latency.

Commonly confused terms

TermHow it differs
Prefill attackPrefill as an inference phase refers to parallel prompt processing inside the LLM engine; prefill attack refers to exploiting an API feature that lets users pre-populate the start of the assistant's reply to bypass safety alignment.
Chunked versus disaggregated prefillChunked prefill splits a long prompt into smaller chunks interleaved with decode steps on the same GPU; disaggregated prefill separates prefill and decode onto distinct GPU pools optimised for each workload.

Why AI prefill matters for on-premises AI

In in-house applications with long documents or long system prompts, prefill largely sets how long users wait for the first word. Compute grows with prompt length, with the attention part growing with its square. Prefix caching skips prefill for repeated prompt parts, while chunked prefill stops one long prefill from stalling other users' answers. Capacity plans measure prompt and answer lengths separately, as the phases stress hardware differently.

ON-PREMISES EXAMPLE

An engineering firm's technical standards assistant sends a long standard in one prompt; since users see the first answer only after prefill, the firm keeps frequently used standards in the prefix cache.

Common questions

How does prefill differ from decoding?

Prefill processes the whole prompt in parallel and is compute-bound. Decoding produces answer tokens one at a time and is limited by memory bandwidth.

How can prefill time be reduced?

Skip repeated prompt parts with prefix caching, trim unneeded context and use efficient attention kernels such as FlashAttention.

Sources

In the glossary: Glossary, M to Q

What does this term mean for your infrastructure?

We can work out together in a technical assessment how it maps onto your data, hardware and security requirements.

Request a technical assessment →

This site sets no cookies. Visitor numbers are counted in aggregate using a cookieless method that does not identify anyone. See the Privacy Policy for details.