Long-Context Inference: Costs, KV Caching, and Engineering Considerations
Long-context inference requires more than a large context window: it must also be affordable, fit in memory, and respond quickly. This article explains how to interpret reported model metrics, distinguish per-token costs from total task costs, assess the engineering trade-offs of sparse activation, and estimate KV cache memory requirements. Hypothetical calculations show why lower per-token compute does not necessarily mean cheaper tasks—and why cache optimization matters at million-token context lengths.
1. Understanding the Reported Metrics
Recent public disclosures describe several metrics for a next-generation large language model: approximately 1.6 trillion total parameters, around 49 billion parameters activated per inference token, KV cache memory usage at about 10% of the previous generation under a one-million-token context window, and per-token inference compute cost at roughly 27% of the previous generation.
These figures come from public disclosures. The specific model, release date, and test conditions should be checked against the vendor’s latest official documentation. This article does not independently verify the figures; it uses them only to illustrate estimation methods.
First, distinguish the four metrics:
- Total parameters: All learnable weights in the model. This affects model storage requirements and knowledge capacity.
- Active parameters: The weights that actually participate in computation for a given token. This affects computational workload.
- KV cache memory usage: The GPU memory used to store keys and values for previously processed tokens during inference. It grows with context length.
- Per-token compute cost: The average computational cost of processing a token, often expressed relative to the previous model generation.
The bottleneck in long-context inference is shifting from whether the computation is possible to whether it is affordable, fits in memory, and delivers acceptable response times.
2. Separate Per-Token Cost from Total Task Cost
A lower per-token cost does not necessarily mean that the same budget can process more text in a complete task.
Suppose a task uses eight times as many tokens as before, while per-token compute cost falls to 27% of the previous level. The total task cost becomes approximately 2.16 times the original cost:
8 × 0.27 = 2.16
Cost discussions therefore need two distinct measures:
- Unit cost: The computational cost per input or output token, which measures model and system efficiency.
- Task cost: The total cost of completing a task, which depends on the total number of tokens required.
Engineering decisions should be based on task cost. Sending an entire long document to the model may cost several times as much as retrieving relevant passages first and sending only those passages. The appropriate choice depends on the use case.
3. Sparse Activation: Total Parameters Are Not Per-Token Compute
A common sparse-activation architecture is Mixture of Experts (MoE). The model contains multiple expert networks, and a routing network selects only a small subset of experts to process each token.
Using the reported figures above, the active-parameter share per token is:
49 billion / 1.6 trillion ≈ 3.06%
This allows the model to retain a large knowledge capacity while limiting the computational workload of each inference step.
The routing process can be represented with pseudocode:
Input: Token hidden vector h1. Compute expert scores:scores = Router(h)2. Select the k highest-scoring experts:selected = TopK(scores, k)3. Compute the weighted sum of selected expert outputs:output = Σ weight_i * Expert_i(h)// Only experts where i ∈ selected participate in computation.4. Return output
The value of k varies by model and is commonly a small integer between 1 and 8. A capacity factor limits the number of tokens each expert can receive in a processing step. Tokens beyond that limit are typically dropped or rerouted. Load-balancing strategies, such as an auxiliary loss, help distribute tokens more evenly across experts.
Sparse activation also introduces engineering costs:
- Expert weights still require storage. All expert weights must remain in GPU memory or system memory, so storage requirements do not decrease in proportion to the active-parameter ratio.
- Uneven workloads can slow inference. Overloaded experts can become bottlenecks for an entire inference batch.
- Distributed experts require communication. When experts are spread across multiple GPUs, each relevant layer must dispatch token representations across devices and combine the results.
A rough estimate of communication volume per token per layer is:
k × hidden dimension × bytes per element
Multiply this by the number of layers and tokens to estimate the overall volume. Actual communication also depends on how experts are distributed across devices.
Evaluating a sparse model therefore requires more than its active-parameter count. Memory usage, communication overhead, and load balancing also matter.
4. KV Cache: A Core Bottleneck for Long Contexts
During autoregressive generation, each new token requires access to the keys and values of earlier tokens. These stored intermediate results form the KV cache. As the context grows, so does the cache.
Its memory footprint can be estimated as follows:
KV cache bytes ≈2× number of layers× number of KV heads× head dimension× context length× bytes per element× batch size
Consider the following hypothetical configuration. These values do not represent any specific model:
- Number of layers: 60
- Number of KV heads: 8
- Head dimension: 128
- Context length: 1,000,000 tokens
- Bytes per element: 2, using FP16
- Batch size: 1
Substituting these values gives:
2 × 60 × 8 × 128 × 1,000,000 × 2≈ 2.46 × 10^11 bytes≈ 246 GB, or approximately 229 GiB
This estimate covers only the KV cache. It excludes model weights, activations, GPU memory fragmentation, and inference-framework overhead.
Even without those additional requirements, the cache for a single request exceeds the memory capacity of most individual GPUs. Long-context serving therefore requires cache optimization.
Common approaches to reducing KV cache memory include:
- Reducing the number of KV heads: Using multi-query attention (MQA) or grouped-query attention (GQA).
