In AI inference, particularly for Large Language Models (LLMs), processing happens in two distinct phases: Prefill and Decoding.

1. Prefill Phase (The Input Phase)

The prefill phase takes the entire user prompt and processes it all at once to understand the context.


2. Decoding Phase (The Output Phase)

The decoding phase generates the response text sequentially, one single token at a time.

https://medium.com

How KV Caching Optimizes Inference

During the Decoding phase, the AI model needs to look at all previous tokens to predict the next word. Without KV (Key-Value) Caching, the GPU would have to re-calculate the mathematical representation (the attention context) of every single prompt token and generated token for every single new word it outputs.

KV Caching works like a temporary scratchpad: