What matters in AI.

Subscribe

Learn / AI basics

Definition · AI basics

Prefill step

The prefill step is the first step of language model inference. The model reads all prompt tokens at the same time. It keeps their keys and values in the KV cache. The step ends with the first output token. The decode step then writes the remaining answer.

Last reviewed

Key points

  • Inference has two steps. Prefill reads the full prompt, then decode writes the answer one token at a time.
  • Prefill needs GPU compute. Decode needs memory speed.
  • Time to first token (TTFT) measures prefill. A long prompt gives a slow first token.
  • Prompt caching keeps prefill results for use again. In 2024, seven API providers shared one cache across users, a timing side channel.
  • A "prefilling attack" is a different thing. It is a jailbreak.

How the prefill step works

Prefill is the first of two steps of inference.

Prefill Decode
Reads All prompt tokens One new token each time
Speed limit GPU compute Memory speed
Makes KV cache, first token The remaining answer
Metric Time to first token Time for each output token

For each token, the attention mechanism makes keys and values. The key value cache keeps them for decode.

Prefill fills the KV cache one time. Decode reads it for each token.
Prefill fills the KV cache one time. Decode reads it for each token.

Why the prefill step matters

  • Cost. Patel and colleagues (2023) found prefill “compute-intensive” and decode “memory-intensive”.
  • Shared GPUs. On the same GPUs, the two steps slow each other (Zhong and colleagues, 2024).
  • Data leaks. A shared cache is faster on a hit. This timing can show that a different user sent a prompt.

In practice: four methods to manage the prefill step

  1. Prompt caching. The server keeps the KV cache of a prompt start for use again. OpenAI gives a discount of a maximum of 95% on cached input tokens (OpenAI documentation, read December 2026). The full prefix must be the same. Thus, put stable instructions first.
  2. Chunked prefill. SARATHI cuts a prefill into equal chunks and mixes them with decode work. Agrawal and colleagues (2023) report up to 10x higher decode throughput for LLaMA-13B on one A6000 GPU.
  3. Different machines. Splitwise (2023) and DistServe (2024) put prefill and decode on different GPUs. DistServe reports up to 7.4x more requests than the systems in its test.
  4. Shorter prompts. Fewer input tokens give less prefill work. This includes text that tools add to the context window.

Three questions for a security team:

  • Is the prompt cache for each user, each organization, or all users?
  • Can a user measure the time to the first token?
  • Do the prompts hold secrets that an attacker can guess?

Where definitions disagree

The word “prefill” has two meanings in AI.

Serving. In inference papers, prefill is the step that reads the prompt. This page uses that meaning.

Response prefill. Some APIs let a developer write the first words of the answer. Attackers use this in a jailbreak called a prefilling attack. Qi and colleagues (2024) write: “prefill the first few tokens with a non-refusal prefix”. They show that safety training changes mostly the first few output tokens.

The two meanings use one mechanism: the model reads the given tokens in the prefill step. But the risks are different. One is a timing leak. The other is control of the answer.

Questions and answers

What is the difference between prefill and decode?

Prefill is the first step of language model inference. The model reads all prompt tokens at the same time and keeps their keys and values. Decode is the second step. The model writes output tokens one at a time. NVIDIA tells that prefill is very parallel and decode is memory-bound.

Why does a long prompt make the first token slow?

The prefill step must read all prompt tokens before the first output token comes. Time to first token (TTFT) measures the prefill step. More input tokens give more work and a slower first token. Prompt caching can stop this work for a prompt start that the server knows.

Is a prefilling attack the same as the prefill step?

No. The prefill step reads the prompt. A prefilling attack is a jailbreak. The attacker writes the first words of the answer of the model, and the model continues from them. Qi and colleagues (2024) show why it works.

Sources

  1. Mastering LLM Techniques: Inference OptimizationNVIDIA Technical Blog (Verma and Vaidya), 17 Nov 2023
  2. Splitwise: Efficient generative LLM inference using phase splittingarXiv (Patel et al.), 30 Nov 2023
  3. DistServe: Disaggregating Prefill and Decoding for Goodput-optimized Large Language Model ServingarXiv (Zhong et al.), 18 Jan 2024
  4. SARATHI: Efficient LLM Inference by Piggybacking Decodes with Chunked PrefillsarXiv (Agrawal et al.), 31 Aug 2023
  5. Prompt cachingOpenAI
  6. Auditing Prompt Caching in Language Model APIsarXiv (Gu et al.), 11 Feb 2025
  7. Safety Alignment Should Be Made More Than Just a Few Tokens DeeparXiv (Qi et al.), 10 Jun 2024

In the news