A Practical Guide to Serving Million-Token Context LLMs
Long context windows are a capacity ceiling, not a free resource. This guide explains why KV Cache—driven by input length, output length, and concurrency—is the real constraint, and how to plan capacity from request length distributions. It outlines a four-tier serving strategy that isolates long-context workloads, plus the quotas, queues, and observability needed to keep cost and stability in balance.
Introduction: Window Size Is Not Usable Capacity
When an LLM advertises a million-token context window, many teams immediately assume they can finally stuff an entire codebase or a full year of logs into a single request. In production, however, these requests are often the first to hit timeouts, queueing, or outright service rejection. The reason is simple: a context window is a capability ceiling, not a resource quota you can spend freely. To use long context well, you first need to understand what it actually consumes, and then design your serving architecture around that.
1. Long Context Actually Consumes KV Cache
During autoregressive generation, the model must cache the keys and values of all previous tokens—this is the KV Cache. Its footprint is roughly proportional to three factors: input length, output length, and the number of concurrent requests.
This means two seemingly different workloads can exert nearly identical pressure: a single request approaching one million tokens, and several concurrent requests of a hundred-plus thousand tokens each, produce shocks of the same order of magnitude in both GPU memory usage and scheduling queues. More importantly, the KV Cache keeps growing throughout generation, so the output phase is where peak pressure comes from. Capacity planning based on input length alone will typically underestimate the real cost.
From an engineering standpoint, this leads to a few direct conclusions:
- GPU memory is a hard constraint; context length is a soft limit.
- Concurrency and per-request length must be modeled together, not evaluated separately.
- Workloads with uncontrollable output length need their own protective thresholds.
2. Profile Request Length Distribution Before Capacity Planning
Scaling decisions should not be based on the “maximum supported length” but on a real traffic profile. Start by collecting a request length histogram over a period of time, examining the P50, P90, and P99 percentiles, and separating input length from output length.
# Example: profile request length distribution to inform tiering strategybuckets = [(0, 32_000), (32_000, 128_000), (128_000, 1_000_000)]stats = {b: {"count": 0, "total_tokens": 0} for b in buckets}for req in request_stream:length = len(req.input_tokens) + len(req.output_tokens)for low, high in buckets:if low <= length < high:stats[(low, high)]["count"] += 1stats[(low, high)]["total_tokens"] += lengthbreak
In most workloads, the actual distribution shows a pronounced long tail: the vast majority of tasks fall within 32K, and only a few scenarios genuinely need ultra-long context. This finding drives the tiered design that follows.
3. Tiered Serving: Take Long Context Off the Main Path
Based on the length distribution, serving can be split into four tiers, each with its own resource pool and scheduling policy.
Tier 1: routine tasks within 32K. This tier has the highest share and should prioritize retrieval-augmented generation (RAG) and a shared serving pool, using cache reuse and batching to improve throughput while avoiding reserving too many resources for long context.
Tier 2: medium-to-long tasks from 32K to 128K. Apply quotas and a queueing mechanism, and cap per-tenant concurrency so that individual tasks cannot crowd out shared resources.
Tier 3: long-context tasks above 128K. Deploy these separately, physically isolated from the routine path, so long requests do not block the time-to-first-byte of short requests.
Tier 4: extreme tasks approaching one million tokens. These suit a low-concurrency dedicated queue combined with an asynchronous redesign, returning results through a message queue or object storage rather than making the caller wait synchronously.
Routine pool (<=32K) shared, high concurrency, low latencyMedium pool (32K-128K) quota + queueLong pool (>128K) dedicated instances, rate limitingDedicated queue (~1M) low concurrency, async callback
4. Quotas, Queues, and Observability
After tiering, stability depends on three things: quotas, queues, and monitoring.
Quotas should constrain both length and concurrency at the same time, avoiding either extreme of “a very long single request with low concurrency” or “short individual requests with extremely high concurrency.” Queues need a maximum wait time and priorities, with long tasks defaulting to low priority to prevent head-of-line blocking.
For observability, collect at least: request length distribution, KV Cache utilization, queue wait time, time-to-first-byte, and timeout and rejection rates. This data is both the basis for capacity scaling and the feedback source for quota tuning. Combining monitoring alerts with logging services helps establish baselines and issue early warnings when the share of long requests rises abnormally.
5. Common Pitfalls
- Pitfall 1: bigger window is always better. In reality, most tasks never use a long window, and maxing it out blindly only raises cost and lowers stability.
- Pitfall 2: looking only at input and ignoring output. Cache growth during the output phase is the real source of peak usage.
- Pitfall 3: using a synchronous API for ultra-long tasks. Long tasks should be made asynchronous to avoid holding connections open for a long time.
- Pitfall 4: scaling first, optimizing later. Doing RAG, caching, and tiering first is often more cost-effective than simply adding GPUs.
Conclusion
A million-token context is a capability that must be managed, not a benefit that is enabled by default. Starting from the request length distribution, using KV Cache as the capacity model, and applying tiered serving, quotas and queues, and observability as the implementation means, enterprises can balance cost and stability. With the general-purpose capabilities of cloud platforms such as Baidu AI Cloud, components like container platforms, message queues, object storage, and monitoring alerts can be combined into this tiered architecture—turning long context into a controllable source of productivity rather than a potential stability risk.
