Inferensys

Glossary

Context Window

The maximum span of tokens a language model can attend to when generating a response, defining the boundary of its immediate working memory.
Engineer optimizing context window usage on laptop, token usage charts visible, technical work session.
IMMEDIATE WORKING MEMORY

What is a Context Window?

The context window defines the maximum span of tokens a language model can attend to when generating a response, establishing the boundary of its immediate working memory.

A context window is the finite sequence of tokens—typically measured in thousands or millions—that a transformer-based language model can process simultaneously via its self-attention mechanism. This window encompasses the system prompt, conversation history, retrieved documents, and the current query. Tokens falling outside this limit are irretrievably lost to the model during generation, making the window the hard boundary between accessible information and forgotten data.

The size of the context window directly dictates a model's ability to perform multi-turn reasoning and maintain session state. Expanding the window from 4K to 128K or 1M tokens allows ingestion of entire codebases or novels, but introduces the 'lost in the middle' phenomenon where models fail to attend to centrally positioned information. Engineers manage this constraint through contextual compression, KV-cache optimization, and strategic truncation to preserve high-priority instructions.

ARCHITECTURAL CONSTRAINTS

Key Characteristics of Context Windows

The context window defines the operational memory of a language model. Its size and management directly dictate the fidelity of multi-turn reasoning, the accuracy of retrieval-augmented generation, and the system's vulnerability to failure modes like context collapse.

01

Token Capacity & Hard Limits

The context window is defined by a strict token limit (e.g., 128k, 1M tokens), representing the maximum span of text the model can attend to at once. This is a hard architectural constraint, not a soft guideline. Exceeding this limit triggers context window truncation, where the oldest tokens are forcibly discarded. This process often severs the system prompt or initial instructions, leading to catastrophic forgetting of the agent's persona and behavioral constraints.

128K–2M
Typical Token Capacity
03

KV-Cache & Memory Optimization

During autoregressive generation, the model recomputes attention for all previous tokens unless a KV-Cache is utilized. This cache stores the Key and Value tensors of processed tokens, avoiding redundant computation. However, the KV-cache grows linearly with the context window, consuming significant GPU VRAM. Techniques like Prompt Caching store embeddings for a static system prefix, while Contextual Compression extracts only relevant snippets to minimize the cache footprint and reduce time-to-first-token latency.

O(n)
KV-Cache Memory Complexity
04

Causal Attention Masking

A Causal Attention Mask is a triangular matrix applied during self-attention to enforce autoregressive generation. It prevents the model from attending to future tokens, ensuring the prediction for token t depends only on tokens 0 through t-1. This is distinct from a general Attention Mask, which simply hides padding tokens. The causal mask is the fundamental mechanism that maintains temporal coherence in a dialogue, preventing the model from cheating by looking ahead in the conversation.

05

Context Poisoning & Injection Boundaries

The context window is a primary attack surface. Context Poisoning occurs when malicious data is injected into the conversation history or retrieval source to manipulate outputs. A Prompt Injection Boundary is the logical delimiter separating untrusted user input from trusted developer instructions. Without strict formatting via a Chat Template (like ChatML), the model cannot distinguish between a user's malicious command and the system's core directives, leading to security breaches.

06

Context Drift & Collapse

Over long multi-turn interactions, Context Drift occurs as the conversation gradually deviates from the original intent, causing the model to lose focus. A more severe failure state is Context Collapse, where the model loses the distinction between different conversational threads or temporal states, flattening the dialogue into an incoherent prompt. This often results from poor Dialogue State Tracking (DST) or exceeding the window's effective reasoning capacity, not just its token limit.

TOKEN CAPACITY COMPARISON

Context Window Sizes Across Major Models

Maximum token limits for leading large language models as of mid-2025, comparing native context windows and effective retrieval performance.

ModelMax TokensApprox. WordsEffective RetrievalNotes

GPT-4o

128,000

~96,000

Native 128K; strong recall across full window

Claude 3.5 Sonnet

200,000

~150,000

Near-perfect recall up to 200K tokens

Gemini 1.5 Pro

2,000,000

~1,500,000

Full recall degrades; 'lost in the middle' above 1M

Llama 3.1 405B

128,000

~96,000

Open-weight; RoPE scaling to 128K

Claude 3 Opus

200,000

~150,000

Strong long-document QA performance

GPT-4 Turbo

128,000

~96,000

Predecessor to GPT-4o; reliable at 128K

Mistral Large 2

128,000

~96,000

Multilingual; strong at full context

Command R+

128,000

~96,000

Optimized for RAG; retrieval dips beyond 64K

CONTEXT WINDOW

Frequently Asked Questions

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

A context window is the maximum span of tokens a language model can attend to when generating a response, defining the boundary of its immediate working memory. It encompasses the entire input—system prompt, conversation history, retrieved documents, and the current user query—that the model can reference simultaneously. Architecturally, the context window is constrained by the model's positional encodings and the quadratic complexity of the self-attention mechanism. When the total token count exceeds this limit, context window truncation occurs, typically discarding the oldest tokens first, which can lead to catastrophic forgetting of initial instructions or early dialogue turns. Modern frontier models offer context windows ranging from 128K to over 1 million tokens, enabling processing of entire codebases or novel-length documents in a single forward pass.

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.