GuardrailMiddleware

org.llm4s.agent.graph.middleware.GuardrailMiddleware
See theGuardrailMiddleware companion object
final class GuardrailMiddleware(input: Seq[InputGuardrail], output: Seq[OutputGuardrail], val id: MiddlewareId) extends AgentMiddleware

Runs input guardrails on the run's input (beforeAgent) and output guardrails on its final answer (afterAgent). Each list runs in order, each guardrail on the value the previous one returned, so a Fix transforms; a Warn passes. Failures are collected into one GuardrailBlocked, which blocks the run (design 4.13): it ends as a finished failure, an input block storing nothing of the turn and an output block removing it, and the thread stays usable. Agent reports it as AgentStatus.Blocked. A guardrail does not suspend.

On a root agent it guards the whole agent family: its input guardrails run on every turn's query and its output guardrails on every final answer, whichever agent is active after a handoff. On a handoff target it applies only while that agent is active, inside the root's.

Attributes

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

Members list

Value members

Concrete methods

override def afterAgent(answer: String, context: ModelContext): Result[BoundaryResult]

Sees the run's final answer: returns it, possibly changed, with an update to this middleware's writes, or Left to block the run.

Sees the run's final answer: returns it, possibly changed, with an update to this middleware's writes, or Left to block the run.

Attributes

Definition Classes
Source
GuardrailMiddleware.scala
override def beforeAgent(text: String, context: ModelContext): Result[BoundaryResult]

Sees the run's input before anything else runs: returns it, possibly changed, with an update to this middleware's writes that the turn's first model call sees, or Left to block the run.

Sees the run's input before anything else runs: returns it, possibly changed, with an update to this middleware's writes that the turn's first model call sees, or Left to block the run.

Attributes

Definition Classes
Source
GuardrailMiddleware.scala

Inherited methods

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: MiddlewareStack.of - so 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. Register the two in the right order, or name other in runsBefore. Name other by its type: ids are configurable.

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: MiddlewareStack.of - so 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. Register the two in the right order, or name other in runsBefore. Name other by its type: ids are configurable.

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); each must be registered in the same stack.

Middleware this one must run inside of (be wrapped by); each must be registered in the same stack.

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala

Middleware this one must run outside of (wrap); each must be registered in the same stack.

Middleware this one must run outside of (wrap); each must be registered in the same stack.

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

Wraps one model call. A wrapper may rewrite the request (inject a note, filter tools), call next more than once (retry, fallback), transform its response, add an update to its writes (ModelResponse.addUpdate), or return Left, which fails the run. Only an AgentMiddleware.Asking wrapper suspends, by asking its question. Every call next makes is counted in the thread's usage, whichever response the wrapper returns.

Wraps one model call. A wrapper may rewrite the request (inject a note, filter tools), call next more than once (retry, fallback), transform its response, add an update to its writes (ModelResponse.addUpdate), or return Left, which fails the run. Only an AgentMiddleware.Asking wrapper suspends, by asking its question. Every call next makes is counted in the thread's usage, whichever response the wrapper returns.

Filtering ModelRequest.tools shapes what the model is offered and is not a permission control: a tool the model calls anyway still runs through wrapToolCall, where denial belongs.

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala
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, and short-circuits by not calling next. It may call next more than once (retry) and transform the outcome next returns.

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, and short-circuits by not calling next. It may call next more than once (retry) and transform the outcome next returns.

Attributes

Inherited from:
AgentMiddleware
Source
AgentMiddleware.scala
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

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
GuardrailMiddleware.scala