Million-Token Context Windows: Attention, KV Cache, and Production Evaluation Costs
A million-token window describes an input limit, not a usable long-context capability. This article examines the engineering costs behind large windows: the growth of attention computation and KV-cache state, hybrid attention and compressed or tiered cache designs, numerical stability in long-sequence training and inference, application patterns that avoid sending everything into context, a four-part evaluation method, and a deployment checklist. Readers get a practical framework for deciding when long context is operationally and economically viable.
The Problem Is Not the Window, but the Usable Cost
As large models move into a phase where inference-time compute is traded for quality, context is no longer merely a container for prompts. A model must retrieve evidence from longer materials, maintain multi-step reasoning, and repeatedly revisit earlier state during generation. Labeling the maximum length as one million tokens only says that the interface can accept that many tokens. It does not answer three more important questions:
- Is the computation cost still acceptable for every additional generated token?
- Can the historical key-value state fit into existing GPU memory and caching systems?
- Are answers over very long inputs still stable, reproducible, and locatable?
Evaluating a context-capability update, therefore, should shift the topic from product messaging to engineering constraints: once length reaches the million-token scale, have attention computation, cache representation, and numerical stability in training and inference been redesigned together?
Why Standard Attention Hits the Wall First
During autoregressive decoding, the current position usually needs to establish relationships with the existing history. If every new token accesses all historical keys and values without distinction, per-token computation rises with history length. Attention cost across the whole sequence is closer to a quadratic relationship with length. When length grows from tens of thousands to one million, the additional burden is not simply “reading more text.” Every layer and every decoding step must perform another round of history access.
The key-value cache expands along with computation. Each layer must store keys and values for historical tokens, and the number of layers, number of heads, head dimension, and precision all affect the footprint. At million-token sequence lengths, the cache often becomes the bottleneck in GPU memory, bandwidth, and scheduling before the parameters themselves do. Prefill must fill the cache, decoding must repeatedly read it, and with concurrent requests the system must decide which requests stay resident and which are evicted. Increasing the window parameter without changing the representation that keeps full state for every historical token can easily lead to collapsing throughput, runaway time to first token, or long requests competing for the same resources.
It helps to view this as two curves rising at the same time:
- Computation curve: the longer the history, the more state must be scanned to generate the next token.
- Storage curve: the longer the history, the larger the cache that must remain resident or be quickly retrievable.
If either curve gets out of control, the million-token window is only a nominal capability.
Three Changes for Usable Long Context
The practical paths used in industry are not single-point optimizations. They put “compute less, store less, and train more stably” into one design.
Reduce Historical Access per New Token
Hybrid attention assigns different responsibilities to different layers or heads instead of forcing every layer to perform dense full-history association. Common combinations include local windows for nearby syntax and coreference, a small number of global or summary states for cross-segment topics, and retrieval-based or compressed states for distant evidence. In this design, most decoding steps touch only a controlled subset, while full-detail attention is reserved for positions that truly require long-range dependencies.
The tradeoffs must be explicit. If the window is too small, cross-segment constraints are easy to lose. If there are too few global states, a long document may “see the title but miss the clauses” in later sections. If compressed states update too aggressively, early critical facts can be overwritten. A hybrid architecture must therefore be paired with positional encoding, segment boundaries, and the training distribution rather than having a sparse mask applied only at inference time.
Make Cache Representation Grow Sublinearly with Length
The goal of cache optimization is to decouple the recoverability of historical information from its footprint. Common approaches include low-rank or quantized keys and values, merging across tokens, tiered page-based caching, and keeping hot state resident while fetching cold state on demand. During evaluation, do not look only at the compression ratio. Check three additional questions:
- Does recall degrade after compression?
- Does decoding bandwidth actually decrease?
- Does cache swapping introduce unacceptable jitter?
A useful order-of-magnitude estimate does not depend on a specific model; it can be derived from billing and hardware:
per-layer cache bytes ≈ sequence length × number of heads × head dimension × bytes per element × 2 (keys and values)total cache ≈ per-layer cache × number of layers
Use the p95 length of real business requests, not the maximum value advertised by the API. If, after compression or paging, p95 requests still exceed the cache budget of a single GPU or node, the length capability does not match the deployment shape. In that case, options include reducing precision, decreasing resident layers, or converting very long materials into a “retrieval plus short context” workflow.
Numerical Stability in Training and Inference
Very long sequences amplify positional extrapolation, attention overflow, and gradient noise. If training converges only on short sequences, suddenly running inference at million-token length often produces symptoms such as forgetting in later sections, repetitive generation, or excessive sensitivity to noisy snippets. Stable practice usually includes curriculum-style length extension during training, loss and regularization constraints on long sequences, and inference-side temperature, repetition penalties, and truncation strategies aligned with the training distribution. These are general engineering principles, not a proprietary switch for any particular model.
Application Design: Do Not Assume Everything Belongs in Context
A million-token window reduces segmentation cost, but it does not eliminate the need to organize information. For developers, a safer approach is to choose a context strategy based on the task.
- For contracts, specifications, and codebases—tasks that “must be checked against the source”—keep traceable snippets and explicitly provide section or symbol boundaries in the prompt so citations can be verified.
- For logs, tickets, and multi-turn sessions—materials with high redundancy—deduplicate, compress the timeline, and extract key fields before sending them to the model. This is usually more stable than concatenating everything raw.
- For materials whose knowledge changes or whose permissions vary, continue to use retrieval or permission filtering instead of freezing an entire corpus into a single request. Long context solves the visibility available to one inference call; it does not solve data freshness or access control.
For integration patterns, object storage is suitable for raw long documents, message queues are suitable for asynchronous prefill of partitioned input, container platforms can isolate resources by request length, and logs and monitoring should record input length, cache hits, time to first token, and generated-token rate. These are general capability categories. When selecting a design, look at whether it can rate-limit by length bucket rather than at interface slogans.
How to Evaluate Without Being Misled by Window Numbers
At minimum, run four comparisons using self-built, reproducible samples. Do not rely on leaderboard scores that cannot be audited.
- Length sweep: Test the same task at 8K, 32K, 128K, and the real upper bound for the business. Observe whether accuracy, latency, and failure rate degrade monotonically.
- Evidence position: Place the key fact at the beginning, middle, and end. Check whether “lost in the middle” becomes worse. Testing only the recency effect in short prompts overestimates long-context performance.
- Distractor density: Add incorrect sentences on the same topic around the correct passage. Observe whether the model recalls the right evidence or is led astray by nearby text.
- Cost loop: Record prefill time per thousand input tokens, decode time per output token, and cache occupancy under concurrency. If a quality improvement comes with unacceptable queueing, it cannot be considered usable.
Evaluation samples should use a fixed segmentation method and billing basis. Avoid a false improvement caused by measuring one run in characters and another in tokens. For code and tables, separately inspect whether structure is truncated, because token counts do not align with human reading length.
Deployment Checklist
Before launch, use the following questions as gates rather than checking only whether “long context is supported”:
- What are the p95 and p99 lengths for the business, and is the nominal limit far above real traffic?
- Can the cache byte budget per layer and per request be calculated clearly? When the limit is exceeded, will the system reject, degrade, or evict?
- Have sparse or compressed attention approaches been validated for recall on in-house data rather than only regression-tested on short samples?
- Are timeouts, retries, and idempotency designed for “heavy prefill and long-running decode”?
- Are permissions, redaction, and auditing completed before model input, so that very long text does not expand the exposure surface for sensitive information?
- Can monitoring explain latency by length percentile instead of looking only at average QPS?
Conclusion
Million-token context is a systems problem. Attention must do less unnecessary historical access. The cache must move from “full per-token residency” to state that can be managed in tiers. Training and evaluation must cover real lengths and evidence positions. The window parameter is only the entry point. Only by designing computation, storage, stability, and application organization together can long context move from a demonstration capability into a production inference budget.
