ExAgent.Guardrail (ExAgent v3.0.0)

View Source

A blocking, decision-only capability. Put guards directly in the agent's :capabilities list. Within each stage the first tripwire or error wins.

Stability — Stable.

policy = ExAgent.Guardrail.input("size", fn
  text when is_binary(text) and byte_size(text) > 512 -> {:tripwire, "too_long"}
  _ -> :ok
end)

ExAgent.new(model: %ExAgent.Models.Test{}, capabilities: [policy])

A check receives one value and returns :ok, {:tripwire, info} or {:error, reason}. Use a stable policy name and an optional diagnostic code for info. Both identifiers accept 1–64 ASCII letters, digits, dots, underscores and hyphens. Tripwire events and telemetry omit info. An exception, throw, exit or invalid return is a capability failure.

Checks are trusted host functions. Keep them side-effect free. The final output guard gates delivery. A stream guard can release reviewed provisional text and commentary before the final output check. The tool-output guard runs after a confirmed effect. Progress can show its receipt while the guard checks the return. A veto leaves the effect status and usage with content_withheld: true.

In a root agent continuation, checks for stream windows, tool arguments, tool returns and final output are bound to the continuation's versioned :policy and to the ordered rule names and stages. Bump that version when changing a check's code or captured configuration. A resumed run requires its original policy. An argument guard checks the effective approved arguments before dispatch, including any review edit.

The candidate persisted handoff integration also binds the destination's rules. Set its :policy with ExAgent.Handoff.new/1 to version those rules separately. A destination's input check receives the bounded handoff view. Earlier tool and output checks continue to apply.

Summary

Functions

Check the new user's normalized prompt before the first model request.

Check the final value after output validation and any output function.

Review provisional model text and commentary before publication.

Check a function ToolCall with decoded, schema-validated args before authorization.

Check the confirmed ToolReturn after the after-tool hooks, before the next request.

Types

decision()

@type decision() :: :ok | {:tripwire, String.t() | nil} | {:error, term()}

stage()

@type stage() :: :input | :stream | :output | :tool_input | :tool_output

stream_window()

@type stream_window() :: %{text: String.t(), commentary: String.t()}

t()

@type t() :: %ExAgent.Guardrail{
  check: (term() -> decision()),
  name: String.t(),
  stage: stage(),
  window: %{max_bytes: pos_integer(), max_ms: pos_integer()} | nil
}

Functions

input(name, check)

@spec input(String.t(), (term() -> decision())) :: t()

Check the new user's normalized prompt before the first model request.

output(name, check)

@spec output(String.t(), (term() -> decision())) :: t()

Check the final value after output validation and any output function.

stream(name, check, opts \\ [])

@spec stream(String.t(), (stream_window() -> decision()), keyword()) :: t()

Review provisional model text and commentary before publication.

The check receives both channels for the current window, with an empty string for a channel absent from that window. A window closes at :max_bytes (default 1024, range 4–65,536), at :max_ms from its first text (default 50, range 1–1000), or at the end of the response. With several stream guards, their smallest limits apply to every check in order. Oversized deltas are split at UTF-8 boundaries. Separate windows do not overlap. Buffered model responses use the same byte windows.

tool_input(name, check)

@spec tool_input(String.t(), (ExAgent.Message.Part.ToolCall.t() -> decision())) :: t()

Check a function ToolCall with decoded, schema-validated args before authorization.

tool_output(name, check)

@spec tool_output(String.t(), (ExAgent.Message.Part.ToolReturn.t() -> decision())) ::
  t()

Check the confirmed ToolReturn after the after-tool hooks, before the next request.