Skip to main content

Interceptors & Guardrails

Interceptors and Guardrails

Interceptors are custom logic that can run before or after a request is routed, allowing you to enrich, validate, or transform requests and responses. Guardrails are a common type of interceptor used to enforce policies.

TypePurposeBusiness ValueExample Configuration
pre_requestAnalyze or enrich requestClassify topic, check for risk, personalizerate_limiter, semantic_guardrail, toxicity_guardrail
post_requestAnalyze or modify responseModerate output, add fallback, redact sensitive infofallback_response, content_moderator

Pre-Request Interceptor Configuration

Pre-request interceptors are defined in the routing.pre_request array and run before routing evaluation:

{
"routing": {
"pre_request": [
{
"name": "rate_limiter",
"type": "interceptor",
"limit": 1000,
"period": "day",
"target": "user_id"
},
{
"name": "semantic_guardrail",
"type": "guardrail"
}
]
}
}

Interceptor Result Variables

When an interceptor runs, it injects its results into the routing context using the pre_request.{interceptor_name} namespace:

Result Variable PathDescriptionExample ValueBusiness Use
pre_request.{interceptor_name}.passedWhether the interceptor check passedtrueConditional routing based on checks
pre_request.rate_limiter.passedRate limit check resultfalseEnforce usage quotas and prevent abuse
pre_request.semantic_guardrail.passedContent policy check resulttrueBlock policy violations
pre_request.toxicity_guardrail.passedToxicity check resultfalseBlock harmful content
pre_request.{guardrail}.result.topicDetected topic from semantic analysis"billing"Route to topic-specialized models
pre_request.{guardrail}.result.scoreConfidence or toxicity score0.8Threshold-based routing decisions
pre_request.{guardrail}.result.*Any custom result from interceptorVariousCustom business logic

Common Interceptor Types

Interceptor TypePurposeConfiguration FieldsResult Fields
rate_limiterEnforce request quotaslimit, period, targetpassed
semantic_guardrailContent classification and filteringCustom guardrail configurationpassed, result.topic, result.*
toxicity_guardrailDetect harmful contentCustom guardrail configurationpassed, result.score
custom_interceptorBusiness-specific logicVaries by implementationpassed, result.*

Usage in Routing Conditions

Use interceptor results in your routing conditions:

{
"conditions": {
"all": [
{ "pre_request.rate_limiter.passed": { "$eq": true } },
{ "pre_request.semantic_guardrail.passed": { "$eq": true } },
{ "pre_request.semantic_guardrail.result.topic": { "$eq": "support" } }
]
}
}

Error Handling with Message Mappers

Block requests that fail interceptor checks:

{
"name": "rate_limit_block",
"conditions": {
"pre_request.rate_limiter.passed": { "$eq": false }
},
"message_mapper": {
"modifier": "block",
"content": "Rate limit exceeded. Please try again later."
}
}

Note on Custom Guardrails: Guardrails like semantic_guardrail and toxicity_guardrail are powerful examples of custom guardrails. Check out the Guardrails section for implementation details and configuration options.