Quick Example
How It Works
Three checkpoints run automatically on everyarun() / arun_stream() call:
- Input — after memory recall, before the model call. Can block or modify the user message.
- Tool — inside the tool call loop, before each tool execution. Blocked tools send an error result back to the model.
- Output — after the model response, before memory store. Can block, modify, or redact the response.
Guardrails Constructor
List[InputGuardrail]
default:"[]"
Guardrails that check the user message before the LLM call.
List[OutputGuardrail]
default:"[]"
Guardrails that check the model response after the LLM call.
List[ToolGuardrail]
default:"[]"
Guardrails that check each tool call before execution.
str
default:"fail_fast"
"fail_fast" stops at the first block. "run_all" runs every guardrail and collects all results.str
default:"raise"
"raise" throws InputCheckError or OutputCheckError. "return_message" returns a RunOutput with status=RunStatus.blocked.Built-in Guardrails
Input Guardrails
InputGuardrail
Blocks input that exceeds
n tokens. Uses the specified model’s tokenizer for counting.InputGuardrail
Blocks input containing any keyword from the
topics list (case-insensitive substring match).InputGuardrail
Blocks or redacts input matching any of the given regex patterns. Set
action="modify" to redact matches instead of blocking.Output Guardrails
OutputGuardrail
Detects PII (credit cards, SSN, email, phone) and redacts it with tokens like
[CREDIT_CARD], [SSN], [EMAIL], [PHONE]. Set action="block" to block the entire response instead.OutputGuardrail
Blocks output that exceeds
n tokens.Tool Guardrails
ToolGuardrail
Only allows tools whose names appear in the
allowed set. All others are blocked.ToolGuardrail
Blocks tools whose names appear in the
blocked set. All others are allowed.Custom Guardrails
Using Decorators
The fastest way to create a custom guardrail:@output_guardrail and @tool_guardrail.
Class-Based
Implement the protocol directly for more control:Modify Action
Guardrails can rewrite content instead of blocking:Composable Guardrails
Combine guardrails with logic operators:Block Handling
Raise Exceptions (Default)
Return Blocked Status
Tracing Events
Guardrail activity is captured in the agent’s trace stream:What’s Next
Agents Overview
Learn how agents orchestrate models, tools, and guardrails.
Middleware
Add request/response transforms alongside guardrails.
Testing
Use MockModel to test guardrail behavior without API calls.
Error Handling
Handle InputCheckError, OutputCheckError, and other exceptions.