Break a request into sections, reserve generation space, and prioritize the largest source of context growth.
Start with a complete budget
A request is more than the latest user message. Include system instructions, conversation history, retrieved documents, user text, and an allowance for message or tool overhead. Reserve space for generated output.
In the example below, 8,096 of 8,192 tokens are allocated, leaving just 96. Adding a 500-token document pushes the planned request 404 tokens over the limit. These are teaching inputs, not a model-specific context claim.
Study guide & lesson outlineLEARNING CONTRACT
Understand → implement → investigate
Goal: explain the mechanism in this lesson, reproduce one example and test a failure case. Reading alone does not mark mastery.
Understand: state assumptions, shapes/units and a counterexample.
Implement: reproduce a numerical example and inspect boundaries.
Investigate: compare one alternative under a matched budget.
Find the largest contributor
Enter the sections separately in Context budget. The tool encodes each section and displays its contribution. Load the over-limit example to see the overflow state and the large RAG section.
Separate encoding is useful for attribution but is not a byte-for-byte reconstruction of the final request. Separators, templates, tool schemas, and media can change actual usage. Keep an explicit allowance and compare with observed API usage.
For oversized retrieval, deduplicate and rerank first, then retain relevant passages with their sources. For long history, summarize older turns while preserving decisions and keep the recent exchange verbatim.
Recount after each change and run a representative answer-quality test. Do not silently truncate system constraints. Check the model’s separate output limit as well as its context window; a large context does not imply an equally large output allowance.
8096 of 8192 leaves 96 tokens. Adding 500 overflows by 404; to also preserve a 256-token reserve, remove 660. Provider template overhead must be measured separately.
Shared code, English mathematical explanation and key multilingual numeric passages reviewed. This is not a full translation proofread.
Mechanism map: follow the transformations from top to bottom.
Before running: predict a boundary case
8096 of 8192 leaves 96 tokens. Adding 500 overflows by 404; to also preserve a 256-token reserve, remove 660. Provider template overhead must be measured separately.
Hint: isolate the assumption
Keep the baseline input fixed, change one parameter or boundary, and compare both the numerical result and the claimed units. A passing example verifies this fixture only.
Minimal executable example
Save the example and linked modules in one folder. Use Node.js 22 or newer, then run node context-budget-rag.mjs. No third-party runtime dependencies.
8096 of 8192 leaves 96 tokens. Adding 500 overflows by 404; to also preserve a 256-token reserve, remove 660. Provider template overhead must be measured separately.
Record input, code version, expected result and observed result in the notebook below. Then construct a counterexample that violates the assumption above and explain why the original conclusion no longer follows.
Before revealing an answer, predict one result. After the exercise, record the discrepancy and a transfer test. Saved locally across language editions.
The mathematical review scope is recorded per lesson. It does not establish empirical paper reproduction; report suspected errors in your notes. Review scope