Navigation

Introduction to AI

Machine Learning

Deep Learning

Generative AI

Tools & Frameworks

General

Context Length and Retrieval

The context window is the amount of text an LLM can attend to in a single forward pass. Understanding its implications — and knowing when retrieval is a better option — is essential for building reliable AI applications.

What Is Context Length?

Every LLM has a maximum context window, measured in tokens (roughly ¾ of a word each). Modern models range from:

  • 8K–32K tokens: Older GPT-4, Mistral 7B
  • 128K tokens: GPT-4o, Claude 3.5 Sonnet, Llama 3.1
  • 1M+ tokens: Gemini 1.5 Pro, Claude 3.5 with extended context

The context window holds the system prompt, conversation history, retrieved documents, and the current user message all at once.

How Context Length Affects LLM Behavior

The “Lost in the Middle” Effect

Research shows that LLMs perform best when relevant information appears at the beginning or end of a long context. Information buried in the middle of a very long prompt tends to be underweighted during attention. For critical information, placement matters.

Cost and Latency

Processing more tokens costs more money and takes longer. Filling a 128K context window is 16× more expensive per call than filling an 8K window. Long contexts also increase time-to-first-token latency.

Attention Scaling

Standard attention is quadratic in sequence length — O(n²). Efficient attention variants (Flash Attention, sliding window attention) reduce this, but long contexts still increase compute significantly.

Context Pollution

Irrelevant content in the context degrades model performance. A well-curated 4K context typically outperforms a noisy 32K context on focused tasks.

Long Context vs. Retrieval: When to Use Each

Use Long Context When:

  • The entire document or conversation must be visible for the task (e.g., editing a full codebase, analyzing a contract).
  • The query requires cross-document reasoning where pre-selecting chunks would lose important connections.
  • Latency and cost are not constraints.
  • The input is small enough to fit comfortably (e.g., a few documents, not thousands).

Use Retrieval (RAG) When:

  • The knowledge base is too large to fit in any context window.
  • You need up-to-date information that changes frequently.
  • Costs must be controlled — retrieval fetches only the relevant 3–10 chunks.
  • The query is focused and well-defined, making dense retrieval accurate.
  • You need source attribution — retrieval gives you explicit document references.

Hybrid Approaches

Many production systems combine both: retrieve relevant chunks first, then place them in a long-context model along with the conversation history. This gives the benefits of retrieval (scale, freshness) with the benefits of long context (full chunk visibility, coherent reasoning).

Designing for Context

Prioritize Placement

Put the most important information at the start of the context (after the system prompt) and the user question at the very end. Avoid burying critical instructions in the middle.

Compress Before Inserting

Use summarization, extraction, or structured representations to reduce context size:

  • Summarize long documents before including them.
  • Extract only the relevant sections from PDFs.
  • Use structured formats (JSON, tables) instead of verbose prose where possible.

Chunk Intelligently

When using RAG, chunk documents at semantic boundaries (paragraphs, sections) rather than arbitrary character counts. Add overlap between chunks to preserve context at boundaries.

Include Metadata

Add document titles, dates, and section headings as context for each chunk. This helps the model understand what it is reading and improves citation quality.

Context Window Management in Conversations

Long conversations accumulate history that eventually exceeds the window. Strategies:

  • Sliding window: Drop the oldest turns while always keeping the system prompt.
  • Summarization: Compress old conversation history into a running summary.
  • Memory store: Extract key facts from the conversation and store them in a vector database, retrieving them in future turns.
  • Session reset: Start a new session for unrelated topics.

Practical Limits

Even if a model supports 1M tokens, that does not mean you should use all of it:

  • Accuracy degrades with very long contexts on some models.
  • Cost becomes prohibitive for high-volume applications.
  • Latency becomes unacceptable for interactive use.

The practical sweet spot for most tasks is under 32K tokens, with longer contexts reserved for specific whole-document tasks.