Longer context windows increase GPU memory usage almost entirely through the KV cache, which grows linearly with the number of tokens in a sequence, so doubling the context length roughly doubles the memory each active request consumes, independent of the model weights, which stay fixed. This means a model that comfortably serves many short conversations can run out of memory quickly once users start sending long documents or the conversation history grows, even though the weights themselves have not changed. The effect compounds with concurrency, since every simultaneous request holds its own KV cache, so a server handling ten users at 4K tokens each needs roughly ten times the KV cache memory of a single such user, and that multiplies again if the context length is extended to 32K or 128K tokens. Grouped-query attention, multi-query attention and KV cache quantization to FP8 or INT8 are the main levers for reducing this cost without shrinking the usable context window. Capacity planning for any production deployment has to model the realistic distribution of context lengths and concurrent sessions, not just the maximum context the model supports. Nanobase AI models this growth curve explicitly when sizing GPUs for long-context enterprise use cases.
The growth curve, made concrete
Context length affects memory almost entirely through KV cache, and because that relationship is linear, it is worth seeing the actual curve rather than just being told it grows. The table below uses a 70B model with grouped-query attention, at roughly 320 KB of FP16 KV cache per token.
| Context length | KV cache, 1 session | KV cache, 10 concurrent sessions |
|---|---|---|
| 4K tokens | ~1.3 GB | ~13 GB |
| 8K tokens | ~2.6 GB | ~26 GB |
| 32K tokens | ~10.2 GB | ~102 GB |
| 128K tokens | ~41 GB | ~410 GB |
Key takeaway: doubling context length doubles KV cache exactly, and multiplying concurrent sessions multiplies it exactly again, so the two effects compound fast.
Why weights staying fixed makes this feel deceptive
Model weights are loaded once and never change size regardless of how long a conversation runs, so a deployment that works fine in testing with short prompts can look identical, memory-wise, right up until real users start sending long documents or extended conversation histories. This is the most common way a 70B deployment on a single 80 GB GPU passes every pre-launch test and then fails in production: the weights (70 GB in FP8) leave only about 10 GB of headroom, which the table above shows is barely enough for a single 32K-token session, let alone several concurrent ones.
Because the weight and KV cache costs are independent line items, sizing has to model the actual distribution of context lengths a deployment will see, including the tail of unusually long requests, not just the average case.
Key takeaway: fixed weight memory hides the KV cache growth curve until real traffic exposes it, which is why context length has to be sized explicitly rather than assumed.
Mitigations that change the curve itself
Three techniques directly reduce the slope of this growth rather than just buying more memory to absorb it. Grouped-query and multi-query attention, built into the model architecture, reduce the number of key-value heads and therefore the per-token cost before any deployment decision is made, which is why the 320 KB per token figure above is already far lower than an older full multi-head attention model would produce. KV cache quantization to FP8 or INT8 at serving time roughly halves or quarters the same curve without retraining or re-downloading the model. Sliding-window or chunked attention strategies, where supported, cap how much history contributes to KV cache regardless of nominal context length, trading some long-range recall for a hard memory ceiling.
Key takeaway: architecture, quantization and windowing techniques change the slope of the memory curve itself, which is a more durable fix than simply adding GPUs.
Measuring your own context length distribution first
Sizing decisions made from the model's maximum supported context length rather than what users actually send routinely overprovision or underprovision hardware. The more reliable approach is logging prompt and response token counts from a pilot or comparable prior deployment, then sizing around the 95th-percentile length rather than either the average or the theoretical maximum, since a small number of unusually long requests should not dictate hardware for the entire fleet if they can be routed separately. Deployments that skip this step tend to either buy far more GPU memory than typical usage needs, or discover the gap only when a handful of long-context users degrade the service for everyone else.
Key takeaway: size context-driven memory around your measured 95th-percentile request length, not the model's maximum context or a rough average.
Frequently asked questions
Does prompt length or response length matter more for memory?
Both contribute to KV cache identically, since KV cache accumulates for every token processed, whether it came from the input prompt or was generated as output. A short prompt with a very long generated response consumes just as much KV cache as the reverse.
Can I set a hard limit on context length to protect memory?
Yes, and it is standard practice. Serving engines like vLLM expose a max-model-len setting that rejects requests beyond a configured token count, which protects the rest of the concurrent batch from being starved by one oversized request.
How does context length interact with time-to-first-token?
Longer prompts take longer to process during the prefill phase before the first token is generated, since prefill is compute-bound and scales with input length. This is a separate cost from KV cache memory, but both grow with context length and both need to be accounted for in latency targets.
Is there a context length where KV cache exceeds the model weights themselves?
Yes, and it happens sooner than most teams expect. For a 70B model in FP8 (~70 GB weights), a handful of concurrent 32K-token sessions already approaches the weight memory figure in KV cache alone, which is why long-context deployments often need proportionally more GPU memory than their weight size alone would suggest.
How Nanobase AI helps
Nanobase AI models this growth curve explicitly when sizing GPUs for long-context enterprise use cases, using the customer's actual expected distribution of prompt and conversation lengths rather than a single average figure. We also tune grouped-query attention, KV cache quantization and context limits as part of the deployment, detailed further in our on-premise LLM deployment guide. See our live demo to see this sizing process in action.
Ready to discuss your project? Contact Nanobase AI or email hello@bumu.tech.