ToolOutputOffload

org.llm4s.agent.harness.ToolOutputOffload
See theToolOutputOffload companion object
@Experimental
final class ToolOutputOffload(val thresholdTokens: Int, val maxResultBytes: Long, val maxTotalBytes: Long, val id: MiddlewareId) extends AgentMiddleware

Keeps a large tool result out of the conversation (design ยง4.34). A Success whose text is over thresholdTokens estimated tokens (four characters a token) is stored in ToolOutputOffload.key, cut to maxResultBytes, and the model is shown instead a header naming the path /large_tool_results/<idempotencyKey> and the first lines of the text. OffloadBackend, mounted at ToolOutputOffload.Mount in a CompositeBackend, serves the stored results to file tools, read-only.

  • Identity. The path is the call's IdempotencyKey, the runtime's identity of the call: a provider that reuses a tool call id cannot make an older result's path name a newer call's content, and a rerun of the same call (recover) replaces its own entry.
  • The bound. The stored results are at most maxTotalBytes, as a checkpoint stores them: each update evicts the oldest until they fit, when it is applied, so the bound holds under a parallel batch and whether or not a SummarizationMiddleware runs. Reading an evicted path is a tool error that says so.
  • Content parts. Only text is offloaded. Every non-text part of a result is passed on as it is, after the preview.
  • Cleanup. With a SummarizationMiddleware, results whose path appears in a tool result the summary covers are dropped at the next model step. The bound does not depend on it. The summariser runs outside this middleware - build() refuses the other order (SummarizationMiddleware.placement).

An output Block restores the key, so a blocked turn's results are never kept.

Value parameters

maxResultBytes

the most bytes of one stored result; at least 1 and at most maxTotalBytes

maxTotalBytes

the most bytes all stored results take together

thresholdTokens

a result over this many estimated tokens is offloaded; at least 1000

Attributes

Companion
object
Source
ToolOutputOffload.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

Sends every tool result of the request within the threshold, whoever produced it: an outcome given instead of running the tool - the loop's argument, approval and timeout errors, a denial from a middleware outside this one - never reaches wrapToolCall, so its text is cut here, as it is sent. A Success over the threshold was already offloaded. Media parts are kept.

Sends every tool result of the request within the threshold, whoever produced it: an outcome given instead of running the tool - the loop's argument, approval and timeout errors, a denial from a middleware outside this one - never reaches wrapToolCall, so its text is cut here, as it is sent. A Success over the threshold was already offloaded. Media parts are kept.

Attributes

Definition Classes
Source
ToolOutputOffload.scala
override def wrapToolCall(request: ToolCallRequest, context: ToolContext)(next: () => ToolOutcome): ToolOutcome

Wraps one tool call, after its arguments were validated. A wrapper denies with ToolOutcome.Error("Denied: ..."), asks for approval with NeedsApproval(reason) (unless context.approved), fails the run with Fatal - its Left: the run fails with GraphError.ToolFailed - and short-circuits by not calling next. It may call next more than once (retry) and transform the outcome next returns, adding an update to its writes with ToolOutcome.addUpdate. next takes no request: the call is read-only.

Wraps one tool call, after its arguments were validated. A wrapper denies with ToolOutcome.Error("Denied: ..."), asks for approval with NeedsApproval(reason) (unless context.approved), fails the run with Fatal - its Left: the run fails with GraphError.ToolFailed - and short-circuits by not calling next. It may call next more than once (retry) and transform the outcome next returns, adding an update to its writes with ToolOutcome.addUpdate. next takes no request: the call is read-only.

Attributes

Definition Classes
Source
ToolOutputOffload.scala
override def writes: Set[StateKey[_, _]]

The state keys this middleware's hooks may write: an update from beforeAgent, wrapModelCall or afterAgent, or one wrapToolCall adds to a Success. A hook's write to any other key fails the run.

The state keys this middleware's hooks may write: an update from beforeAgent, wrapModelCall or afterAgent, or one wrapToolCall adds to a Success. A hook's write to any other key fails the run.

Attributes

Definition Classes
Source
ToolOutputOffload.scala

Inherited methods

def admit(query: String, context: AdmissionContext): Result[String]

Admits the turn's query before anything of the turn is stored (#2854): returns it, possibly rewritten, or refuses it. Each middleware's stage runs in stack order on the query the one before returned, and all of them before any beforeAgent.

Admits the turn's query before anything of the turn is stored (#2854): returns it, possibly rewritten, or refuses it. Each middleware's stage runs in stack order on the query the one before returned, and all of them before any beforeAgent.

  • Right(query) admits the turn with that text: it is what the turn's first checkpoint, its run queue entry and every later hook hold, and the query as submitted is stored nowhere. Redact here, not in beforeAgent.
  • block refuses it: the turn ends AgentStatus.Blocked(id, reason), and nothing of the query is stored - the blocked run records which middleware refused, and its GuardrailBlocked event, and no text.
  • any other Left refuses to start the turn: agent.run returns it as its Left, the thread unchanged and no run started. A blank query returned is refused the same way.

It decides and rewrites, and nothing else: it has no thread state to read, returns no update, cannot ask a question (a middleware that asks from here is refused) and has no tool to call; its context offers only the turn's identity and a way to count a model call. Run before the turn is stored, it runs once for the turn, however often the turn's input step runs afterwards - a recover or a resume runs beforeAgent again, never this - and its verdict is final: a later change of mind is beforeAgent's.

It runs before the thread is claimed, so a turn the runtime then refuses (ThreadBusy, PendingInterrupts, ...) has had its stages run, and a model call one of them made is counted in no thread's usage.

It runs before the query is stored whenever the turn starts through Agent.run, start or stream, and when a queue's submitter calls Agent.admit first. A turn that is stored before the agent sees it - a query put on a run queue as it is, a delegated child's task - runs the stage as the first thing of its input step instead: the verdict is the same, but that query was already stored by whoever stored it, and the stage runs again if that step does (a recover of a step that failed).

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala

Sees the run's final answer: returns it, possibly changed, with an update to this middleware's writes. A Left blocks the turn, removing it; block blocks it as an outcome, AgentStatus.Blocked.

Sees the run's final answer: returns it, possibly changed, with an update to this middleware's writes. A Left blocks the turn, removing it; block blocks it as an outcome, AgentStatus.Blocked.

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala

Sees the run's input, as admitted, before anything else of the turn runs: returns it, possibly changed, with an update to this middleware's writes that the turn's first model call sees. A Left blocks the turn; block blocks it as an outcome, AgentStatus.Blocked. The query it is given is already in the turn's first checkpoint: to keep text out of the store, rewrite or refuse it in admit. On a handoff target it also runs at the handoff (#3312), after the target's admit stages, on the turn's stored query and before the target's first model call; its update commits with the handoff, and a question it asks suspends the run with the handoff stored.

Sees the run's input, as admitted, before anything else of the turn runs: returns it, possibly changed, with an update to this middleware's writes that the turn's first model call sees. A Left blocks the turn; block blocks it as an outcome, AgentStatus.Blocked. The query it is given is already in the turn's first checkpoint: to keep text out of the store, rewrite or refuse it in admit. On a handoff target it also runs at the handoff (#3312), after the target's admit stages, on the turn's stored query and before the target's first model call; its update commits with the handoff, and a question it asks suspends the run with the handoff stored.

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala

Whether this middleware's afterAgent can block or rewrite the final answer (#3231). With streaming (withStreaming()), the answer is then not sent to listeners as the model writes it: the text of a model call that makes no tool call is held, and the answer is sent as one TextDelta, as stored, once every afterAgent of the agent (and, for a handoff target, of the root) has passed it; a blocked answer is never sent. Text the model writes beside a tool call is sent when the call returns. false by default, so a middleware whose afterAgent only reads the answer leaves streaming live; GuardrailMiddleware and GuardrailReviewMiddleware are true when they have output guardrails, and a middleware whose afterAgent blocks or rewrites should override it too.

Whether this middleware's afterAgent can block or rewrite the final answer (#3231). With streaming (withStreaming()), the answer is then not sent to listeners as the model writes it: the text of a model call that makes no tool call is held, and the answer is sent as one TextDelta, as stored, once every afterAgent of the agent (and, for a handoff target, of the root) has passed it; a blocked answer is never sent. Text the model writes beside a tool call is sent when the call returns. false by default, so a middleware whose afterAgent only reads the answer leaves streaming live; GuardrailMiddleware and GuardrailReviewMiddleware are true when they have output guardrails, and a middleware whose afterAgent blocks or rewrites should override it too.

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala

Where this middleware must run relative to other, when both are present; None, the default, when it does not matter. A requirement is checked, never arranged: AgentBuilder.build() refuses a stack in which this middleware does not run outside other, and a Placement.OutsideAtBoundary is checked across an agent family too, which runsBefore cannot arrange. Register the two in the right order, or name other in runsBefore. other is the middleware itself, so match it by its type, as a MiddlewareRef names one, or by other.id.

Where this middleware must run relative to other, when both are present; None, the default, when it does not matter. A requirement is checked, never arranged: AgentBuilder.build() refuses a stack in which this middleware does not run outside other, and a Placement.OutsideAtBoundary is checked across an agent family too, which runsBefore cannot arrange. Register the two in the right order, or name other in runsBefore. other is the middleware itself, so match it by its type, as a MiddlewareRef names one, or by other.id.

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala

Content for the system prompt of each model request: collected before every model step from the committed state, in stack order, and never stored. Each middleware bounds its own; the agent bounds them all together (maxPromptSectionTokens). Left fails the run.

Content for the system prompt of each model request: collected before every model step from the committed state, in stack order, and never stored. Each middleware bounds its own; the agent bounds them all together (maxPromptSectionTokens). Left fails the run.

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala

Middleware this one must run inside of (be wrapped by), named and arranged as runsBefore's are.

Middleware this one must run inside of (be wrapped by), named and arranged as runsBefore's are.

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala

Middleware this one must run outside of (wrap): the stack is arranged so. Each is named by id or by type (MiddlewareRef) and must be in the same stack, unless the reference is optional (ifPresent).

Middleware this one must run outside of (wrap): the stack is arranged so. Each is named by id or by type (MiddlewareRef) and must be in the same stack, unless the reference is optional (ifPresent).

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala

Tools this middleware contributes; they join the loop's tool set and are validated like any other.

Tools this middleware contributes; they join the loop's tool set and are validated like any other.

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala

Concrete fields

This middleware's identifier, unique in its stack; must match [a-zA-Z0-9_-]{1,64}.

This middleware's identifier, unique in its stack; must match [a-zA-Z0-9_-]{1,64}.

Attributes

Source
ToolOutputOffload.scala

Attributes

Source
ToolOutputOffload.scala