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
"fail_fast" stops at the first block. "run_all" runs every guardrail and collects all results."raise" throws InputCheckError or OutputCheckError. "return_message" returns a RunOutput with status=RunStatus.blocked.Built-in Guardrails
Input Guardrails
Blocks input that exceeds
n tokens. Uses the specified model’s tokenizer for counting.Blocks input containing any keyword from the
topics list (case-insensitive substring match).Blocks or redacts input matching any of the given regex patterns. Set
action="modify" to redact matches instead of blocking.Output Guardrails
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.Tool Guardrails
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.