Skip to content
Technology, explained from the insideAI · Software · Digital payments
TayoTechTalk.

How AI Works

KV Cache Explained: Why Long AI Chats Use More Memory

By Temitayo Solomon Gbadebo · November 17, 2026
A row of tokens feeds two trays of colored blocks representing cached keys and values.

A chatbot can answer a short question quickly, then become slower as the conversation grows. One reason is that generating the next piece of text depends on information from earlier text. Repeating every calculation would be wasteful, so many transformer-based language models keep a key-value cache, usually shortened to KV cache.

Think of it as reusable working material for a calculation, not a diary of what the assistant knows about you. Understanding that distinction helps explain both AI performance and the limits of conversational “memory.”

What the cache actually stores

In an attention layer, a token produces numerical representations called a query, a key and a value. Roughly, the query helps determine what information the current position needs; keys help determine relevance; values supply information to combine.

During ordinary causal generation, later tokens do not change the already-computed keys and values for earlier positions. The model can therefore keep those earlier results and reuse them. It still performs attention and other calculations for the new token; caching does not make the next answer free. Hugging Face’s caching explanation describes this mechanism and its relationship to causal attention.

Prefill and decode are different jobs

Prefill processes the input prompt and builds its attention state. Many input-token computations can run in parallel. A large document can make this phase substantial before the first visible answer appears.

Decode continues the sequence, typically producing one new token per generation step. Each step uses previous cached keys and values and adds the newly processed token’s state. A token is a model’s text unit, not necessarily a complete word.

That is why “time until the first token” and “tokens generated per second” measure different experiences. A system might start answering sooner without dramatically speeding up the rest of a long response. NVIDIA’s inference guide explains the two phases and their different compute and memory demands.

A small example of reuse

Suppose a simplified model receives four tokens, represented as A, B, C and D. These letters stand for token positions, not literal words.

  1. Prefill processes A–D and stores their keys and values at each relevant attention layer. The model predicts token E.
  2. To continue, it processes E, adds E’s keys and values, and reuses the stored state for A–D. It predicts F.
  3. It then processes F, extends the cache again, and predicts the next token.

The saving is avoiding repeated construction of the earlier keys and values. The model still consults the relevant earlier state. This example is deliberately simplified: real serving systems can batch requests and use more advanced generation strategies.

Three rows show prefill computing A through D, then decode reusing prior keys and values while computing them for E and F.
Prefill builds the initial cache. At each decode step, the model reuses stored keys and values and adds state for the newly processed token.

Why longer conversations need more memory

A conversation included in a prompt occupies token positions. In a conventional full-attention cache, more retained positions mean more cached numerical data. Serving several conversations simultaneously adds further demand. Model architecture, numerical precision and the number of attention layers also affect the size.

Not every model keeps a cache that grows indefinitely with the full sequence. Sliding-window attention can limit the retained state for affected layers. Implementations can also reserve fixed capacity, move cache data to CPU memory, or compress its numerical representation. These choices involve memory, transfer and performance trade-offs. Hugging Face’s cache-strategy guide compares the approaches.

Prefix caching is a related optimisation

Imagine asking several questions about the same long document. If successive requests begin with the same token sequence, a serving engine may reuse that common prefix’s KV state rather than process it again.

This requires compatible cached state to remain available. Similar meaning is not enough: rewriting an early sentence changes the prefix. vLLM’s automatic prefix caching documentation explains that this optimisation reduces shared-prefix processing, rather than directly shortening the generation of a new answer.

It is not durable personal memory

The useful distinction is between computational reuse and product-level memory. A saved preference, conversation database or retrieval system decides what information to preserve and supply later. A KV cache helps process a compatible sequence efficiently.

A cache can be evicted, rebuilt or reused under a service’s implementation rules. Its existence does not promise permanent recall, update the model’s learned weights, or extend the model’s supported context limit. Conversely, clearing a computation cache does not prove that stored conversation records were deleted.

The takeaway: KV caching trades storage for less repeated work. For an AI application, measure prompt-processing latency, generation speed and memory usage separately. For an everyday user, remember that a faster repeated prompt and an assistant remembering your preferences are two different features.