Token LabSearch

← All tutorials
DEVELOPER GUIDE / 03

Debug a context overflow: a practical RAG budget

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.

context_limit = 8192
system        =  400
history       = 1800
retrieval     = 3600
user          =  120
extra         =  128  # estimate
output        = 2048  # reserve

allocated = 400 + 1800 + 3600 + 120 + 128 + 2048
remaining = 8192 - allocated  # 96 tokens
Try it in the tool →

Reduce input without breaking the task

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.

References & implementation

MATHEMATICAL REVIEW · 2026-10-26

Assumptions, implementation & acceptance

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.

System + history + retrieval + user → Add template estimate + output reserve → Compare allocated total with limit → Trim retrieval while retaining reserve
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.

const allocated=400+1800+3600+120+128+2048;
console.log(allocated,8192-allocated,allocated+500-8192); // 8096 96 404
console.assert(allocated+500-660+256===8192);

Test and transfer

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.

Download complete kit (.tar.gz) · Complete reference kit and regression checks

Predict, test and keep evidence

Before revealing an answer, predict one result. After the exercise, record the discrepancy and a transfer test. Saved locally across language editions.

Open notebook / export / clear →

The mathematical review scope is recorded per lesson. It does not establish empirical paper reproduction; report suspected errors in your notes. Review scope