An agent has a 128k window and may run up to 50 iterations. Design a token budget policy that does not run out of room halfway through a long run. Cover the static prefix, summary growth, scratchpad, and tool results.
Carve the window into a small static prefix, a small persistent memory slot, a reserved output slot, and a large working area governed by a hierarchical summary plus soft and hard watermarks that trigger compaction
Think of your suitcase before a 50-day trip. You cannot bring 50 fresh outfits, it will not fit. So you set aside one pocket for documents (the static prefix, never changes), one pocket for essentials (persistent memory), and you keep one corner empty for the souvenir you will pack last (the output). The rest of the suitcase is for clothes, and you cycle them: every five days you bundle the dirty ones into one compact pouch (a level-one summary). When the suitcase looks 80 percent full, you re-roll the pouches. When it looks 95 percent full, you bundle the oldest pouches together into one even smaller bundle. The shape never changes, so you always have room for tomorrow.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Designing an iteration-aware token budget is what separates demo agents from production agents. A 128k window across 50 iterations sounds generous on paper, but without explicit budget discipline the scratchpad, the tool results, and the message history will combine to overflow somewhere between iteration 20 and iteration 35. The fix is not a bigger model, it is a budget design that keeps the prompt roughly flat in size across the trajectory. This deep dive walks through the four-slot layout, the hierarchical summary that governs the working area, the tool-result discipline, and the watermark policy that ties it all together.
Why a flat window is not enough
A naive long-running agent appends every model reply, every tool call, and every tool result to a growing message list. Over 50 iterations even modest per-turn additions compound: 2k of scratchpad plus 3k of tool result per turn is 250k after 50 turns, which is roughly twice the 128k window.
The agent does not crash gracefully. What happens is that around iteration 25 the prompt approaches the window, the provider truncates the oldest messages (silently in some APIs, with an error in others), and from that point the agent has forgotten its goal, its plan, and its earlier tool results. The trajectory continues but the work is wasted.
The insight that fixes this is that the window is not the budget. The budget is what is left after you reserve room for the things the agent always needs: the system prompt, the persistent memory, and the output. The working area is everything else, and it must be governed by an eviction policy that runs on every iteration, not just when overflow is imminent.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Anthropic's Claude Opus 4.7 agent loops in production use exactly this slot plus watermark discipline for multi-hour code agents
- LangGraph's MessagesState reducer combined with a summarization node implements the hierarchical summary pattern directly
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you tune the watermark thresholds for a latency-sensitive vs a cost-sensitive agent?
Latency-sensitive agents want compaction to happen rarely because a compaction pass is an extra LLM call. Raise the soft watermark to 90 percent and let evictions happen more often. Cost-sensitive agents want the prompt to stay small, so lower the soft watermark to 70 percent and accept the extra compaction cost. Replay representative trajectories to find the sweet spot for the workload.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Treating the 128k window as one undifferentiated pool, then watching the agent crash at iteration 30 when the unbounded scratchpad and uncapped tool results have eaten the whole budget.
60 second bullets to scan on the way to the call.
Name the four slots and the approximate size of each
Explain why the static prefix must stay byte-stable
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.