ExAgent.Tool.OutputLimit (ExAgent v2.3.0)

View Source

Capability that shortens large successful tool results before the model sees them.

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. By default every result below the run's :max_payload_bytes retention ceiling (1 MiB by default) is kept whole. That ceiling is checked before any hook runs, so a larger result still fails the run with this capability installed.

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. Results within the limit, failures and retries are left unchanged.

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 (default 32_768, minimum 256). The shortened result, notice included, fits within it.
  • :keep — :head_tail (default), :head or :tail.
  • :tools — :all (default) or a list of tool names to limit.
  • :on_truncate — optional fn 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 (for example, how to page through the stored copy), cut if needed so the result still fits :max_bytes.

The shortened result is what later model requests, the conversation history and events see. It runs as an after_tool_execute hook, so list it after capabilities that should see the full result. The tool's raw outcome is confirmed before hooks run: a persisted continuation's effect record, and a run that fails while the hook runs (for example, a slow :on_truncate reaching the tool timeout), can still hold the full result. Keep :on_truncate fast.

Summary

Types

t()

@type t() :: %ExAgent.Tool.OutputLimit{
  keep: :head_tail | :head | :tail,
  max_bytes: pos_integer(),
  on_truncate: (map() -> String.t() | nil) | nil,
  tools: :all | [String.t()]
}

Functions

after_model_request(cap, state)

Callback implementation for ExAgent.Capability.after_model_request/2.

before_model_request(cap, state)

Callback implementation for ExAgent.Capability.before_model_request/2.

before_tool_execute(cap, ctx, tool_call)

Callback implementation for ExAgent.Capability.before_tool_execute/3.

new(opts \\ [])

@spec new(keyword()) :: t()

Build a validated limit. Raises ArgumentError for invalid options.