Basis Lab

Answers · How models work

Why does a transformer need a cache that grows with sequence length?

In shortA transformer stores the key and value for every token it has already seen so later tokens can look back at them, and that store grows with the conversation.

A transformer caches the key and value vectors for every token it has already processed, in every layer, because each new token's attention compares against all of them. The cache therefore holds one entry per token per layer, so its size rises in a straight line with the length of the conversation, and the work needed to read it for each new token rises in the same way.

M(t)=c t,c=0.5 MB1000 tokens=0.0005 MB per tokenM(t)=c\,t,\qquad c=\frac{0.5\ \text{MB}}{1000\ \text{tokens}}=0.0005\ \text{MB per token}

Take the store at 0.5 MB for the first thousand tokens. At 8 thousand tokens the conversation is eight times longer, so the store is 0.5×8=4.00.5 \times 8 = 4.0 MB. At 64 thousand it is another factor of eight, 4.0×8=324.0 \times 8 = 32 MB. At 512 thousand it is another factor of eight, 32×8=25632 \times 8 = 256 MB. A fixed carried state of 8 MB does none of this: it is 8 MB at 1 thousand tokens and 8 MB at 512 thousand. At the long end the cache is 256/8=32256/8 = 32 times the fixed state, for one conversation.

The constant is the whole argument. At 2000 tokens the store is 0.0005×2000=1.00.0005 \times 2000 = 1.0 MB; at 4000 tokens it is 0.0005×4000=2.00.0005 \times 4000 = 2.0 MB. Doubling the tokens doubles the store. Linear growth is not constant, and no amount of fast arithmetic changes the fact that the store has to be held somewhere.

FIG 1
contextstore per conversationconversations in 40,960 MB
1 thousand tokens0.5 MB81,920
8 thousand tokens4.0 MB10,240
64 thousand tokens32.0 MB1,280
512 thousand tokens256.0 MB160
fixed state, any length8 MB5,120

Read the last column as capacity. At 1 thousand tokens the store is small, so 40,960/0.5=81,92040{,}960/0.5 = 81{,}920 conversations fit. At 8 thousand tokens the store is 4 MB and only 40,960/4=10,24040{,}960/4 = 10{,}240 fit: eight times the context, an eighth of the conversations. At 64 thousand tokens 40,960/32=1,28040{,}960/32 = 1{,}280 fit, and at 512 thousand only 40,960/256=16040{,}960/256 = 160. The fixed state holds 5,120 at every one of those lengths. The machine's capacity has become a function of how long people have been talking, which is the operator's problem.

The cache exists because the alternative is recomputation. For each new token the layer makes a query and compares it with stored keys, then mixes the stored values. Without the cache it would recompute keys and values for every previous token at every step, which is more work, not less. So the cache trades recomputation for memory, and the memory it trades for is linear in the number of tokens.

Why one new token is slower at the end

Each new token must consult every entry in the store, not just the newest one. At 512 thousand tokens there are 512,000/1000=512512{,}000/1000 = 512 times as many entries as at 1 thousand, so the read is roughly 512 times larger. The arithmetic may be cheap relative to moving the data, which is why time per token climbs roughly with context length and a reply late in a session feels slower than the same reply early on.

The fixed-state guess

The natural guess is that attention is a fixed operation on a fixed-size state, so serving cost should be flat. The weights are fixed, but the per-conversation state is not: it is the concatenation of every previous token's key and value. Recomputing those from scratch for each new token would be worse, so the cache is necessary, but the cache is exactly the thing whose size grows. Nothing in the weights grows; the growth is in the working memory held for each conversation.

The comparison with a carried state can be made exact. Write the look-back layer as a recurrence: st=st−1+ϕ(kt)vt⊤s_t = s_{t-1} + \phi(k_t)v_t^\top. Here sts_t is the running summary, and it accumulates one contribution per token. That is why it keeps growing in effect: nothing is discarded. Put a decay factor in front of st−1s_{t-1} and the state becomes fixed; that single coefficient is the difference between the two families.

Where the growth can be removed

The growth disappears if the layer is not allowed to look back without limit. Restricting attention to a fixed window caps the store at the window size, and replacing the layer with a recurrence that carries a fixed block removes it entirely. The price is exact recall: a window cannot recover a name from before the window, and a fixed state compresses rather than stores. A hybrid keeps most layers fixed and a few looking back. Two look-back layers out of thirty six scale the growing part of the bill by 2/36=1/182/36 = 1/18, so the slope is eighteen times smaller than a pure look-back stack's. That turns a slope that forces short contexts into one that can be absorbed, while the look-back layers still recover exact details when needed.

Worth keeping

  • At half a million tokens the growing store is 256 megabytes per conversation, while a fixed carried state is 8 megabytes.
  • On forty gigabytes that is 160 conversations against 5,120 for the fixed state at the long end.
  • Capping attention to a window or carrying a fixed state removes the growth, at the price of exact recall over everything older.

Where this goes next

This question is one step of Models That Carry a Memory, a course that works through its subject from the beginning. The page that teaches this part of it is Constant Memory Against a Growing Cache, and it reads the whole way through whether or not you have an account.

What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.

Read that pageThe whole course

Other questions people ask about this

  1. Why can a linear recurrence be computed as a convolution?Because a linear recurrence adds up past inputs with fixed decaying weights, and that weighted sum is a convolution with the recurrence's impulse response.
  2. How can a model remember a sequence without keeping every past input?A model can remember a sequence by keeping a fixed-size block of numbers and rewriting it at every step from its old value and the new input.
  3. How does a gate let information pass through unchanged?A gate lets old information take a direct additive route through the steps, with a learned fraction deciding how much survives each step.
  4. What is actually stored in the state that a model carries forward?It is a fixed-size block of numbers rewritten at each step from its old value and the new input, not a stored copy of earlier inputs.
  5. Why does the model get stuck repeating the same sentence over and over?The model is not stuck in a memory loop: once it has written a phrase, that phrase is in its context and makes writing it again more likely, so a decoder that always picks the top word cannot leave.
  6. Why does diffusion add Gaussian noise instead of any random corruption?Diffusion adds Gaussian noise because Gaussian errors combine exactly: each step keeps the spread fixed and gives a closed-form noisy picture at any later step, which makes the reverse step learnable.

Read alongside