Where Did the Compute for Million-Token Contexts Go? A Developer's Guide to Cost Optimization
Since the second half of last year, the context windows of Large Language Models (LLMs) have rapidly expanded, with some frontier models supporting million-token inputs. However, many developers have found that bills and quota consumption deplete much faster than expected. This article explores why a drop in API unit prices hasn't translated into lower overall costs, tracing the root causes back to the inherent resource consumption of long contexts. It breaks down the primary cost drivers—KV Cache memory footprint and quadratic attention computation—and explains why token-based billing and monthly subscription quotas don't fully reflect actual resource usage. Finally, it provides actionable advice for developers on reviewing their bills and avoiding common application-layer inefficiencies.
Since the second half of last year, the context windows of Large Language Models (LLMs) have rapidly expanded, with some frontier models supporting million-token inputs. While tasks like repository analysis, long contract review, and whole-book Q&A have become feasible, many developers quickly noticed that bills and quota consumption deplete much faster than expected. Although some platforms have lowered API unit prices, many users on monthly subscription plans still complain about limited quotas. The drop in unit price hasn’t translated into a synchronized decrease in overall costs, primarily due to the inherent resource consumption of long contexts.
This article starts from the inference mechanism, explains where the costs originate, and provides actionable optimization methods for developers.
1. Costs Mainly Come from KV Cache and Attention Computation
When generating text, an LLM reads the Key and Value vectors of all preceding tokens for every new token generated. To avoid redundant calculations, the inference engine caches these vectors in GPU memory. This cache is known as the KV Cache.
The memory footprint of the KV Cache can be estimated using the following formula:
Memory ≈ 2 × Layers × KV Heads × head_dim × Sequence Length × Bytes per Element
Assuming a fixed number of layers, heads, and dimensions, the memory grows almost linearly with the sequence length. Let’s estimate with a hypothetical configuration: 80 layers, 8 KV heads, a head_dim of 128, using FP16 storage. Each token occupies about 320 KB of memory; 100,000 tokens require around 33 GB, and 1 million tokens require about 330 GB. This means a single million-token request could exhaust the memory of one or more compute cards, subsequently reducing the number of concurrent requests a single card can serve.
Attention computation is another major cost driver. The computational complexity of standard global self-attention is proportional to the square of the sequence length. During the prefill phase, the model must process all inputs and compute attention between every pair of input tokens before generating the first output token. Therefore, even if a user simply inputs a long document and asks a basic question, the model must first complete the prefill for the entire document. This waiting period is known as the “Time to First Token” (TTFT) latency.
For the same total number of tokens, how they are segmented also affects the cost. Taking full attention computation as an example, a single 1 million-token request has about 10 times the attention computation overhead of ten 100,000-token requests, because 1,000,000² compared to 10 × 100,000². This ratio holds true only when caching cannot be reused and batching is not possible. Actual costs also depend on the ratio of prefill to decoding length, batching efficiency, and cache hit rates, which require empirical testing based on your specific workloads.
2. Why Unit Prices Drop, But Quotas Are Still Insufficient
API billing is typically calculated based on the number of input and output tokens. While token count reflects the request’s scale, it doesn’t fully represent resource consumption. For two inputs of 10,000 tokens, one request might be a continuation based on existing cached data, while the other might be a brand new long document. The difference in GPU time consumed between these two scenarios is significant.
Monthly subscription quotas are usually designed based on average workloads. When a large number of users initiate long-context requests simultaneously, the platform must bear a higher instantaneous load. To ensure service stability, platforms may implement rate limiting, queuing, or allocate quotas based on concurrency. As a result, users may feel that their quotas are “insufficient” or that responses are slower.
When reviewing their bills, developers can check line by line: whether input and output unit prices differ; whether cache-hit inputs have a separate price; whether inference tokens from reasoning models are counted as output; and whether failed or timed-out requests are charged. If the platform’s rules indeed involve multipliers or additional deductions, this should be clearly stated in the official billing documentation, allowing developers to adjust their API call strategies accordingly.
The most common waste occurs at the application layer. Sending the entire conversation history or full document with every request, even when the model only requires a small portion of the information, leads to unnecessary resource consumption. Developers should optimize their prompt engineering and context management to mitigate these hidden costs.
