Sarghaby Araghatta
Home / Knowledge Base / Context engineering (what to put in the window)
Prompting & context · AI Engineering

Context engineering (what to put in the window)

Deciding what goes into the context — instructions, retrieved docs, tool results, memory, history — and in what order, so the model has just enough signal and no noise.

Retrieve factsfrom RAGSummarize historyfor continuityInclude tooloutputs for dataTrim contextfor budgetAssemble finalcontext window
Dynamic context assembly process

How it works

Context engineering involves dynamically assembling the input window for a language model. You start by retrieving relevant facts using a method like Retrieval-Augmented Generation (RAG), which pulls in documents that contain the most pertinent information. Next, you summarize the conversation history to maintain continuity and ensure the model remembers key points. Then, you add outputs from any tools that provide real-time data, like API calls or database queries. Finally, you trim the assembled context to fit within the model's token budget, ensuring you don’t exceed the maximum limit while retaining essential information.

Placement of the information is crucial. For instance, you want to position key facts either at the top or bottom of the context window. If important details are buried in the middle, the model may overlook them, leading to less accurate responses. This careful arrangement helps the model focus on the most relevant signals while minimizing noise from irrelevant or redundant information.

To illustrate, consider a customer support scenario. You might pull in the latest product documentation, summarize the last few interactions with the user, and include the results of a tool that checks the user's account status. By organizing this information effectively, you ensure the model can generate a response that is not only accurate but also contextually aware of the ongoing conversation.

  • Retrieve facts from RAG first.
  • Summarize history for context continuity.
  • Include tool outputs for real-time data.
  • Trim irrelevant information to fit budget.
  • Assemble context in prioritized order.

The software-engineering analogue

A good analogy in software engineering is Dependency injection, where you assemble the necessary components to fulfill a request. You might think of it as building a request payload that includes only what’s needed for the task at hand. However, the key difference here is that adding more context isn’t just about having more data; it can actively degrade the model's performance. Irrelevant tokens can confuse the model, leading to inaccuracies and increased computational costs, making it clear that less can often be more.

Why not the obvious alternative

A common pitfall for newcomers is to dump the entire knowledge base or the full chat log into the context, thinking the model will sort it out. This approach dilutes the model's attention, inflates processing costs, and can lead to distractions that hinder the quality of the output. When the model is overwhelmed with information, it struggles to discern what is truly relevant, ultimately leading to poorer results.

Curated context, on the other hand, improves both the quality and cost-effectiveness of the interaction. By selectively including only the most relevant pieces of information, you help the model focus on what truly matters, allowing it to generate more accurate and contextually appropriate responses without unnecessary overhead.

When to use which

Use retrieval when dealing with large or frequently changing knowledge bases, as this allows you to pull in the most current information. Summarization is ideal when the conversation history becomes lengthy, helping to condense important points without losing continuity. Include tool outputs when you need live data, and always strip away anything that doesn’t serve a specific purpose for the current interaction. This focused approach ensures that the model has just the right amount of context to work effectively.

Less is often more Including too much context can dilute model focus and inflate costs, leading to decreased accuracy.

In production: when it breaks

The scenario Model performance degraded with 30% lower accuracy on user queries.
Context Data QualityInstruction ClarityRetrieved DocumentRelevanceTool Result ValidityMemory ManagementEfficiency
Debugging context engineering issues

Why it happens. This issue often arises from including too much irrelevant noise in the context window, which overwhelms the model's ability to focus on the most pertinent information. If the context includes outdated or unrelated documents, the model may struggle to prioritize the relevant signals needed for accurate responses.

  • Step 1: Analyze the context window content for relevance and recency, checking which documents or instructions are included.
  • Step 2: Measure the model's performance on a subset of queries with varying context configurations to identify which configurations lead to higher accuracy.
  • Step 3: Log the frequency of irrelevant responses and correlate it with the specific elements present in the context window.
The call The fix + the tradeoff + what to monitor> To address this, streamline the context window by removing irrelevant documents and prioritizing high-quality, relevant sources. The tradeoff may be a slightly reduced context length, but it will improve response accuracy. Monitor the percentage of irrelevant responses and adjust the context dynamically based on performance metrics.

What it delivers

Ultimately, mastering context engineering is one of the highest-use skills in applied LLM work. Most improvements in output quality come not from using a more advanced model, but from providing better context. By carefully curating what goes into the model's input window, you can significantly enhance the relevance and accuracy of its responses.