How Guardrails Work
Guardrails are executed as hooks at two stages:- Input Guardrails (
before_request_hooks) - Executed before sending to the provider - Output Guardrails (
after_request_hooks) - Executed after receiving provider response
Guardrail Types
The gateway supports two types of hooks:Guardrail Hooks
Validate content without modifying it. Can deny requests/responses.Mutator Hooks
Transform content (redact PII, modify prompts, etc.). Cannot deny requests.Mutator hooks with
async: true are skipped - mutators must run synchronously to transform the request.src/middlewares/hooks/index.ts:449-464
Built-in Guardrail Checks
The gateway includes default guardrail checks:Content Filtering
contains - Check for specific words or phrasesnone- Deny if ANY word is foundany- Approve if ANY word is foundall- Approve only if ALL words are found
Guardrail Configuration
Shorthand Format
Full Hook Format
Conversion to Hooks
The gateway automatically converts shorthand guardrails to full hooks:src/handlers/handlerUtils.ts:233-275
Hook Execution
Execution Phases
Hooks are executed in four phases:- Sync Before Request - Synchronous input validation
- Async Before Request - Asynchronous input validation
- Sync After Request - Synchronous output validation
- Async After Request - Asynchronous output validation
src/middlewares/hooks/globals.ts
Sequential vs Parallel
By default, checks within a hook run in parallel. Setsequential: true to run them in sequence:
src/middlewares/hooks/index.ts:366-418
Deny Logic
A request/response is denied if:- Any hook has
deny: true - That hook’s verdict is
false - The check was not skipped
src/middlewares/hooks/index.ts:246-275
Hook Context
Hooks receive a context object with request and response data:Text Extraction
The gateway extracts text for validation: Request text:src/middlewares/hooks/index.ts:93-161
Skip Conditions
Hooks are skipped when:- Request type is not
chatComplete,complete,embed, ormessages - Request type is
embedand hook is after request (no output to validate) - Request type is
embedand hook is a mutator - After request hook and response status is not 200
- Before request hook and this is a retry (has parent hook span)
- Mutator hook with
async: true
src/middlewares/hooks/index.ts:449-464
Hook Results
Each hook execution returns a result:Plugin System
Guardrails can be extended through the plugin system:plugins/ directory and registered in conf.json:
src/middlewares/hooks/index.ts:282-326
Example Use Cases
PII Protection
Content Moderation
Retry on Blocked Output
Combined Input/Output Validation
Feedback System
Guardrails can provide feedback on success or failure:- Successful check IDs
- Failed check IDs
- Errored check IDs
src/middlewares/hooks/index.ts:466-503
Error Handling
Guardrail errors are handled gracefully:fail_on_error: false:
src/middlewares/hooks/index.ts:282-326
Best Practices
Use Input Guardrails First
Validate and block malicious input before it reaches the provider to save costs.
Keep Checks Fast
Guardrails add latency - keep individual checks under 100ms.
Use Async Sparingly
Async hooks don’t block the response - use for logging/monitoring only.
Test Thoroughly
Test guardrails with real traffic patterns to avoid false positives.
Next Steps
Configs
Learn how to configure guardrails in your gateway config.
Retries
Combine guardrails with retries for robust validation.
Providers
Understand how guardrails work with different providers.
Routing
Use guardrails with routing strategies.