Introduction
When building AI-powered applications, particularly those relying on retrieval-augmented generation (RAG), getting the recall settings right is crucial. I've spent a good chunk of time wrestling with scenarios where our agents would either hallucinate due to insufficient context or struggle with performance because the context window was too large. A common pitfall I observed, and one I initially fell into myself, was treating context length and quote limits as independent variables. It turns out that tuning one without considering the other often leads to suboptimal recall and, in some cases, outright incorrect responses.
My team was working on an internal knowledge base agent. We initially focused on maximizing context length, assuming more information was always better. Our reasoning was that the more data we fed the model, the higher the chance it would find the relevant snippets. However, this led to a noticeable degradation in response quality. The model, when presented with a massive block of text, would sometimes latch onto less relevant information or struggle to synthesize the core answer. Performance also took a hit, as processing larger contexts naturally consumed more resources and time. For instance, a common failure mode we encountered was 'topic drift.' If a user asked a question about a specific product feature, and the extensive context included documents discussing broader company initiatives or unrelated product lines, the agent might incorporate details from these tangential topics into its answer, even if the core information was present elsewhere. This manifested as answers that were technically plausible but ultimately irrelevant or misleading to the user's original query. We then tried to compensate by aggressively reducing the quote limit – the number of retrieved documents or chunks sent to the LLM. This swung the pendulum too far in the other direction. The agent became concise, sometimes overly so, but frequently missed critical details because the quote limit was too restrictive, even if the surrounding context was theoretically available in the larger retrieval pool. This often resulted in terse, unhelpful responses that lacked the necessary depth, prompting users to rephrase or ask follow-up questions to extract the full picture.
Balancing Context and Quote Limits
The breakthrough came when we started viewing context length and quote limit as a closely coupled pair. Instead of isolated adjustments, we began to define them in relation to each other and to the expected complexity of the queries. For simpler queries with a high likelihood of a direct answer in a single document, a smaller context window and a tighter quote limit (e.g., 2-3 quotes) proved effective. This configuration allowed the model to quickly identify and extract precise information without being distracted by extraneous details. For more complex, multi-faceted questions requiring synthesis from several sources, we expanded both. This provided the model with a broader informational landscape from which to draw connections and formulate a comprehensive answer. The key was to ensure that the quote limit allowed enough relevant snippets to populate a meaningful portion of the context window without overwhelming the model with redundant or marginally useful information. We found that a good starting point was to aim for retrieved quotes to occupy roughly 30-50% of the total context window, leaving the rest for the prompt instructions and the model's internal working memory. This approach ensured that the model received a diverse but manageable set of information, improving both accuracy and efficiency. This balanced approach reduced instances of topic drift and improved the completeness of answers by providing sufficient, but not excessive, context.
One important caveat: this paired tuning isn't a one-time setup. The optimal balance between context length and quote limit is highly dependent on the domain, the quality of your embeddings, and the specific characteristics of your retrieval system. Regular evaluation with diverse query sets is essential. We found that as our knowledge base evolved, and as the types of questions users asked changed, we needed to revisit and re-tune these parameters. Before committing to a specific configuration, consider the following:
| Aspect to Verify | Description The key was to ensure that the quote limit allowed enough relevant snippets to populate a meaningful portion of the context window without overwhelming the model with redundant or marginally useful information. We found that a good starting point was to aim for retrieved quotes to occupy roughly 30-50% of the total context window, leaving the rest for the prompt instructions and the model's internal working memory. This approach ensured that the model received a diverse but manageable set of information, improving both accuracy and efficiency.
Conclusion
One important caveat: this paired tuning isn't a one-time setup. The optimal balance between context length and quote limit is highly dependent on the domain, the quality of your embeddings, and the specific characteristics of your retrieval system. Regular evaluation with diverse query sets is essential. We found that as our knowledge base evolved, and as the types of questions users asked changed, we needed to revisit and re-tune these parameters. For those exploring similar agent architectures, I highly recommend checking out resources like the FastGPT on GitHub project, which delves into architectural considerations for robust AI agents, including how to manage context effectively to ensure audit immutability.

Follow on Facebook



