Skip to main content

Chapter 2: Token Limits and Context Windows

Context Length Evolution​

ModelYearContext
GPT-320202K
GPT-3.520224K/16K
Claude 22023100K
GPT-4 Turbo2023128K
Claude 32024200K
Gemini 1.520241M+

Handling Long Documents​

Chunking Strategies​

def chunk_text(text, max_tokens=4000, overlap=200):
tokens = tokenizer.encode(text)
chunks = []
for i in range(0, len(tokens), max_tokens - overlap):
chunk = tokens[i:i + max_tokens]
chunks.append(tokenizer.decode(chunk))
return chunks

RoPE for Long Context​

Rotary Position Embedding enables length extrapolation:

  • LLaMA uses RoPE
  • Position info in rotation, not addition

Key Takeaways​

tip
  1. Context windows have grown dramatically (2K → 1M+)
  2. Chunking with overlap preserves context
  3. RoPE enables better long-sequence handling