Lesson 16 / 25
Keeping Tool Output Under Control
Large tool results can flood the context of an agent.
Truncate, summarise, or fetch on demand
Agents call tools that can return huge results: logs, web pages, database dumps. Put the raw result into the context and a single call can consume the whole window. Options: truncate with head and tail (errors are often at the end), summarise or extract the fields needed, paginate (let the agent request more), or store the full result and give the model a handle it can query. Always mark tool output as untrusted data, and tell the model when content was cut so it does not assume it saw everything.
Head-and-tail truncation of a long log, run
I ran this with plain Python 3 (standard library only); the data is made-up example data. A 501-line log (6,417 characters) is cut to the first three and last three lines plus an omission marker (112 characters); the final ERROR line survives.
log = "\n".join(f"line {i}: ok" for i in range(1, 501)) + "\nline 501: ERROR disk full"
def head_tail(text, keep=6):
lines = text.splitlines()
if len(lines) <= 2 * keep:
return text
cut = len(lines) - 2 * keep
return "\n".join(lines[:keep] + [f"... [{cut} lines omitted] ..."] + lines[-keep:])
short = head_tail(log, keep=3)
print(short)
print("chars:", len(log), "->", len(short))
Output:
line 1: ok line 2: ok line 3: ok ... [495 lines omitted] ... line 499: ok line 500: ok line 501: ERROR disk full chars: 6417 -> 112
Design tools to return less
Give tools parameters like limit, fields and filters so they return only what the task needs.
Quick check: Why keep the tail when truncating a long log?
- Models only read the tail
- The tail is always shorter
- Errors and final status often appear at the end
- The head is never useful
Answer
Errors and final status often appear at the end — Head-and-tail keeps context and outcome.