Pick the right fallback when the assembled prompt overflows the model's context window
The right overflow fallback is a documented eviction order: sacred (system prompt + current turn + output reservation) protected, evictable (extra chunks, summary levels) trimmed in declared order.
Imagine a suitcase that will not close. Three bad options. First, refuse to go on the trip until the traveler removes things; rude, and they did not pack most of it themselves. Second, just sit on the lid and squish whatever happens to be at the bottom; you smash the medicine and the passport. Third, secretly trade the suitcase for a bigger one without telling them; they did not budget for a bigger suitcase. The right way is a packing list with priorities. The passport and medicine always stay. The third pair of shoes, the extra snacks, those come out first, in that order, until the suitcase closes. That order is the eviction order, and it is decided before the trip, not on the curb.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3 minutes: state the right answer is documented eviction, walk one failure mode for each of the three wrong answers, and name sacred tiers (system prompt + current user turn + output reservation).
| Option | Failure mode | When (if ever) right |
|---|---|---|
| Reject the request | Blames the user for system-side overflow | Only when user input alone exceeds budget after max eviction |
| Silent head-truncate | Drops system prompt and safety rails | Never; antipattern |
| Documented eviction order | Quality degrades gradually under budget pressure | Default for any production system |
| Silent model swap | Changes price and behavior without notice | Never silent; ok with an explicit declared ladder and logging |
Real products, models, and research that use this idea.
- Anthropic Claude Code applies a compaction step before overflow: stale tool results and older summary levels are evicted from working context while CLAUDE.md and the active task block stay sacred.
- LangGraph's trim_messages with strategy='last' preserves recent turns and the system prompt while trimming older history.
- LlamaIndex ContextChatEngine has a token_limit and a documented trim order (system → recent → summary → retrieved) that mirrors the production-correct pattern.
- Cursor's background agents declare an explicit model ladder (Claude Sonnet 4.6 → Opus 4.7 → Opus 4.7 with extended context) with logged escalations.
- Vercel AI SDK's experimental_telemetry surfaces eviction events so the consumer of the API can audit when a prompt was trimmed and how many tokens were removed.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhat does a good error message look like when reject is the correct response?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Picking head-truncation as the simple default. It drops the system prompt first, which is exactly the tier that should be untouchable.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.