Why cap tool output size at the tool boundary instead of letting the model deal with it?
Capping tool output at the wrapper keeps a single tool call from torching the context budget and triggering lost-in-the-middle, and the wrapper has structured-format knowledge the model lacks.
Picture a research assistant who can fetch books from a library for you, and your desk only fits five books at a time. If the assistant dumps every book they can find onto the desk, half of them slide off and the ones underneath are buried. A good assistant skims each book first, hands you the relevant chapter, and writes the rest of the title on a sticky note so you can ask for more if you need it. The tool wrapper is that assistant. The model is you at the desk. Without a cap at the wrapper, one tool call can bury everything else on the desk, including the question you were trying to answer. With a cap, the desk stays usable and the agent can ask for more on demand.
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.
Tool wrappers are the seams between the model and the outside world. They take the model's structured tool call, invoke an external service, and hand the result back into context. The single most consequential design decision at this seam is the policy for output size, how much of the underlying response actually reaches the model, and in what shape.
Getting this wrong is one of the most common reasons agent systems break under their own weight. A few uncapped tool calls into a long trajectory and the context is full of pages of HTML the model is not really reading, the user's original question has slid into the lost-in-the-middle trough, and the cost per turn has quadrupled. This deep dive walks through why capping at the tool boundary is the canonical pattern, how to choose the right capping strategy per tool, and what failure modes show up when the cap is too tight or too loose.
Why the model cannot self-truncate a tool result
It is tempting to imagine the model handling tool-result size on its own. Just give it the whole response and trust it to focus on the useful parts. This intuition is wrong, and the reason is mechanical.
The model has no operation for "read only the first N tokens of the tool result." The result is a single tool_result message in the conversation, and from the model's perspective it is one chunk of text alongside the assistant's turns and the user's turns. Attention spreads across the whole message proportional to learned weights, not according to a relevance filter the model can apply on the fly. Whatever the wrapper returns is what gets attended to.
This is fundamentally different from how a human runs a tool. A human running cat large_file.txt sees the terminal start scrolling, hits Ctrl-C, and runs head or grep instead. The model has no such interrupt. It does not see the result streaming in, it sees a completed message, all at once, after the tool call finishes. The decision of how much to read has already been made by the time the model gets the result.
This is why the wrapper, not the model, is the right place to enforce size limits. The wrapper has the choice of what to return. The model only has the choice of how to interpret what was returned.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Anthropic's Claude Code caps Bash and file-read tool outputs at a few thousand tokens by default and appends a 'use grep for more' hint when truncated.
- Cursor's agent mode wraps file-read with a chunked by symbol reader that returns one function or class at a time rather than the whole file.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow do you decide between hard truncation, top-N extraction, and summarization for a given tool?
It depends on the source structure and the cost budget. Structured sources (JSON APIs, databases) get field projection, return only the keys the agent asked about. Semi-structured sources (HTML pages, markdown docs) get top-N extraction by section relevance, often with a small reranker. Truly unstructured very-large sources (log files, transcripts) get a summarizer pass when latency allows, or a chunked-pagination tool when it does not. Hard byte truncation is the last resort and should always be paired with a 'truncated' marker.
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 tool outputs as raw passthroughs and assuming the model will handle truncation on its own, when in fact the model sees the entire blob and burns budget over it.
60 second bullets to scan on the way to the call.
State why the model cannot self-truncate a tool result
Connect tool-output bloat to the lost-in-the-middle effect
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.