Skip to content

KV-Cache in Simple Terms: Why Long Dialogues Cost More

Cover: KV-Cache in Simple Terms

Noticed that towards the end of a long dialogue, the model responds slower and each request becomes more expensive? It's not due to 'tiredness' of the model, but due to the KV-cache — a mechanism that accelerates generation at the cost of growing memory.

Why Do We Need a Cache at All

A language model generates text token by token. To predict the next token, it 'looks' at all previous tokens. A naive approach — recalculating everything from scratch at each step — is horribly inefficient: for one new word, the model would reprocess the entire dialogue.

KV-cache solves this: the results of processing previous tokens are saved and not recalculated. A new token is simply added to what has already been calculated.

What Is Stored in the Cache

For each token, the model stores two things:

  • K (Key) — 'what this token can offer to others';
  • V (Value) — 'what this token carries in meaning'.

These are internal vectors of the attention mechanism. When a new token arrives, it 'checks' against all K and gathers meaning from V.

Why Dialogues Become More Expensive

The cache grows with each message: the longer the history, the more memory K and V occupy.

flowchart LR
    A[Token 1] --> B[KV-cache]
    C[Token 2] --> B
    D[Token N] --> B
    B --> E[Memory grows linearly<br>with dialogue length]

From this, three consequences follow:

  1. Slower — more data to read from VRAM;
  2. More expensive — more calculations for each new token;
  3. More constrained — the cache may hit memory limits before the context window.

How Much Is This in Numbers

For a model with 7 billion parameters, one context token occupies approximately 0.5–1 KB in the cache. A dialogue of 10,000 tokens already amounts to tens of megabytes of auxiliary memory. For larger models and longer contexts, the count is in gigabytes.

This is why inference servers value cache optimization so much: it directly saves hardware.

How to Live with This

  1. Keep dialogues short. New task — new chat.
  2. Summarize long conversations and continue with the summary.
  3. Don't insert large documents in full — this instantly inflates the cache.
  4. For production, choose servers with efficient cache (vLLM and similar).

What Is Prompt Caching

A separate trick: if the system prompt is the same for all requests, its KV can be calculated once and reused. This is prefix caching, and it significantly reduces the cost of mass requests with a common prefix.

In Brief

KV-cache is the price for speed. It frees the model from recalculating the past but occupies memory that grows with each token. Being mindful of dialogue length is a concern for both speed and cost.

See Also