Sarghaby Araghatta
Home / Knowledge Base / The context window & context management
Model foundations · AI Engineering

The context window & context management

The maximum tokens a model can attend to at once (system + history + retrieved docs + user + output), from ~8K to 1M+. Nothing outside it exists to the model.

Send full contextProcess contextGenerate responseManage contextUpdate contextUser Input(Question/Prompt)Context Window(History + Docs)Model Processing(Stateless)Output(Response)Context Management(Truncate/Summarize/RAG)
Context Management Flow

How it works

In a large language model (LLM), the context window defines the maximum number of tokens the model can process at once, which includes the system prompt, user input, historical interactions, retrieved documents, and the model's output. When you send a request, you must provide the entire context because the model does not retain any state between calls. This means that if you exceed the token limit, you need to manage the context effectively. You can do this by truncating older parts of the conversation, summarizing the rolling history to condense information, or using a retrieval-augmented generation (RAG) approach to fetch only the most relevant chunks of information instead of sending everything. This way, you keep the context relevant and within the limits of what the model can handle.

The management of the context window is crucial because anything outside this limit is effectively invisible to the model. For example, if your model has a context window of 8,192 tokens and your conversation history plus the current prompt exceeds this limit, the model will ignore the oldest tokens. This can lead to a loss of important information that might be necessary for generating coherent and contextually appropriate responses. By carefully curating the context, you ensure that the model has access to the most relevant information, which directly impacts the quality of its output.

When you implement context management strategies, you can significantly influence the model's performance. For instance, if you decide to summarize previous interactions, you might lose some nuanced details, but you gain efficiency and keep the context within bounds. On the other hand, if you retrieve specific chunks of information that are relevant to the current query, you can provide the model with grounded facts, which improves its accuracy. This interplay between context management techniques and the model's response quality is vital for building effective applications.

  • Send full context with each request.
  • Use truncation/summarization for efficiency.
  • Retrieve only relevant chunks when needed.
  • Long windows are better for reasoning.
  • Short windows improve relevance and cost.

The software-engineering analogue

Think of the context window as the RAM in a computer system, where the model actively processes information. Just like RAM holds active data for quick access, the context window retains the tokens that the model can use to generate responses. In contrast, your database or RAG store acts like disk storage, holding vast amounts of data that can be retrieved when necessary. However, there’s a key difference: while a computer can page data in and out of RAM, anything outside the context window in an LLM is simply not visible to the model. This means that if you don't load the relevant information into the context, it won't just be slow — it will be completely absent from the model's perspective.

This distinction is crucial because it highlights how LLMs operate differently from traditional software systems. In typical software, you can manage memory and storage dynamically, but with LLMs, the context window is fixed during a single call. If you exceed this limit, you can't just swap in older data; you must actively manage what is included in the context. This requires thoughtful consideration of what information is essential for the task at hand.

Why not the obvious alternative

Many newcomers might think the simplest solution is to just use a model with a larger context window, such as a 1M-token model. While this approach seems straightforward, it comes with significant drawbacks. Larger models often incur higher costs, both in terms of computational resources and latency. As the context grows, the model can struggle with recall, especially for facts that are buried deep within the context. This phenomenon, often referred to as getting 'lost in the middle,' can lead to a decline in the model's ability to generate accurate responses.

Instead of relying solely on a larger context window, focusing on curating a tighter, more relevant context can yield better results. By selectively including only the most pertinent information, you can enhance the model's performance without the added costs and latency of a larger model. This approach emphasizes the importance of context management strategies over simply increasing the window size, as it allows for more efficient and effective use of the model's capabilities.

When to use which

Choosing between a long context window and a more curated approach depends on the specific requirements of your task. Long context windows are beneficial for tasks that require whole-document reasoning or when you need to provide the model with extensive context for few-shot learning scenarios. In these cases, having access to a larger body of text can help the model understand the nuances and relationships within the data.

On the other hand, if you are working at scale and need to control costs, retrieval and summarization techniques often prove more effective. By focusing on relevance rather than sheer volume, you can ensure that the model receives the most important information without overwhelming it with unnecessary data. This trade-off allows you to optimize for both performance and resource management.

Context Truncation Pitfall Over-truncating can lead to loss of critical information, affecting model accuracy.

In production: when it breaks

The scenario The model fails to generate relevant responses, with only 40% accuracy on user queries.
Context window sizeToken overflowInsufficient historylengthRetrieval strategymismatchAdjust context windowExpand history lengthOptimize retrieval method
Context window management diagnostics

Why it happens. The model has a limited context window, and when you exceed it, the oldest tokens are truncated. This results in the model losing important context from earlier interactions, leading to less coherent responses and a higher likelihood of irrelevant output. Sending excessive tokens increases processing time and costs.

  • Measure the average token count of each call to the model.
  • Log the relevance of output quality over time, focusing on coherence and relevance to user queries.
  • A/B test different strategies for context management, such as summarizing old turns and deduping retrieved chunks.
The call To fix this, implement a strategy to trim and compress the context sent to the model, keeping only the most relevant tokens. The tradeoff is potentially losing some historical context, but this will improve output quality and reduce costs. Monitor the average token count and output relevance metrics to ensure the model remains effective with the new context management strategy.

What it delivers

Effective context management delivers control over accuracy, latency, and cost in any LLM application. By thoughtfully managing the context window, you can enhance the model's ability to produce relevant and coherent responses while keeping resource usage in check. This balance is essential for building scalable and efficient AI systems.