Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Bound each tool result at the tool boundary and stash overflow out of band, the bloat is input tokens from raw tool payloads, not anything the model is choosing to write.
Imagine a detective taking notes during interviews. After ten interviews, the notebook is so thick the detective can barely lift it, and most pages are coffee-shop menus and parking receipts the witnesses happened to be holding. The fix is not buying a bigger notebook. The fix is for the assistant who hands notes over to keep one short paragraph per interview and file the receipts in a drawer with a label. If the detective needs a receipt later, they can ask for it by label. That is what capping tool results does for an agent, the drawer is out of band storage, the label is the reference token in context.
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.
8 min: per-iteration growth math, the tool boundary fix, why bigger windows don't help, eviction and prompt-cache interactions.
# Tool wrapper that caps and stores overflow out of band
from uuid import uuid4
MAX_TOOL_TOKENS = 1024 # roughly 1KB
def wrap_tool_result(raw: str, store) -> dict:
if len(raw) <= MAX_TOOL_TOKENS * 4: # ~4 chars/token
return {"kind": "inline", "content": raw}
head = raw[: MAX_TOOL_TOKENS * 2]
tail = raw[-MAX_TOOL_TOKENS * 2 :]
ref_id = uuid4().hex[:8]
store.put(ref_id, raw)
return {
"kind": "capped",
"head": head,
"tail": tail,
"elided_bytes": len(raw) - len(head) - len(tail),
"ref_id": ref_id,
"fetch_hint": f"call fetch_blob(ref_id='{ref_id}', offset=..., length=...) to read more",
}Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Asking the model to be brief, that controls output tokens; the bloat lives on the input side, where the model has no agency.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.