Paths Subjects Questions Quizzes Pricing Search
Intermediate Open Free

Explaining Why Long Context Costs More Than It Looks Like It Should

Your product team wants to double the maximum conversation length your chat product supports, from 16k to 32k tokens of context. Finance pushes back: the infra team's early load test shows GPU memory usage for concurrent sessions went up by far more than 2x, and inter-token latency on long conversations got noticeably worse too. Nobody on the product team understands why "twice the context" isn't "twice the cost."

  1. Explain, mechanistically, what is actually consuming the extra memory, and why it scales the way it does.
  2. Sketch a rough number: for a 13B-parameter model with 40 layers, d_{model}=5120, GQA with 8 KV heads (head dim 128), fp16 throughout, how much KV cache memory does one 32k-token conversation need? How does that compare to the model weights themselves?
  3. Why did per-token latency get worse too, not just memory usage?
  4. Name two architectural or serving levers you could pull if you wanted longer context without this scaling hitting you as hard.
Solution

1. What's consuming the memory

Every generated token requires the model to attend back over every prior token's key and value vectors. Rather than recomputing those key/value projections from scratch at every generation step (which would make generation quadratically expensive in redundant work), the serving stack caches them — one K and one V vector per token, per layer, per KV head. That KV cache is stored in the same GPU memory as the model weights, and its size scales linearly in context length but also linearly in every other cache dimension (layers, KV heads, head dimension, batch size). Doubling context length doubles the cache, but total memory pressure is cache-plus-weights-plus-activation-buffers, and if the cache was already a meaningful fraction of memory at 16k, doubling it can push total usage well past 2x once you account for reduced headroom for concurrent requests (fewer requests fit before hitting the memory ceiling, so the load test's effective per-request cost looks worse).

2. The number

Per-token cache cost: 2 \times 40 \times 8 \times 128 \times 2\text{ bytes} = 163{,}840 bytes \approx 160 KB/token.

At 32k tokens: 160\text{ KB} \times 32{,}000 \approx 5.1 GB per conversation.

Model weights at 13B params, fp16: 13\text{e}9 \times 2 bytes = 26 GB, loaded once and shared across all requests.

So one long conversation's cache is about a fifth of the entire model's weight footprint — and that's per conversation. Ten concurrent 32k-token sessions would need ~51 GB of cache alone, nearly double the weights. This is the arithmetic that explains why "just double the context" is a capacity-planning decision, not a config change.

3. Why latency got worse too

Decoding one token at a time is memory-bandwidth-bound, not compute-bound: each step's actual FLOPs are tiny (one new token), but the GPU still has to read the relevant KV cache (and the model weights) from memory for every step. A longer context means more cache to read on every single decode step, so each step's memory traffic grows with context length even though the compute per step barely changes. That's why inter-token latency creeps up as a conversation gets longer — it isn't a bug, it's a direct consequence of what decode is bottlenecked on.

4. Levers

  • Reduce KV heads further (more aggressive GQA/MQA) — directly divides the per-token cache cost, at some quality cost that has to be validated.
  • Quantize the KV cache (e.g., to int8 or lower) — same linear-scaling shape, smaller constant factor, roughly halving or better the memory cost with usually modest quality impact.
  • Also acceptable: cache eviction/windowing strategies (keep a sliding window plus a compressed summary of older turns) or serving techniques like paged KV-cache allocation that don't reduce total bytes but make the memory usable more efficiently — the key is recognizing these are serving-layer mitigations, not free lunches.

Share this question

← Back to Transformers for AI Engineers practice

We use cookies for product analytics to improve OmniAtlas. See our Privacy Policy.