Guardrails let you enforce content policies on every agent run — blocking dangerous input, redacting PII from output, and restricting which tools the model can call.Documentation Index
Fetch the complete documentation index at: https://docs.definable.ai/llms.txt
Use this file to discover all available pages before exploring further.
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
Guardrails that check the user message before the LLM call.
Guardrails that check the model response after the LLM call.
Guardrails that check each tool call before execution.
"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.Blocks output that exceeds
n tokens.Tool Guardrails
Only allows tools whose names appear in the
allowed set. All others are blocked.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:| Event | Fields | Emitted When |
|---|---|---|
GuardrailCheckedEvent | guardrail_name, guardrail_type, action, message, duration_ms | After each check completes |
GuardrailBlockedEvent | guardrail_name, guardrail_type, reason | When a guardrail blocks |
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.