Token Lab検索

← チュートリアル一覧
開発者ガイド / 03

コンテキスト超過を調べる:実践的なRAG予算

リクエストを分解し、生成領域を予約して、増加の主因を特定します。

予算全体を把握

リクエストは最新のユーザーメッセージだけではありません。システム指示、会話履歴、検索文書、ユーザー入力、メッセージやツールの付加情報を含め、生成出力の領域も確保します。

例では8,192のうち8,096を割り当て、残りは96です。500トークンの文書を追加すると404超過します。これは説明用の数値で、特定モデルの仕様ではありません。

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.

最も大きな部分を探す

予算ツールに各部分を入力すると、それぞれの寄与が表示されます。超過例を読み込んで警告と大きなRAG部分を確認してください。

個別エンコードは内訳の分析に便利ですが、最終リクエストを完全に再現しません。区切り、形式、ツール定義、メディアで使用量が変わるため、余裕を設けてAPIの実測値と比較します。

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
ツールで試す →

タスクを壊さず削減

検索結果は重複排除と再ランキングを先に行い、関連する箇所と出典を残します。長い履歴は古い部分を要約し、決定事項と最近の会話を保ちます。

変更後は再計数し、代表的な回答品質をテストします。重要な指示を黙って切り捨てないでください。コンテキスト上限だけでなく出力上限も確認します。大きな入力枠が同じ大きさの出力枠を意味するわけではありません。

参考資料と実装

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