Token Budgets
Spending your context window deliberately
A token budget splits the context window among system, tools, history, retrieval and output — before you send the call. Budgeting is a design skill, not an accident of prompt length.
What you will learn
- Allocate context slots explicitly
- Protect the output budget
- Trim history and retrieval by value
What is this?
A token budget reserves parts of the context window for each job: fixed system+tool slot, a history slot that shrinks by compression, a retrieval slot refilled per query, and a guaranteed output slot. Enforcing the budget before sending prevents silent truncation, cost blowouts and degraded answers.
Why do you need it?
- Output truncation silently degrades products and confuses tests.
- Unbounded history grows every call's cost and latency.
- Budgeting forces you to choose what matters — the essence of context engineering.
- Predictable budgets make costs and latencies forecastable, not surprising.
Mental model
Packing a suitcase with a weight limit
Token budgets
You can't take everything; the scale (token count) decides. Essentials (system rules, output space) go in first, souvenirs (old chat, redundant retrieval) get compressed or left home. Pack once, fly everywhere.
How it works
- Define slots: system+tools (fixed), conversation (compressed), retrieval (reset), output (reserved).
- Count actual tokens per slot with the real tokenizer.
- Compress old turns into summaries that age out of the budget.
- Trim lowest-value content before trimming the output budget.
- Monitor actual vs budgeted usage each call.
A sensible context budget.
In production
- Agents recompute budgets per turn so tool outputs never eat the answer slot.
- RAG services size top-k retrieval by the remaining retrieval slot.
- Support copilots compress multi-turn histories into slot-bounded summaries.
Best practices
- Always reserve an output floor (e.g. 25% or an explicit cap).
- Enforce budget in code — assert before calling, not after.
- Log used-vs-budget per slot for calibration.
- Re-balance slots when adding a new tool or retrieval source.
Common mistakes
- Letting accumulated conversation history silently dominate the window.
- Shrinking the output slot to nothing for 'more context' — truncation returns.
- Budgeting by character count.
- Per-branch budgets that differ across runs (non-deterministic tests).
Common issues
Truncated completions at the same ceilingOutput slot squeezed out by context growth
Set an explicit output reserve and enforce it in the assembler.
Summarized history reduces qualityOver-aggressive compression
Keep recent turns verbatim; summarize only old ones; retain named entities.
Debugging tips
- Answers cutting off at same token count → output budget starvation; check the reserve.
- First-answer quality dips after long chats → history compression is dropping kindergarten nuance.
- Cost creeping per turn → history slot is growing fat; compress earlier.
Interview checkpoint
Quick quiz
Which slot should you protect from starvation above all?
Budgeting by characters is a sound practice.