ExAgent. Tool. OutputLimit
(ExAgent v3.0.0)
View Source
Capability that shortens large successful and :tool_error results before the model sees them.
Stability — Candidate.
A tool result stays in the conversation and is sent again on every later step,
so one large file read or command log can dominate the context window and the
bill. Before result hooks run, ExAgent checks the whole tool return against
its reservation. Each return gets at most :max_payload_bytes (1 MiB by
default). Calls in the same step share the remaining history space, so each
reservation can be smaller. If a return exceeds it, the run fails after
the tool executes and :on_truncate never receives that result.
Results larger than :max_bytes are replaced by their beginning and end with a
notice saying how much was omitted, so the model can ask for a narrower read
instead of guessing. Text is cut on UTF-8 character boundaries. Non-string
results are measured and cut as their JSON encoding, so the model receives a
string. A {:tool_error, message} result (for example an MCP isError
reply) is shortened the same way. Results within the limit, fatal failures
and retries are left unchanged.
An oversized ExAgent.Tool.Content becomes an omission notice with its image
count. The UTF-8 ResultStore and :on_truncate handle text and JSON only.
ExAgent.new(model: model, tools: tools, capabilities: [ExAgent.Tool.OutputLimit.new(max_bytes: 16_000)])Options (struct fields):
:max_bytes— largest result kept whole, in bytes (default32_768, minimum256). The shortened result, notice included, fits within it.:keep—:head_tail(default),:heador:tail.:tools—:all(default), a list of names to limit, or{:except, names}to limit every tool whose name is outside that list.:offload— optional{ExAgent.Tool.ResultStore.ETS, server}or anotherExAgent.Tool.ResultStoreimplementation. Addsread_resultandgrep_resultto the inventory. Only the originating run may read a reference. The preview begins with the reference and full byte size. If storage fails, the preview says so. Readers enforce their own limits. Choose either this option or:on_truncate.:on_truncate— optionalfn info -> String.t() | nil end, called with%{tool_name, tool_call_id, content, bytes}holding the full result, before it is shortened. Use it to keep the full output elsewhere; a returned string is added to the notice when it fits in full (for example, how to page through the stored copy). An oversized hint leaves the usual preview intact. A hint that fits exactly can occupy all the preview space. A tool call ID correlates the result with a call in one model response. When storing full results, return a fresh storage reference after each successful write, ornilto show just the omission notice. Return the hint as valid UTF-8 text. Encode opaque binary references in the host, for example withBase.url_encode64(ref, padding: false). A malformed hint leaves the preview and its omission notice intact.
After-tool hooks run in reverse list order, so list this capability
before the capabilities whose after_tool_call/4 should see the full
result. In an ordinary
run, progress uses the shortened result once the runtime observes completion
of the tool task. A Server interrupted after that update retains the shortened
result in its history. Snapshots taken earlier, including runs interrupted
during :on_truncate, and a persisted continuation's effect record can hold
the full result. Filter sensitive data before returning it from the tool and
keep :on_truncate fast.
With offload, a schema-free callable's retained return becomes a preview
before the runtime confirms its outcome. Later hooks see that preview.
For a tool with :result_schema, result hooks and validation see the full
business value before this capability projects it. A continuation can retain
the raw version of that return. A delegated child's terminal output and an
externally supplied result also have independently recorded sources.
Summary
Functions
Build a validated limit. Supply each option once. Duplicate options and
invalid field values raise ArgumentError.
Extract the opaque reference from an offloaded result preview, or return nil.
Types
@type t() :: %ExAgent.Tool.OutputLimit{ keep: :head_tail | :head | :tail, max_bytes: pos_integer(), offload: ExAgent.Tool.ResultStore.t() | nil, on_truncate: (map() -> String.t() | nil) | nil, tools: :all | [String.t()] | {:except, [String.t()]} }
Functions
Build a validated limit. Supply each option once. Duplicate options and
invalid field values raise ArgumentError.
Extract the opaque reference from an offloaded result preview, or return nil.