Skip to main content
Guardrails check the user prompt and the model’s text. Register them with WithGuardrail. A deny ends the run. A modify replaces the text the rest of the run uses. Hooks still run. They do not see the original user prompt, and they cannot undo an output deny or an output modification. See Hooks for tool arguments, tool results, retrieved documents, and memory.

Register

Order matters. A modify is the text the next guardrail sees, and the first deny stops the chain. Call WithGuardrail more than once to append. On Temporal, the starter and the worker must register the same guardrails in the same order.

What is checked

The input stage runs once, before conversation history, memory recall, and retrieval. The model, the recall query, the retriever query, and anything persisted all see the modified prompt. An input deny writes nothing and does not call the model. The output stage runs on every model response, after your AfterLLM hooks. It is skipped when the response has tool calls and empty text. Input.Final is true when the response has no tool calls. AfterLLM hooks see the model’s original text. A logging hook there records text that an output guardrail may still redact. Put that redaction in the hook when the log must be redacted too. Text a BeforeLLM hook writes into the request is not checked again. Input guardrails cover the user prompt. Each agent checks its own loop. A sub-agent’s input stage sees the delegated query. Parent guardrails are not copied onto the child.

Decisions

errors.Is(err, agent.ErrGuardrailBlocked) works on the local, Temporal, and Restate runtimes. On the local runtime, errors.As also yields *guardrail.Error with the guardrail name, stage, and reason. A deny is not retried and is not sent to error-control fallback. If Evaluate returns an error, or the decision action is empty or unknown, the step fails and retries under that step’s policy. That failure is not ErrGuardrailBlocked. An output-stage error retries the whole model call, so the model runs again. Keep output guardrails local, or make them handle their own retries.

Built-in checks

All of these use the standard library. Pass guardrail.StageInput, guardrail.StageOutput, or both, except JSON, which is output only. The deny reasons for MaxLength, DenyList, and JSON do not include the user text.

Custom checks

guardrail.New does not check the function. A nil function fails the step when it runs. Guardrails must be safe for concurrent Run and Stream calls.

Streaming

When any output guardrail is registered, assistant text is held and sent once, after the check. A modify is that one message. A deny sends no assistant text. Reasoning deltas still stream and are not checked. Input-only guardrails leave token streaming unchanged.

Timeouts

WithGuardrailExecutionConfig sets the timeout and attempts for the input check. The default is 5 minutes and 3 attempts. Output checks run inside the model call and use WithLLMExecutionConfig.

Not checked

Tool-call arguments, tool results, retrieved documents, memory context, the system prompt, and reasoning deltas are not checked. Use hooks for those.