The context window is the working memory of a Large Language Model (LLM) during an active session. The model uses this "memory" to process your input and generate a response. It determines exactly how much information—including system instructions, conversation history, retrieved data, and the model's own output—the model can hold in its "mind" at once. As an engineer, you must treat this as a finite resource.
Once the conversation or the data payload exceeds this hard limit, the model loses its ability to reference earlier information. Because the model can only "see" what is currently held within the active window, anything that falls outside is effectively purged. This is not merely a storage issue; it is a performance bottleneck. When the window overflows, the model begins making educated guesses about missing information, which leads to hallucinations and a breakdown in reasoning continuity.

What is a context window?
A context window is the technical limit on the total number of tokens a model can process in a single request-response cycle.
Think of it as a "working memory" for the model. While the model's training data provides a global knowledge base, the context window provides the specific, local facts and history needed for the current task.

A useful analogy is a fixed-size box. If your conversation, source code, and documents fit inside the box, the model recalls every detail perfectly. If the contents overflow, the model must drop earlier data to make room for new input. Once a token is pushed out of the box, it no longer exists from the model's perspective.
The capacity of these "boxes" has expanded rapidly. Early LLMs were restricted to roughly 2,000 tokens. Modern enterprise models have stretched that far higher: IBM Granite 3 offers 128,000 tokens, while flagship models like Claude Opus 4.8 and Claude Sonnet 4.6 reach 1 million tokens. Google's Gemini 1.5 Pro currently pushes this to 2 million tokens. This space is not exclusively for your messages, though. The window is occupied by several competing data types:
- System prompts: High-level instructions that define the model's persona and safety guardrails.
- Conversation history: The transcript of all previous user and assistant turns.
- External assets: Attached PDF pages (up to 600 in some Claude 4 models), images, or source code files.
- RAG data: Snippets of information retrieved from external vector databases to ground the response.
- Model Context Protocol (MCP) bloat: Technical overhead from third-party tools and servers that can consume thousands of tokens before you even send a prompt.
How does a context window work?
To manage the context window effectively, you need to understand how language is quantified and processed within this space.
Tokens and language variance
Tokens are the atomic units of an LLM. While you see words, the model sees numerical representations of characters, parts of words, or phrases. The word "amoral" is often split into two tokens, "a" and "moral," because the prefix "a" carries distinct semantic meaning.
Tokenization efficiency also varies by language. While a rule of thumb for English is 1.5 tokens per word, other languages are far less efficient. A sentence in Telugu might have fewer characters than its English translation but produce over seven times the number of tokens. This means your context window "budget" effectively shrinks when you build multilingual applications.
The self-attention mechanism and quadratic cost
The reason an LLM can understand the relationship between a pronoun at the end of a document and a noun at the beginning is the self-attention mechanism. It computes "vectors of weights" that represent how relevant every token is to every other token in the sequence.
From an engineering perspective, this is the root of the quadratic scaling challenge. In a standard Transformer architecture, the model performs an "all-to-all" comparison. If you double the number of tokens in the window, the number of internal comparisons increases by a factor of four (O(n^2)). Larger context windows are therefore not just a matter of memory, but of steep computational demand and increased latency.
Extended thinking and tool use
Advanced models like Claude use "extended thinking" blocks to reason through complex problems. These thinking tokens count toward your total context limit and output budget during the current turn. To maintain efficiency, the Claude API automatically strips these thinking blocks from the conversation history in subsequent turns.
There is one critical exception: tool use cycles. If the model generates a thinking block and then calls a tool, you must return that unmodified thinking block along with the tool result in the next turn. This is verified via cryptographic signatures. Failing to preserve the reasoning block during tool use breaks the model's reasoning continuity and triggers an API error.
Why isn't more context always better?
You might be tempted to use the largest window available for every task. That is often a mistake, for several reasons.

Performance degradation and cognitive bias
Models suffer from context rot, or the lost-in-the-middle phenomenon. Research confirms that LLMs exhibit primacy and recency biases: they recall information from the very beginning or the very end of a prompt with high accuracy but struggle with information buried in the center. If you place a critical "needle" of data in the middle of a 100,000-token "haystack," the model's retrieval accuracy drops significantly.
Computational latency and cost
Because of the quadratic scaling of self-attention, the time required to predict the next token increases as the window fills. High-resolution workflows or real-time coding agents can become painfully slow as they approach the end of a large context window. Processing millions of tokens per request also carries a high financial cost that may not be justified if a leaner prompt could handle the task.
Context bloat via MCP
Model Context Protocol (MCP) servers let you plug in pre-made toolsets, but they are a major source of context bloat. Adding multiple MCP servers can fill a third of your context window with system instructions and tool definitions before the model even begins processing your actual data. Keep your context lean by enabling only the tools the specific task needs.
Safety risks
Larger windows provide a broader attack surface for adversarial prompts. Techniques like many-shot jailbreaking bury harmful instructions deep within a massive volume of benign text. When the context is extremely long, it becomes more difficult for standard safety filters to identify and mitigate these embedded risks.
What happens when you exceed the context window?
When the combined total of input and output tokens hits the model's hard limit, behavior depends on the model version.
- Truncation and forgetting: Most chat interfaces use a "first in, first out" (FIFO) system. The earliest parts of the conversation are dropped to make room for new tokens, which leads to the model "forgetting" the initial constraints or facts of the session.
- Hallucinations: When a model loses access to early context but is still asked to reference it, it will not necessarily admit it has forgotten. Instead, it makes "educated guesses," creating confidently stated but entirely false information.
- API behaviors: Newer models, such as Claude 4.5 and Sonnet 4.6, will often accept a request even if the
max_tokensparameter might push the total over the limit. If the limit is hit during generation, the API returns a stop reason ofmodel_context_window_exceeded. Older models typically return a validation error upfront and refuse to process the request.
How do you manage a context window effectively?
Effective context management is the difference between a brittle prototype and a production-ready AI system.

Token counting and awareness
Always use a dedicated token-counting API to estimate your payload before sending it. Models like Claude Sonnet 4.6 and Haiku 4.5 also feature context awareness: they explicitly receive an update on their remaining "token budget" after each tool call. This lets the agent understand how much space is left to finish a task instead of guessing its remaining capacity.
Compaction and summarization
For long-running sessions, use server-side compaction. This has the LLM summarize the preceding conversation history into a concise block that preserves the intentions and key facts while discarding thousands of redundant tokens. It pulls the conversation back from the limit and mitigates lost-in-the-middle problems.
Retrieval-augmented generation (RAG)
RAG is the standard architectural pattern for managing massive datasets. Instead of stuffing every document into the context window, you store data in an external database and inject only the most relevant "needles" of information into the prompt. This keeps the attention mechanism focused and the computational cost low.
Context editing
In complex agentic workflows, you edit the context manually. This includes clearing out old tool results that are no longer relevant and stripping unnecessary reasoning blocks once a tool cycle is complete. Keeping the context lean is a primary responsibility of the system architect.
FAQ
How do I calculate the number of tokens in a document? For English text, use the rule of thumb that 100 words equal roughly 150 tokens. For high-precision requirements, use a model-specific tokenizer API, since the "exchange rate" varies between model architectures.
What is the largest context window currently available? Google Gemini 1.5 Pro offers up to 2 million tokens. Flagship Claude models like Opus 4.8 and Sonnet 4.6 offer 1 million tokens, though some versions on specific platforms like Microsoft Foundry may be capped at 200,000.
Does a model's "thinking" count toward the limit? Yes. All internal reasoning tokens count toward the context window and output budget during the turn they are generated. In Claude, these are typically stripped for future turns, except during active tool-use cycles.
Why does my model's performance drop when I give it more data? This is often lost-in-the-middle degradation. LLMs prioritize information at the beginning and end of a prompt, so when critical details are buried in the center of a long context, the model's attention mechanism may deprioritize them.