Context Length & Window Limits

AI & Machine Learning Foundations beginner How AI Works 11 min read

Overview

The context window (or context length) is the maximum number of tokens a language model can process in a single forward pass — encompassing both the input prompt and the generated output. It is one of the most practically important constraints in AI engineering because it determines how much information you can feed to the model at once: your system prompt, conversation history, retrieved documents, and the model’s response all compete for space within this fixed budget.

The fundamental reason context windows have hard limits is rooted in the Transformer’s self-attention mechanism. Because every token must attend to every other token, computation and memory scale quadratically with sequence length. Doubling the context window requires roughly 4× the compute and 4× the memory for the attention layers alone. This O(n²) scaling creates physical constraints that no amount of engineering has fully eliminated.

For AI engineers building production systems, context window limits drive critical architectural decisions: how much text to retrieve in a RAG pipeline, when to use summarization vs. raw context injection, how to manage multi-turn conversations without exceeding capacity, and how to budget tokens across system prompts, few-shot examples, and user data.

How It Works

The Quadratic Scaling Problem

Self-attention computes an n × n attention score matrix for every head in every layer. Here’s what that looks like at scale:

Context Length    Attention Matrix Size    Relative Compute
512 tokens       262,144 entries          1×
2,048 tokens     4,194,304 entries        16×
8,192 tokens     67,108,864 entries       256×
32,768 tokens    1,073,741,824 entries    4,096×
128,000 tokens   16,384,000,000 entries   62,500×
1,000,000 tokens 1,000,000,000,000       ~3,800,000×

Each entry requires computation (dot product, softmax) and memory (storing attention weights for the backward pass during training, or for the KV cache during inference). This is why a 128K context request costs dramatically more than a 4K request — the model is doing orders of magnitude more work.

Key Concept — O(n²) Attention Cost

For a Transformer with L layers and H attention heads, the total attention computation for a sequence of length n is proportional to L × H × n². This quadratic factor is the single biggest driver of context window limitations, inference latency, and API pricing.

Memory: The KV Cache

During inference, the model maintains a Key-Value (KV) cache — storing the Key and Value vectors for all previously generated tokens so they don’t need to be recomputed at each generation step. The KV cache grows linearly with sequence length, but for large models it becomes enormous:

KV Cache Size = 2 × num_layers × num_heads × head_dim × seq_length × bytes_per_param

Example (70B parameter model, 128K context, FP16):
= 2 × 80 × 64 × 128 × 128,000 × 2 bytes
≈ 167 GB of GPU memory just for the KV cache

This memory requirement is why serving long-context requests requires multiple high-end GPUs and why providers charge premium pricing for extended context windows.

Context Window Sizes Across Models (2026)

The context window arms race has pushed limits dramatically higher, though effective utilization often lags behind advertised capacity:

However, research consistently shows that effective context utilization is only 60-70% of the advertised limit. Beyond that threshold, models exhibit degraded recall, “lost in the middle” effects, and increased hallucination rates.

The “Lost in the Middle” Problem

A landmark study by Liu et al. (2023) demonstrated that LLMs struggle to use information placed in the middle of long contexts. Models perform best when relevant information is near the beginning or end of the prompt, and worst when it’s buried in the middle. This has direct implications for how you structure RAG prompts — placing your most important retrieved chunks first or last in the context significantly improves output quality.

Attention Dilution

As the context window fills with tokens, each token’s attention budget gets spread across more positions. Critical information competes with noise for the model’s attention weights. This “attention dilution” means that simply having a large context window does not guarantee the model will effectively use all the information in it.

Techniques to Extend Context

Researchers and engineers have developed several approaches to push beyond the quadratic limit:

Sparse Attention

Instead of every token attending to every other token, sparse attention patterns restrict which token pairs can interact. Common patterns include:

Linear Attention Approximations

Architectures like Linformer and Performer approximate the attention matrix using low-rank projections or kernel methods, reducing complexity from O(n²) to O(n). However, these typically sacrifice some accuracy compared to full attention, and most frontier models still use standard quadratic attention.

FlashAttention

FlashAttention (Dao et al.) doesn’t change the mathematical complexity of attention — it’s still O(n²) — but it dramatically reduces memory usage and improves speed by restructuring the computation to be IO-aware. Instead of materializing the full n × n attention matrix in GPU memory, it computes attention in blocks (tiles), keeping intermediate results in fast SRAM. FlashAttention 2 and 3 further optimize with better parallelism and hardware utilization. This is the standard attention implementation used in most production LLMs today.

Positional Encoding Extensions

Models trained with a fixed context length can sometimes be extended to longer contexts by modifying their positional encodings. RoPE (Rotary Position Embedding) scaling techniques like YaRN and NTK-aware scaling can extend a model trained at 4K context to operate at 32K or beyond with minimal fine-tuning, though with some quality degradation at the extremes.

Practical Applications

Token Budgeting for RAG Systems

In a production RAG pipeline, your context window budget must be allocated across competing needs:

Total Context Budget: 128,000 tokens

System Prompt:          ~2,000 tokens  (1.5%)
Conversation History:   ~8,000 tokens  (6.3%)
Retrieved Context:     ~80,000 tokens  (62.5%)
Reserved for Output:   ~16,000 tokens  (12.5%)
Safety Buffer:         ~22,000 tokens  (17.2%)

If your retrieved context exceeds the budget, you must either reduce the number of chunks retrieved, apply summarization, or use a reranker to select only the most relevant passages. Overflowing the context window silently truncates tokens in most APIs, potentially losing critical information.

Cost Implications

API pricing is directly tied to token count. Processing a 128K token prompt is significantly more expensive than a 4K token prompt — both in input token costs and in the additional compute required. For high-volume production systems, aggressive context management can reduce costs by 10-50× compared to naively stuffing the maximum context.

Multi-Turn Conversation Management

As conversations grow, prior messages consume an ever-larger portion of the context window. Production chat systems typically implement one or more strategies:

When to Use Long Context vs. RAG

The availability of 1M+ token context windows has raised the question: do we still need RAG? The answer is nuanced:

Key Takeaways

Further Reading