A guardrail is a named check run at one boundary of an agent: its input (the
user text), its output (the model's reply), or a tool call (a tool's name and
arguments before it runs, or its result after). The check returns TRUE to
pass, or a short reason string to fail. On failure the guardrail either
blocks (raises a typed condition and records the event), warns, or merely
flags; in every case the decision is recorded as an event, so a blocked input
is analyzable rather than invisible.
Arguments
- name
A short label for the guardrail (appears in events).
- check
A function
(payload, context) -> TRUE | reason.payloadis the text (input/output) or alist(name=, arguments=, result=)(tool).contextis a small list withstage,agent, andphase("pre"or"post"for tools). ReturnTRUEto pass, or a non-empty character reason to fail.- on_fail
What to do on failure:
"block"(raisellmragent_guardrail_blockand stop),"warn"(warn and continue), or"flag"(record only). Default"block".- stage
Which boundary:
"input","output", or"tool".
Examples
no_pii <- guardrail(
"no_ssn",
function(payload, context) {
if (grepl("\\b\\d{3}-\\d{2}-\\d{4}\\b", payload)) "contains an SSN" else TRUE
},
stage = "input"
)