Million-Token Context Windows: Architecture, GPU Memory, and Deployment Considerations
A million-token context window enables a model to process roughly hundreds of thousands of Chinese characters in a single request. This article explains the technologies behind ultra-long context, estimates KV cache memory requirements, discusses latency challenges, and outlines when this capability is worth using.
What Enables Million-Token Context?
A million-token context window means that a model can accept approximately 1 million tokens as input in a single request. One Chinese character typically corresponds to one or two tokens, so this is roughly equivalent to several hundred thousand Chinese characters.
1. Core Technologies for Long-Context Processing
The computational cost of self-attention in a standard Transformer grows quadratically with sequence length N, with a complexity of O(N²). Expanding the sequence length from 4K to 1M tokens increases the length by 256 times and the attention workload by approximately 65,000 times. As a result, models require several supporting techniques to process extremely long inputs.
Position-encoding extrapolation. During training, a model may only see relatively short sequences, so its position encoding must be adapted to longer positions. A common approach is Rotary Position Embedding (RoPE), combined with techniques such as NTK-aware scaling or YaRN to adjust the base frequency. These methods require native support from the model, and excessive extrapolation can reduce performance on both short and long inputs. Before deployment, different extrapolation parameters should be compared on a validation set.
Sparse and local attention. Sliding-window attention, sparse attention, and global tokens reduce the range that each token needs to attend to. These approaches lower computational requirements but can weaken long-range dependencies. For this reason, many models combine local-attention layers with global-attention layers.
Distributed attention. A single GPU generally cannot hold the KV cache for a million-token sequence. Techniques such as Ring Attention divide the sequence across multiple GPUs. Each GPU computes local attention and passes Key and Value states through a ring-shaped communication pattern, allowing communication and computation to overlap. These methods depend on high-speed interconnects such as NVLink or InfiniBand. Without fast interconnects, communication can become the main bottleneck.
GPU Memory and Latency During Deployment
1. Estimating KV Cache Memory
The KV cache stores historical Key and Value states during inference. Its size can be estimated using the following formula:
KV cache memory in bytes ≈2 × number of layers × number of KV heads × head dimension× number of tokens × bytes per element
Assume a model has 80 layers, 8 KV heads through grouped-query attention, and a head dimension of 128. For a 1M-token context:
- FP16, with 2 bytes per element:
2 × 80 × 8 × 128 × 1,048,576 × 2 ≈ 343.6 GB
This is approximately 320 GiB. - FP8, with 1 byte per element:
approximately 171.8 GB, or 160 GiB.
These estimates do not include model weights, activations, or framework overhead. Therefore, a single million-token request may require multiple GPUs.
2. Optimizing Time to First Token
Time to first token (TTFT) is mainly determined by the prefill phase. Prefill must process the entire input, which can require substantial computation. Common optimization approaches include:
- Chunked prefill: Split a long input into smaller chunks for processing.
