Why is long-context processing computationally expensive?
Long-context processing is computationally expensive because an LLM must process and keep track of many more token positions at the same time.
As the context grows, attention work, memory usage, KV-cache size, and latency all increase.
1. Self-attention becomes more expensive
In standard full self-attention, each token can interact with many other tokens in the sequence.
If the sequence length is (n), the number of pairwise attention relationships grows roughly like:
n × nSo doubling the context length can increase the attention work by much more than two times during full-sequence processing.
2. The model must process more token representations
Every additional token passes through the model's layers.
Longer sequences therefore require more computation for:
- Attention
- Feed-forward layers
- Normalization
- Other per-token operations
Even operations that scale roughly linearly with sequence length become more expensive when millions of token positions are involved across many layers.
3. KV-cache memory grows with context length
During autoregressive generation, LLMs commonly store key and value representations for previous tokens in a KV cache.
Conceptually:
more context tokens
→ larger KV cache
→ more memory usageA larger cache also increases the amount of data the hardware must read while generating later tokens.
4. Prompt processing becomes slower
Before generation begins, the model must process the input context.
A very long prompt therefore increases the prefill workload before the first output token can be generated.
This can increase time to first token.
5. Generating each new token can also become more expensive
During decoding, a newly generated token attends to previously cached context.
As the context becomes longer, there are more past positions to consider.
So long contexts can increase the work required for each subsequent output token as well.
6. Hardware bandwidth becomes important
Long-context inference is not only about arithmetic.
Large KV caches and intermediate tensors must be moved through GPU memory.
Memory bandwidth and available GPU memory can therefore become major bottlenecks.
How do LLMs reduce this cost?
Modern systems may use techniques such as:
- FlashAttention-style optimized attention
- Sliding-window attention
- Sparse attention
- KV-cache compression or quantization
- Grouped-query or multi-query attention
These techniques reduce memory or computation, but long contexts still require more resources than short ones.
For the memory side specifically, see “How does context length affect LLM memory usage?”
In short: long-context processing is expensive because more tokens create more attention work, larger intermediate states, bigger KV caches, greater memory traffic, and higher prompt-processing and generation latency.
Learning Sequence
Large Language Models Curriculum
Explore Related Questions
Deepen your understanding across related Large Language Models topics.