Navigation

Introduction to AI

Machine Learning

Deep Learning

Generative AI

Tools & Frameworks

General

Context Windows in LLMs: Understanding Token Limits

Every Large Language Model has a “context window”—the maximum number of tokens it can process at once. GPT-4o has 128k tokens, Claude 3.5 has 200k, and Gemini Ultra has 1 million. Understanding these limits is critical to building reliable AI systems.

What’s Inside Your Context?

Every interaction consumes tokens:

  • Your prompt: “Summarize this article…” = ~5 tokens
  • The document you’re summarizing: ~2000 tokens
  • System instructions: ~100 tokens
  • Previous messages in a conversation: variable
  • The model’s response: consumed as it’s generated

Total consumed: 2100+ tokens before you get your answer.

Why Context Matters

  1. Can’t Process Everything: You can’t paste your entire codebase and ask “refactor this”—it won’t fit
  2. Performance Degrades: Models perform worse at the end of long contexts (lost-in-the-middle problem)
  3. Costs Add Up: Many APIs charge per token; a 100k context token usage is expensive
  4. Quality Drops: The deeper into a long context, the more the model “forgets” earlier information

Strategies to Manage Context

  • Summarization: Pre-summarize long documents to extract key points
  • Chunking: Break problems into smaller pieces and solve them iteratively
  • Retrieval: Use embeddings to find only relevant sections (RAG pattern)
  • Prompt Compression: Use techniques like LLM-based compression to reduce prompt size
  • Conversation Management: Archive old messages or summarize chat history

Choosing the Right Model

  • Short Tasks: Smaller context works fine (4k tokens)
  • Document Analysis: Mid-range (16-32k tokens)
  • Multi-document Workflows: Large context (100k+ tokens)

The tradeoff: larger context windows often mean slower inference and higher costs.