Long-Context LLM Serving in Practice: From KV Cache to Capacity Planning
Why does a model that "supports 1M tokens" not give every request a 1M-token window by default? This article explains the real cost structure of long-context inference, focusing on how KV Cache consumption scales with input length, output length, and concurrency. It walks through why long-context windows are usually opt-in rather than default, how to measure your actual request length distribution before provisioning resources, and which capacity planning and scheduling techniques—bucketed load testing, admission control, request isolation, prefix caching, and output length limits—help balance performance, cost, and stability in production.
Long-Context LLM Serving in Practice: From KV Cache to Capacity Planning
1. Why “Supports 1M” Doesn’t Mean “1M by Default”
Many teams, when adopting an LLM service, see that the provider claims support for 1M-token context and assume every request can freely use the full window. In production, however, long context is usually not the default serving window—it’s a capability that must be explicitly requested or enabled through specific configuration. This isn’t an arbitrary product restriction; it’s an inevitable consequence of resource economics.
To understand why, look at the cost structure of inference serving. GPU memory on an inference cluster is a hard constraint. Once model weights are resident in memory, the remaining allocatable space is mainly used for the KV Cache—the Key and Value tensors cached per token in the attention mechanism. The context window size directly determines the theoretical upper bound of GPU memory a single request can consume. If every request reserved resources for the maximum window, cluster concurrency and throughput would collapse, with most memory tied up in idle “reserved space” that can never be amortized.
The sensible approach, therefore, is to provide a default serving window that covers most workloads (e.g., 128K), and handle requests that genuinely need ultra-long context separately. This is both resource optimization and a safeguard for service stability.
2. The Real Cost of Long Context: The KV Cache Amplification Effect
Many developers intuit the cost of long context only in terms of “token pricing,” but the real engineering pressure comes from the KV Cache. The amplification effect can be understood along three dimensions:
2.1 Input Length: The Dominant Linear Term
KV Cache memory usage is proportional to token count. For a typical dense attention architecture, the KV memory per token can be approximated as:
KV memory per token ≈ 2 × layers × KV heads × head dim × bytes per element
For a mid-sized model with 40 layers, 8 KV heads, head dimension 128, and FP16 precision:
2 × 40 × 8 × 128 × 2B = 163,840B ≈ 160KB
A single 1M-token request would then need roughly 160GB of KV Cache alone—already beyond the memory of a single mainstream accelerator, requiring multi-GPU parallelism or tiered storage. This is why ultra-long-context requests are “heavy workloads” rather than ordinary traffic for the infrastructure.
2.2 Output Length: The Underestimated Increment
Every newly generated token during decoding appends to the KV Cache, and decoding is serial, token by token. Long outputs therefore significantly extend the wall-clock time a request occupies memory. A request with “10K input, 50K output” can hold memory far longer than one with “60K input, 1K output,” even when the total token count is identical. Capacity planning cannot rely on input length distributions alone.
2.3 Concurrency: The Multiplier on Memory Pressure
Memory pressure ≈ per-request KV footprint × concurrent requests. This means two very different load profiles can produce identical resource pressure:
- A small number of near-1M ultra-long requests;
- A large volume of concurrent 128K regular long-text requests.
The latter often stresses the serving queue more, because requests arrive more frequently and the queuing structure is more complex. The former is extremely heavy per request but low in volume and more predictable.
3. The First Step Before Production: Profile Your Request Length Distribution
Before purchasing resources or designing gateway rate-limiting policies, every organization should answer one question: what does our actual request length distribution look like? Talking about capacity without a distribution is like talking about bandwidth without traffic—pure guesswork.
Recommended metrics to collect:
- Input length distribution: Where do P50, P90, and P99 fall? Are there long-tail ultra-long requests?
- Output length distribution: What are the average and maximum outputs? Are there scenarios generating long documents or long code?
- Concurrency profile: Peak daily QPS, average per-request processing time, timeout and failure rates.
- Scenario breakdown: Document summarization, code completion, and multi-turn conversations have very different length characteristics—model them separately.
In practice, you can instrument the gateway layer and aggregate daily histograms. A simple bucketing example:
buckets = [4_000, 16_000, 32_000, 64_000, 128_000, 256_000, 1_000_000]def bucket_of(tokens):for b in buckets:if tokens <= b:return breturn f">{buckets[-1]}"# Aggregate daily request logs by input_tokens / output_tokens separately# Output the share and P99 of each bucket as input for capacity planning
With the distribution in hand, you can make three kinds of decisions:
- Window policy: If 95%+ of requests fit within 128K, setting the default window to 128K and routing longer requests through a dedicated channel offers the best cost-effectiveness;
- Memory budget: Estimate per-request KV footprint from P99 input + output length, multiply by target concurrency, and derive peak memory requirements;
- Rate limiting and quotas: Set independent concurrency caps and queuing policies for ultra-long requests so they don’t crowd out throughput for regular traffic.
4. Capacity Planning and Scheduling Recommendations
Based on the length distribution, the following engineering practices can be put in place:
Bucketed load testing. Build test traffic from your real distribution rather than uniform fixed-length requests. The impact of long-tail requests on the scheduler only becomes visible once real proportions are mixed in.
Request-level admission control. Inference frameworks typically support dynamic batching based on remaining memory, but the application layer should still enforce token budget caps to prevent ultra-long requests from breaching the memory watermark during peak hours and triggering cascading preemption.
Isolation of long and short requests. Route ultra-long requests to a separate serving group with more generous timeouts and lower concurrency caps, physically isolated from regular traffic so neither drags the other down.
Prefix cache reuse. Multi-turn conversations and repeated questions over the same long document share large repeated prefixes. Leveraging prefix caching can significantly reduce redundant KV computation—it’s one of the most important optimizations for long-context workloads.
Output length constraints. State the expected output length clearly in the prompt and set a reasonable maximum output limit via parameters, so occasional runaway generations don’t hold resources indefinitely.
5. Conclusion
The value of long context lies in business adoption, and adoption depends on understanding its resource model. The core takeaways boil down to three points. First, “supports 1M” is a capability ceiling, not a default service commitment—ultra-long windows require dedicated planning. Second, the real cost of long context is the KV Cache, with input length, output length, and concurrency jointly determining memory pressure. Third, before scaling up, organizations must profile their actual request length distribution and design window policies, rate-limiting rules, and isolation schemes on that basis. Grounding capacity planning in data is what makes it possible to balance quality, cost, and stability.
