How to Plan Context Windows for a Self-Hosted RAG Application
A model’s context limit is not all available for retrieved evidence. Map every part of the request, test crowded conversations, and tune context use against answer quality.

A model’s context limit is not the retrieval budget
A context window is the amount of tokenized input and generation a model can handle in a request. The model’s advertised or configured limit is a ceiling, not a measure of how many tokens can be devoted to retrieved passages.
A RAG request may include instructions, the current question, prior messages, retrieved content, formatting and special tokens, as well as room reserved for the answer. In configurations where prompt and generation share a context limit, the prompt plus predicted output must fit within that limit. The exact behavior depends on the model, serving layer and application configuration, so verify the setup you actually run.
Text length is not a reliable substitute for token count. Tokenizers split text into tokens and may add special tokens; chat templates can also turn message lists into a formatted model input. Count the formatted request with the tokenizer used by the model where possible, rather than estimating from characters or from what the chat interface displays.
- Plan against the effective limit: confirm the context size configured for the model server, not just a model’s advertised maximum.
- Leave room for generation: define an output allowance appropriate to the task, and include it when checking whether a request fits.
- Measure the assembled input: include chat formatting and any special tokens added during preparation.
Keep the two limits distinct
Some serving configurations expose separate controls for prompt context and generation length. For example, the llama.cpp server documents separate settings for context size and the number of tokens to predict. Separate controls do not mean the request can exceed the context available to the model: the combined prompt and generated tokens still need to fit the configured context.
Context-budget worksheet
Use one row per request component to record where it comes from and how to verify it. Determine the effective limit and output allowance for your own model and serving configuration.
| Request component | What to record | Where to verify |
|---|---|---|
| System and task instructions | Instruction text and any fixed examples | Prompt assembly or application configuration |
| Conversation history | Which messages are included and how they are selected | Assembled request for a short and a long conversation |
| Retrieved passages | Passage count, passage content and any added metadata | Retrieval results and the final prompt sent to the model |
| Current user input | Question and any attached context | Request payload or assembled prompt |
| Formatting and special tokens | Chat template and other input formatting | Formatted input or tokenizer count |
| Output allowance | Configured generation length or response allowance | Model-serving configuration |

Inventory the full request before tuning retrieval
Start by tracing how one request is built, from the user’s message to the input sent to the model. Interface labels such as “context,” “memory,” or “top results” may describe application settings, but do not by themselves show the final prompt or its token count.
Record the source of each component, whether it changes between turns, and how you can inspect it. Include formatting and metadata if they are part of the assembled input. Where the application or serving layer exposes the request or token count, use that evidence; otherwise, reproduce the formatting and count it with the relevant tokenizer if you can.
- Identify who controls the instructions, history selection, retrieval count, passage formatting and output limit: the application, model server or another configured component.
- Inspect requests with both a short and a long conversation to see what history is actually included.
- Check whether the request is explicitly truncated, rejected, shortened or otherwise modified when it is too large. Do not assume that an over-limit request fails in one specific way.
- Write down the model and relevant serving and application settings alongside each test, so a result can be reproduced.

Test realistic requests, including crowded ones
Build a small test set that reflects how people use the application. Include questions that need little context and questions that depend on longer source passages. Test a new conversation, a growing conversation and a request with several retrieved passages. Also test a request that approaches the expected input limit while preserving the output allowance you intend to use.
For retrieval tests, keep the question and task consistent while changing the number of passages. Reorder the passages so the relevant evidence appears in different positions. This helps distinguish a context-fit problem from a case where the model has the information but does not use it effectively. A larger input does not guarantee better use of evidence; where relevant content appears can matter.
Use questions with known answers in the supplied documents. Record whether the answer is correct and supported by the retrieved evidence, not only whether the request completes.
- Include a short query and a long query.
- Repeat a query with different amounts of conversation history.
- Test different retrieved passage counts and put the answer-bearing passage in different positions.
- Include a near-limit case and check both request completion and answer quality.
- Keep the test question, documents and settings fixed when comparing one change with another.
A repeatable context-window test cycle
Run the same known-answer task through a controlled sequence. Change one input condition at a time, then compare request behavior and evidence-based answer quality.
- 1Assemble
Capture the complete formatted request and the effective model and serving settings.
- 2Vary one condition
Change query length, history, retrieved passage count or relevant-passage position while holding the rest steady.
- 3Check the outcome
Record completion, truncation or other observed behavior, and whether the answer is correct and supported.
- 4Tune and retest
Adjust one component, compare against the same test, and repeat after configuration changes.
Recognize failure modes and make measured changes
A request that completes is not necessarily a good request. Look for evidence that part of the intended input was omitted or that the model’s response changed as context grew. Depending on the application and serving configuration, an overlong request might be truncated, lose older history, include fewer retrieved passages, fail, or produce an unexpectedly short answer. Treat these as behaviors to test, not as guaranteed outcomes.
When an answer misses evidence, inspect the assembled request first. Confirm that the relevant passage is present, check its position, and establish whether the request fits. Then change one variable at a time—for example, history length or retrieved passage count—and compare answer quality as well as completion. Cutting passages may make a request fit while removing necessary evidence; keeping more context may also make relevant information harder to use.
- Record the symptom, the request conditions and the first point at which it appears.
- Check whether intended passages and messages are present in the actual input, not just in the application’s retrieval or history display.
- Compare accuracy and evidence use alongside request completion and response length.
- If several components change together, revert and test them separately so the cause of a change is clearer.
Keep a context budget and retest changes
Turn the inventory into a worksheet for the request types your application supports. Record the configured context limit, the output allowance, the token counts or measurement method for each input component, and the conditions used to build the request. There is no universal split that works for every model, prompt and retrieval task; use the worksheet to make trade-offs explicit rather than treating a single retrieved-passage count as a fixed rule.
Retest after changing the model, prompt, retrieval settings, chat formatting or application configuration. Any of these may change the formatted input or the space available to each component. Keep the known-answer tests with the deployment notes so teams can check both fit and answer quality after a change.
- Effective context limit and how it was verified.
- Output allowance and the serving setting that controls it.
- Instructions, history, retrieved passages, user input and formatting included in representative requests.
- Observed truncation, dropped content, errors or response changes, with the conditions that triggered them.
- Answer quality and whether the response is supported by the relevant retrieved evidence.
- Model and application settings used, plus the changes that require a retest.
Frequently asked questions
Does the model’s advertised context length equal the space available for RAG passages?
No. Instructions, conversation history, the current message, retrieved content and input formatting all use context, and generation may also need to fit within the configured limit. Verify the effective limit and count the complete formatted request.
Why can a request fail or change even when its text looks short?
Character length does not directly tell you token count. Tokenization, special tokens and chat formatting affect the prepared input. Inspect or count the formatted request with the tokenizer used by the model where possible.
What should I change first if a RAG request is too large?
First verify what is actually in the request and what happens when it approaches the limit. Then change one component at a time, such as included history or retrieved passage count, and compare both request behavior and evidence-based answer quality.
Will providing more retrieved context improve the answer?
Not necessarily. More information can provide useful evidence, but a longer prompt can also make relevant content harder to use. Test passage count and the position of the answer-bearing passage with known-answer questions.
Sources and further reading 3
- Tokenizer documentation Hugging Face
- LLaMA.cpp HTTP Server README ggml-org / llama.cpp
- Lost in the Middle: How Language Models Use Long Contexts arXiv (paper by Liu et al.)