1. Introduction
A context window defines the maximum number of tokens that a Large Language Model (LLM) can process at one time during training or inference. It represents the model’s working memory — everything the model can see, attend to, and reason over in a single forward pass. The context window is a hard architectural constraint and plays a crucial role in:
Long-document understanding
Conversational continuity
Reasoning quality
Retrieval-Augmented Generation (RAG) design
Cost, latency, and scalability of LLM systems
Anything that falls outside the context window effectively does not exist for the model.
2. Tokens and Tokenization
LLMs do not process raw text directly. They operate on tokens, produced by a tokenizer.
A token can be:
A whole word
A sub-word
A symbol or punctuation
Example:
"unbelievable"- ["un", "believ", "able"]Context window size is measured in tokens, not words or characters.
3. Formal Definition
Context Window
The maximum number of tokens that can participate in the model’s self-attention mechanism during a single forward pass.
If:
Context window size = N
Input tokens > N
Then:
Tokens beyond N are truncated
They do not influence output
The model has no awareness of them
4. Where the Context Window Exists (Architectural View)
The context window is not a software feature or memory buffer. It exists inside the Transformer architecture, specifically because of self-attention computation limits.
Key Insight
The context window exists because every token must attend to every other token.
This requirement creates a fundamental scalability limit.
5. Step-by-Step Internal Working
Step 1: Text → Tokens
Input:
"AI models use context windows"Tokenized:
[AI] [models] [use] [context] [windows]Assume:
Context window = 8 tokens
Input tokens = 5 → Allowed
Step 2: Tokens → Embeddings
Each token is converted into a dense vector.
Embedding Matrix Shape:(tokens × embedding_dimension)
Example:5 × 4096This matrix cannot exceed:
context_window × embedding_dimensionStep 3: Self-Attention (Why Context Window Is Limited)
For each token, the transformer creates:
Query (Q)
Key (K)
Value (V)
For N tokens:
Q = N × d
K = N × d
V = N × dAttention is computed as:
Attention = softmax(Q × Kᵀ / √d) × VCritical Limitation
Q × Kᵀ → produces an N × N matrixExample: In Attention, every token attended to every other token, so for 1, 000 token, (1,000 × 1000) are the total attention computation.
| Tokens (N) | Attention Computations |
|---|---|
| 1,000 | 1 million |
| 8,000 | 64 million |
| 128,000 | ~16 billion |
Quadratic growth (O(N²)) is the core reason context windows must be bounded.
6. Decoder-Only Models and Causal Masking
Decoder-only LLMs (GPT, Zephyr, Mistral) generate text left-to-right.
They use causal masking, which ensures:
A token can only attend to previous tokens
Future tokens are hidden

Join the conversation! Your thoughts help the community grow.