Design a token budget for an agent with a 200k context window. How do you allocate it, and how do you enforce it?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Start by separating two numbers people conflate: the window is 200k, the budget is whatever your eval says quality holds at, which is meaningfully lower. Context rot starts well below the limit, prefill latency scales with tokens, and cost scales with tokens. Pick the budget from the quality-versus-length curve on your own eval, not from the model card. On many agents that lands somewhere well under half the window.
Full worked answer with diagram
Start by separating two numbers people conflate: the window is 200k, the budget is whatever your eval says quality holds at, which is meaningfully lower. Context rot starts well below the limit, prefill latency scales with tokens, and cost scales with tokens. Pick the budget from the quality-versus-length curve on your own eval, not from the model card. On many agents that lands somewhere well under half the window.
Then partition it, with a declared cap and a declared degradation strategy per component:
| Component | Cap | On overflow |
|---|---|---|
| System + tool schemas | ~5k | Hard fail, this is a bug |
| Session state / memory | ~2k | Drop lowest-confidence records |
| Retrieved documents | ~25k | Rerank, keep top-n |
| Conversation history | ~50k | Compact at threshold |
| Tool results | remainder | Paginate and truncate with markers |
| Output reserve + margin | ~15% | Never allocate |
Enforcement is architectural, not disciplinary. One ContextBuilder owns assembly, and no code path may append to the prompt directly. Every component registers with a cap and a degrade() callback. Count with the provider's real tokenizer or counting endpoint, never a chars/4 heuristic, which is fine for English prose and badly wrong for JSON, code, and non-Latin scripts.
for comp in components: # stable to volatile order
text = comp.render()
while count(text) > comp.cap:
text = comp.degrade(text)
emit(text, tokens=count(text)) # per-component metric to the trace
That last line is the point most candidates miss. Emit per-component token counts into your traces. You cannot manage what you do not measure, and the near-universal finding on first instrumentation is that one tool is eating the majority of the window while everyone was arguing about the system prompt.
Two interactions worth flagging: order components stable to volatile so the cache prefix is long, and recognise that compaction is a cache-invalidation event, so compact in big chunks rather than every turn.
Worth sketching. The first arrow is the one worth pausing on: the budget is not the window, and everything downstream is divided out of the smaller number.
flowchart TD
A["200k window"] --> B["Usable budget: where the<br/>quality curve bends"]
B --> C["System and tools, 5k"]
B --> D["Session state, 2k"]
B --> E["Retrieved documents, 25k"]
B --> F["History, 50k"]
B --> G["Tool results, remainder"]
B --> H["Output reserve, 15%"]
C --> I["One ContextBuilder:<br/>cap, degrade, emit token counts"]
D --> I
E --> I
F --> I
G --> I
What a good answer covers
- Start by separating two numbers people conflate
- Context rot starts well below the limit, prefill latency scales with tokens, and cost scales with tokens
- Pick the budget from the quality-versus-length curve on your own eval, not from the model card
- On many agents that lands somewhere well under half the window
- Enforcement is architectural, not disciplinary
In the glossary: Context rot · Prefill · Model card / system card · Compaction
Interviewer follow-ups to expect
How would you find the quality-versus-context-length curve for your agent?
Why does chars/4 break as a token estimate, and where does it break worst?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep