GICT knowledge base
Context Window Explained: What Happens When You Give an AI Too Much to Read
Published
Every AI model has a limit on how much text it can read at once. This limit is the context window, measured in tokens. Get it wrong and the model either ignores half your data, crashes mid-query, or charges you for tokens you did not need. Here is how it actually works.
What a Token Actually Is
Language models convert text into numbers first. The conversion process is called tokenization. Most models use Byte Pair Encoding, which splits words into subword units. Common words like “the” become a single token. Rare words get split: “incredibly” might become three tokens: “incred”, “ibly”. A useful rule of thumb is one token equals roughly four characters of English text. One hundred tokens is about 75 words.
You can see this in action with Python and the tiktoken library, which OpenAI uses to count tokens:
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
text = "The quick brown fox jumps over the lazy dog."
tokens = enc.encode(text)
print(f"Token count: {len(tokens)}")
Output:
Token count: 9
Nine tokens for a 45-character sentence. Counting tokens matters because every AI API bills by the token and every context window has a hard limit. Exceed it and the API throws an error. Get close to it and the model starts losing track of what you gave it.
Major Model Context Windows in 2026
Model | Context Window -------------------|---------------- GPT-4o | 128,000 tokens Claude 3.5 Sonnet | 200,000 tokens Claude 4 Opus | 200,000 tokens Gemini 1.5 Pro | 2,000,000 tokens Gemini 2.5 Pro | 2,000,000 tokens Llama 4 Scout | 10,000,000 tokens
One million tokens is roughly the text of three novels. Gemini 2.5 Pro can ingest an entire book plus its sequel in a single prompt and still have room left over.
But here is the catch that marketing numbers do not tell you. Having a large context window does not mean the model uses all of it equally well.
The Lost in the Middle Problem
Research from Stanford and Berkeley, published in 2024 under the title “Lost in the Middle,” tested models on a task called the needle-in-a-haystack test. Researchers buried a specific piece of information at different positions inside a large irrelevant document and asked the model to retrieve it. The results followed a U-shape curve.
When the target was at the beginning or end, retrieval accuracy was high. When it was buried in the middle, accuracy dropped significantly. For a 100-token needle inside a 10,000-token haystack, some models failed to retrieve it more than half the time.
This happens because of how the attention mechanism inside transformer models works. Every token attends to every other token, but the model has limited attention budget. It focuses on the start and end and loses resolution in the middle, like peripheral vision: you see edges clearly but things at the periphery blur.
In practice, if you dump 100 pages of documentation into a prompt and hide the answer on page 63, the model might miss it even though it technically fits within the context window. The model is not hallucinating. It genuinely did not notice.
What Happens When You Exceed the Limit
When your input tokens exceed the context window, the API returns an error. OpenAI returns a specific error code. Anthropic processes input up to the limit and silently drops the rest. Google may truncate or reject. The behavior is not consistent across providers.
The most common workaround is truncation. The naive approach is last-in-first-out, which keeps recent messages and drops the oldest:
def truncate_lifo(messages, max_tokens, encoding):
total = sum(len(encoding.encode(m["content"])) for m in messages)
while total > max_tokens and len(messages) > 1:
removed = messages.pop(0)
total -= len(encoding.encode(removed["content"]))
return messages
This is what most chat interfaces do when a conversation gets long. It preserves recent context but drops the original question if it was asked far enough back.
A smarter approach is to prioritize. Put must-have information last so truncation cannot remove it. Put optional context earlier. Most RAG systems do the opposite: they put retrieved context in the middle and the question at the end, which is exactly the configuration most likely to cause lost-in-the-middle failures.
The Cost Nobody Talks About
Larger context windows cost more per request. OpenAI charges per token, so a 128,000-token prompt costs roughly 64 times more than a 2,000-token prompt. There is also a latency cost. More tokens take longer to process. A prompt using 100,000 tokens will have slower first-token response times than one using 10,000 tokens, even if the answer is short.
The right approach is not to always use the maximum context. It is to use exactly what the model needs and nothing more.
Retrieval-Augmented Generation
The most common failure mode in AI applications is context pollution. You dump a large document into the context, the user asks about a detail buried in the middle, and the model gives a wrong answer or says it cannot find the information.
RAG solves this structurally. Instead of dumping an entire document, a RAG system searches for specific relevant passages and injects only those. This keeps the context small and relevant, avoids the lost-in-the-middle problem, costs less, and responds faster.
The tradeoff is that retrieval quality determines answer quality. Chunk size matters enormously. If you chunk a document into 2,000-token pieces and the relevant fact sits near the boundary of two chunks, the retrieval system might return the wrong side. Smaller chunks around 250 to 500 tokens tend to be more precise, but they lose local context.
Strategies That Actually Work
Compaction is the technique AI coding assistants use. When conversation history approaches the context limit, the agent passes old messages to the model and asks for a summary. The summary replaces the original messages in the context. This recovers most useful information at a fraction of the token cost:
System prompt: Summarize the following conversation, preserving all architectural decisions, unresolved bugs, and user preferences. Discard redundant tool outputs. [conversation history here]
Boundary placement is another proven technique. Anthropic recommends putting the most important content at the very beginning or very end of the context, not in the middle. System instructions go first. Critical directives go last. The model pays more attention to those positions.
For RAG systems, HyDE (Hypothetical Document Embeddings) improves retrieval precision. Instead of searching for the raw query, you first ask the model what the ideal retrieved document would look like for that query, then search for that. This matches dense representations against dense representations rather than sparse text against dense vectors.
Conclusion
The context window is the maximum number of tokens a model can read in a single request, including your prompt, conversation history, retrieved context, and the generated response. Context windows range from 128,000 tokens on GPT-4o to 10 million on Llama 4 Scout. English text averages about 4 characters per token.
The lost-in-the-middle problem means that dumping a large document into the context does not guarantee the model will find facts buried in it. Models perform best when relevant information sits at the beginning or end. Middle content gets attention last and may be ignored entirely.
Truncation is the common fix but it is dumb by default. Smarter truncation preserves must-have content and pushes it to the end. RAG systems solve the problem structurally by retrieving only relevant passages. Compaction, boundary placement, and careful chunk sizing are the techniques that work in production systems today.
Related InterServer guide: If you want AI agents to connect directly to InterServer hosting tools, read InterServer MCP Server: Connect AI Agents to Your Hosting Account.
Related guides
How to Secure the Ollama API with Nginx
Protect a private Ollama API with Nginx authentication and restricted routes. Verify allowed requests, denied management calls and direct-port isolation.
AIHow to Set Up an LLM Gateway with LiteLLM
Configure a LiteLLM gateway with private administration, virtual keys and model restrictions. Test allowed requests and understand budget limitations.
AIHow to Self-Host Karakeep with AI Tagging on a VPS
Self-host Karakeep with local Ollama tagging. Restrict first-user setup, disable public registration and verify collection, search and suggested tags.
AIHow to Categorize Support Tickets with a Local AI Model
Suggest support-ticket categories from a CSV using a CPU-only AI model. Keep inference local, preserve ticket IDs, and review uncertain classifications.
