Skip to main content

Context Window

#Context Window

Overview​

Context Window is the maximum number of Tokens that the large language model can memorize and process in a single conversation. It determines how much information the model can "see" and "understand".

Simple understanding: Context window = "short-term memory capacity" of the model


Core concepts​

1. Composition of context window​

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Context Window β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ System prompts β”‚ β”‚Conversation history β”‚ β”‚ User β”‚ β”‚
β”‚ β”‚ System β”‚ β”‚ History β”‚ β”‚ Query β”‚ β”‚
β”‚ β”‚ Prompt β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚ β”‚ β”‚
β”‚ β–Ό β–Ό β–Ό β”‚
β”‚ [========|============|============] β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚Total number of Tokens ≀ context window β”‚
β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

2. Input vs output window​

TypeDescription
Input windowThe maximum number of Tokens that the model can receive
Output WindowThe maximum number of Tokens that the model can generate (usually < input)

##Context window of mainstream model

Very long context model (>500K)​

modelcontext lengthrelease time
Gemini 2.0 Pro2M tokens2025.02
Gemini 1.5 Pro1M tokens2024.02
GPT-5.21M tokens2024.12
Claude 3200K tokens2024.03

Standard context model (100K-200K)​

modelcontext length
Claude Opus 4.5200K
Claude Sonnet 4.5200K
GPT-4o128K
GLM-4.7128K

Medium context model (32K-100K)​

modelcontext length
GPT-432K / 8K
Claude 2100K
Llama 3.1128K

Small context model (<32K)​

modelcontext length
GPT-3.516K / 4K
Original Claude9K / 72K
Llama 24K

Evolution of context window​

2020 ────── 2022 ────── 2024 ────── 2026
β”‚ β”‚ β”‚ β”‚
2048 β†’ 32K β†’ 128K β†’ 2M
(GPT-3) (GPT-4) (GPT-4 Turbo) (Gemini)

Key Milestones:

timemodelwindow sizemeaning
2020.06GPT-32KFirst large-scale application
2023.03GPT-432KLong text processing
2023.07Claude 2100KExtra long context
2024.02Gemini 1.51MMillion level breakthrough
2025.02Gemini 2.02MCurrent largest

The role of the context window​

1. Code analysis​

Project size Required window Recommended model
────────────────────────────────────
Single file 1K any
Small projects 50K-100K Claude, GPT-4
Medium-sized projects 200K-500K Gemini 1.5
Large Projects 1M+ Gemini 2.0 Pro
Full library analysis 2M+ Gemini 2.0 Pro (needs to be divided into blocks)

2. Document processing​

Document TypeLengthRequired Model
Short article<5K tokensAny
Long article20-50K tokensClaude 3, GPT-4
Books100-500K tokensGemini 1.5, Claude 3
Legal Documents500K-1M tokensGemini 2.0 Pro

3. Multiple rounds of dialogue​

Number of dialogue rounds Average tokens/round Total tokens Model required
────────────────────────────────────────
10 rounds 500 5K any
50 rounds 500 25K Claude, GPT-4
100 rounds 500 50K Claude 3
500 rounds 500 250K Gemini 1.5+

Context management technology​

1. Sliding window​

Original context: [A][B][C][D][E][F][G][H][I][J]
Window size: 4

Step 1: [A][B][C][D]
Step 2: [B][C][D][E]
Step 3: [C][D][E][F]
Step 4: [D][E][F][G]
...

2. Importance sampling​

Full context: [A][B][C][D][E][F][G][H][I][J]
Importance: ↑ ↑ ↑ ↑
Reserved: [A][C][E][G][I]

3. Block processing​

Large document: [==== ==== ==== ==== ==== ====]
↓
Blocking: [Block 1][Block 2][Block 3][Block 4][Block 5][Block 6]
↓
Summary: [Abstract 1][Abstract 2][Abstract 3][Abstract 4][Abstract 5][Abstract 6]
↓
Final: [Comprehensive summary]

4. Vector retrieval (RAG)​

User question β†’ Vectorize β†’ Retrieve relevant fragments β†’ Add to context
↓
[System Prompt] + [Retrieve Fragments] + [User Question]
↓
model answer

Super long context technology​

1. Attention mechanism optimization​

Core question: The complexity of traditional attention is O(nΒ²)

TechnologyComplexityDescription
Flash AttentionO(nΒ²)Optimize memory access
Ring AttentionO(nΒ²)Block-level calculation
Linear AttentionO(n)Linear approximation

2. Position encoding​

TechnologyMaximum lengthDescription
Absolute position encoding2048GPT-3 used
Relative position encoding8192T5 use
RoPE (Rotation Position)∞LLaMA, Gemini used
ALiBi∞BLOOM use

3. KV Cache compression​

Original KV Cache: [==== ==== ==== ==== ====] (takes up a lot of memory)
After compression: [== == == == ==] (key information retained)

Best practices for context windows​

1. Choose the appropriate model​

SceneRecommended modelWindow size
Simple conversationGPT-4o-mini, Claude Haiku128K
Code ReviewClaude Opus 4.5200K
Full database analysisGemini 2.0 Pro2M
Documentation Q&AGemini 1.5 Pro1M

2. Optimize context usage​

- Contains entire file history
+ Only include the currently modified part

- Send the same message repeatedly
+ Use references or caches

- lengthy system prompts
+ Concise and effective prompt words

3. Monitor Token usage​

import anthropic

client = anthropic.Anthropic()

# Check Token usage
response = client.messages.count_tokens(
model="claude-3-opus-20240229",
text="Your content..."
)

print(f"Input Token: {response.input_tokens}")
print(f"Window usage: {response.input_tokens / 200000 * 100:.1f}%")

Limitations of the context window​

1. Decline in quality​

As the context approaches the upper limit of the window, the model may:

  • Forgetting earlier information
  • Decline in answer quality
  • Hallucinations

2. Cost increase​

Token quantity ∝ API cost

3. Increased latency​

context length β†’ inference time
10K tokens β†’ ~2 seconds
100K tokens β†’ ~10 seconds
1M tokens β†’ ~60 seconds+

Reference resources​

paper​

Technology Blog​


Document updated: December 2025