Inferensys

Glossary

Context Window

The maximum number of tokens a large language model can process in a single forward pass, defining the upper limit of its immediate working memory.
Engineer optimizing context window usage on laptop, token usage charts visible, technical work session.
DEFINITION

What is a Context Window?

The context window defines the maximum number of tokens a large language model can process in a single forward pass, establishing the upper limit of its immediate working memory.

A context window is the maximum span of input tokens—including the prompt, conversation history, and any retrieved documents—that a large language model can attend to simultaneously. It represents the model's active working memory during inference, measured in tokens rather than words. Any information beyond this hard limit is truncated or lost, making the window size a critical constraint for tasks requiring long-form reasoning or multi-turn dialogue.

Expanding the context window involves overcoming the quadratic computational complexity of the standard attention mechanism. Techniques like RoPE Scaling, Flash Attention, and Ring Attention enable efficient processing of extended sequences by optimizing memory access patterns and distributing computation. The KV cache stores pre-computed key-value tensors for previous tokens, enabling autoregressive generation without recomputation, but its memory footprint grows linearly with context length.

DEFINING THE WORKING MEMORY OF LLMS

Key Characteristics of Context Windows

The context window is the fundamental constraint on a model's immediate reasoning capacity. It defines the total number of tokens—words, sub-words, or characters—that can be processed in a single forward pass, acting as the model's short-term memory.

01

Token Capacity & Limits

The context window is measured in tokens, not words. A token is roughly 0.75 words in English. A model's maximum context length is a fixed architectural constraint determined during training.

  • GPT-4 Turbo: 128k tokens (~96,000 words)
  • Claude 3: 200k tokens (~150,000 words)
  • Gemini 1.5 Pro: Up to 1M tokens in research previews
  • Llama 3: 8k tokens natively

Exceeding this limit requires truncation or chunking strategies, which can break semantic coherence.

128k+
Standard Max Tokens (2024)
~0.75
Words per Token (English)
02

The 'Lost in the Middle' Phenomenon

Models do not attend to all parts of a long context equally. Research shows a U-shaped attention curve: information at the very beginning and very end of a prompt is recalled most accurately, while content in the middle is frequently overlooked.

  • Primacy Bias: Strong recall of initial instructions
  • Recency Bias: Strong recall of final statements
  • Middle Degradation: Significant performance drop for facts placed between 25% and 75% of the context length

This necessitates strategic prompt engineering, placing critical instructions either at the start or the end of the context.

25-75%
High-Risk Zone for Information Loss
03

Positional Encoding Mechanics

Transformers are inherently permutation-invariant; they have no built-in sense of sequence order. Positional encodings are vectors added to token embeddings to inject order information.

  • Absolute Positional Encoding (APE): Assigns a unique vector to each position (e.g., original Transformer). Limited to the max length seen during training.
  • Rotary Position Embedding (RoPE): Encodes relative position by rotating query and key vectors. Enables better length generalization and is used in Llama, Mistral, and PaLM.
  • ALiBi: Adds a linear bias to attention scores based on distance. Allows extrapolation to unseen lengths without fine-tuning.
04

Context Window Extension Techniques

Extending a pre-trained model's context window without full retraining is a major area of research. Key methods include:

  • RoPE Scaling: Modifies the rotary frequencies to compress long-range positions into the original training range. Techniques include Linear Scaling, NTK-aware Scaling, and YaRN.
  • Position Interpolation: Linearly downscales position indices so that a longer sequence fits within the original context length's positional range.
  • Sparse Attention: Uses sliding windows or dilated patterns to reduce the quadratic complexity of full attention, enabling longer sequences (e.g., Longformer, BigBird).
05

Computational Complexity & Memory

The standard self-attention mechanism has quadratic complexity O(n²) with respect to sequence length n. This creates a hard bottleneck for long context windows.

  • KV Cache Growth: The memory required to store Key and Value tensors grows linearly with sequence length. A 1M token context can consume hundreds of GB of VRAM for the cache alone.
  • FlashAttention: An IO-aware algorithm that tiles computation to avoid materializing the full NxN attention matrix in slow HBM, reducing memory reads/writes by up to 7.6x.
  • Ring Attention: Distributes the long sequence computation across multiple devices in a ring topology, effectively scaling context length linearly with the number of GPUs.
O(n²)
Attention Complexity
~450 GB
KV Cache for 1M Tokens (FP16)
06

Needle In A Haystack Evaluation

The standard benchmark for long-context retrieval is the Needle In A Haystack (NIAH) test. A specific fact (the 'needle') is placed at a random depth within a long, irrelevant document (the 'haystack'), and the model is queried to retrieve it.

  • Pressure Testing: Evaluates recall accuracy across varying context lengths and needle positions.
  • RULER Benchmark: An extension by NVIDIA that adds multi-needle, variable tracking, and aggregation tasks to stress-test context utilization beyond simple retrieval.
  • Practical Implication: High NIAH scores do not guarantee robust reasoning over the entire context, only retrieval.
TOKEN CAPACITY COMPARISON

Context Window Sizes Across Popular Models

A comparison of maximum context window sizes for prominent open-weight and proprietary large language models, illustrating the rapid expansion of working memory in modern architectures.

ModelMax TokensPosition EncodingOpen WeightsRelease Year

GPT-4 Turbo

128,000

RoPE

2023

Claude 3 Opus

200,000

Custom Rotary

2024

Gemini 1.5 Pro

1,000,000

RoPE

2024

Llama 3.1 405B

128,000

RoPE (Base 500,000)

2024

Mistral Large 2

128,000

RoPE

2024

Command R+

128,000

RoPE

2024

GPT-3.5 Turbo

16,385

Learned Absolute

2022

Llama 2

4,096

RoPE

2023

CONTEXT WINDOW

Frequently Asked Questions

Clear, technically precise answers to the most common questions about the working memory limits of large language models.

A context window is the maximum number of tokens a large language model can process in a single forward pass, defining the upper limit of its immediate working memory. It encompasses the entire input prompt, any system instructions, conversation history, and the model's generated output. The window is measured in tokens, not words or characters, and a single word can map to multiple tokens depending on the tokenization method. When the combined input and output exceed this limit, the model loses access to the earliest information, a phenomenon often called the 'lost middle' problem. Modern models range from 4,096 tokens in early GPT-3.5 to over 1 million tokens in Google's Gemini 1.5 Pro, representing a massive expansion in short-term memory capacity.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.