Context planning
How to budget a context window for RAG without squeezing out the answer
Work through a token allocation for instructions, history, tools, retrieved passages and output, then test what happens when the request grows.
By Context Budget Planner editorial · Published
Original worked examples are hypothetical, not measured product results. Sources checked 2026-11-09.
Start with the answer you need, not the document you have
A retrieved document can fit inside a model’s advertised context window while the complete request does not. Instructions, conversation history, tool definitions and the answer all compete for space. Plan those allocations before choosing how much source material to retrieve.
This guide uses a synthetic 32,000-token capacity, not a provider specification. Every count is an explicit planning assumption. Context Budget Planner accepts counts you supply; it does not tokenize documents, select a model or ask a provider whether a request will be accepted. Check the current model documentation and use its counting method before making a production request.
Technical references: Claude context-window documentation
Build a complete request ledger
Imagine an internal policy assistant answering a question from six retrieved passages. Allocate 1,500 tokens to instructions, 6,000 to conversation history, 2,500 to tool schemas and known overhead, 4,000 to the next answer, and 2,000 to a safety margin. These non-source allocations total 16,000 tokens. The remaining source budget is 32,000 minus 16,000: another 16,000 tokens.
The six passages contain 2,400 tokens each after formatting, including their labels and citations. Together they use 14,400 tokens. Total planned usage is therefore 30,400, leaving 1,600 tokens of headroom. This is a capacity calculation, not evidence that six passages are relevant or sufficient.
Put headings, document identifiers and separators somewhere in the ledger. Counting only the passage bodies while overlooking the packaging creates a budget that cannot be reconciled with the assembled request. Keep a note of which category includes that packaging so it is not counted twice.
Technical references: Claude context-window documentation
Stress-test the two changes most likely to break the plan
First, add a seventh 2,400-token passage. Source material becomes 16,800 and total usage becomes 32,800: an 800-token overflow. Removing 800 tokens would restore arithmetic fit, but cutting the end of a passage blindly may remove an exception or its citation. Prefer selecting a different passage set or shortening a passage with an explicit information-preservation check.
Second, leave the original six passages in place but raise the answer reserve from 4,000 to 6,000. Total usage becomes 32,400, an overflow of 400 tokens. A long requested answer can therefore require fewer sources even when the user’s question is short. Record the original allocation and compare it with the changed allocation rather than relying on memory.
Check the maximum output limit separately. If a model allows only 4,096 output tokens, a 6,000-token answer reserve is invalid even after enough source material is removed. A large overall window does not erase a smaller output maximum.
Technical references: Claude context-window documentation
Do not confuse index overlap with prompt evidence
Chunk overlap duplicates text when a document is split for indexing. If retrieval returns adjacent overlapping chunks, some of that duplicated text may reach the prompt again. Count the actual selected, formatted passages after any deduplication; do not assume that six retrieved chunks contain six entirely distinct blocks of evidence.
A fixed-token estimate also differs from a semantic or recursive splitter. LangChain’s recursive splitter documentation, for example, describes character-based defaults and separator choices. A parameter named chunk_size is not automatically a token count. Verify the splitter’s length function before importing its numbers into a token budget.
Technical references: LangChain recursive text splitting
Fit is the first gate, not the quality verdict
The historical Lost in the Middle study showed position-sensitive performance on its tested long-context tasks and models. It does not establish a current model ranking or a universal failure threshold. Its practical lesson here is narrower: accepting a long request does not demonstrate that all relevant evidence will be used.
Test the assembled prompt on questions requiring a short exception, conflicting passages and evidence near different positions. Record whether the answer cites the right passage and whether it admits when the retrieved material is insufficient. Keep these quality checks separate from the arithmetic fit result.
Technical references: Lost in the Middle paper
A request-budget review you can repeat
Before shipping a changed retrieval setup, repeat the ledger with representative small, typical and large requests. Treat the safety margin as a visible assumption to calibrate against observed accounting, not an unexplained percentage that guarantees success.
- Confirm the model’s current context and output limits independently.
- Use a suitable tokenizer or provider count for the actual assembled request.
- Account for instructions, history, tools, source packaging, answer reserve and uncertainty once each.
- Compare a larger source set and a longer answer reserve before selecting a default.
- Inspect retrieval relevance and answer grounding separately from context fit.
Technical references: OpenAI tiktoken
Sources and scope
Provider rules and supported features can change. These sources support the technical background; the worked examples and decision checklists are authored for this article.
- Claude context-window documentation
Provider-specific context accounting; does not establish a universal rule for all models.
- OpenAI tiktoken
Named encodings and model-specific tokenization.
- LangChain recursive text splitting
Character-count defaults and separator behavior differ from fixed-token windows.
- Lost in the Middle paper
Historical evidence about position effects on the studied tasks and models.