How does streaming tool output complicate context budgeting in agent loops?
Streaming tools force the wrapper to choose when to stop reading: timeout, size, or sentinel, each with its own failure mode, and to pair the cutoff with a continuation tool the model can call to resume.
Imagine listening to a friend tell a story while you have a meeting in five minutes. You cannot make them stop mid-sentence and you also cannot wait for the ending. You have three reasonable choices. Set a timer and politely interrupt when it goes off. Decide ahead of time you will leave after a certain number of details. Or wait for a natural pause (the end of a chapter) and exit there. Each one cuts the story off in a different way, and the good move is to also promise your friend you will come back later and pick up where you left off. Streaming tools are exactly this: pick a stopping rule, accept that some endings will be jagged, and give the model a way to resume.
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.
Streaming tools are where context engineering meets distributed-systems engineering. Most of the agent-loop design space assumes tools are synchronous: the model calls a tool, the tool runs to completion, and the result lands in context as one complete message. Streaming tools, long-running shell commands, SSE feeds, build pipelines, database row streams, even model-inference subprocesses, break that assumption. They produce partial output continuously, and the wrapper has to make decisions about when to interrupt and yield to the model, how much to read in each yield, and how to resume.
This deep dive walks through the three canonical stopping criteria and their failure modes, the layered stopping pattern that production agents converge on, the continuation handle that turns a single long stream into a sequence of small bounded reads, and the secondary concerns of back-pressure and stall detection.
Why streaming forces a decision synchronous tools dodge
Synchronous tools have a simple contract: the model calls, the tool runs, the wrapper waits, the result comes back complete and bounded. The wrapper's job is just to forward the call and forward the response. There is no decision about when to return, the underlying tool decides.
Streaming tools invert this. The underlying process emits output over time without a clear end. A pytest -v invocation in a coding agent can run for ten minutes and produce 50K tokens of progress lines before the final summary. A streaming database query can produce rows for hours. A live API feed (stock ticker, server health stream, model inference subprocess) may never end at all by design.
The wrapper now has to make a choice the synchronous case never asked: when to stop reading and hand control back to the model. Reading too little misses the signal. Reading too much blows the budget. Reading forever hangs the agent. There is no default that works.
The wrapper is the right layer for this choice for the same reason it was the right layer for output capping in the synchronous case: it has format-aware structural knowledge of the underlying stream, while the model only sees the final text blob. The model cannot interrupt a stream that has not been handed to it yet.
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 bash tool streams shell-command output with a default timeout, a per-call token cap, and a 'truncated' marker plus a hint to run a narrower command.
- Cursor's terminal integration streams output to the agent in bounded chunks with the model able to issue a continue or kill operation.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow do you decide between buffering early stream content versus late stream content when truncating?
It depends on where the signal lives in the stream. For build / test / compile output, the signal is at the end (failure summary, exit status), so keep the tail and drop the middle. For document streams, file headers, or JSON documents, the signal is at the start (schema, top-level keys), so keep the head. For mixed streams, mark both ends and elide the middle with a note. Some wrappers expose a stream-shape hint as a config so the policy is per-tool.
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 streaming as a synchronous tool by buffering the whole stream, the buffer either blows the budget or holds the agent for minutes; the policy choice is structural.
60 second bullets to scan on the way to the call.
Name the three stopping criteria: timeout, size, marker
Explain a failure mode for each
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.