Skip to main content
Guardrails validate and filter agent outputs to ensure safety, quality, and compliance.

Quick Start

1

Create a Guardrail

2

Async Guardrail


User Interaction Flow


GuardrailResult

Result of guardrail validation.

Factory Methods

Instance Methods


Guardrail Trait

Synchronous validation interface.

AsyncGuardrail Trait

Asynchronous validation interface.

GuardrailAction

Action to take when validation fails.

GuardrailConfig

Configuration for guardrail behavior.

Builder Methods


Common Guardrail Patterns

Length Limiter

Profanity Filter


Best Practices

Use different guardrails for different concerns: length, content, format.
When calling moderation APIs, use AsyncGuardrail to avoid blocking.
Clear error messages help users understand why content was blocked.
Use Retry for transient issues, Stop for hard violations, Fallback for graceful degradation.

Hooks

Lifecycle hooks

Agent

Agent API