deny is true, the gateway blocks the request and returns an error to the caller.
How guardrails work
Every request through the AI Gateway passes through two optional hook points:beforeRequestHook
Runs before the request is forwarded to the LLM provider. Use this to:- Block disallowed request types or models
- Detect prompt injection or jailbreak attempts
- Validate JWT tokens or required metadata
- Redact PII before it leaves your network
- Mutate the prompt (transformer functions)
afterRequestHook
Runs after the LLM returns a response, before it is forwarded to your client. Use this to:- Validate response structure (JSON schema, required keys)
- Block responses that contain forbidden words or code
- Verify all URLs in the response are reachable
- Detect hallucinations or bias in model output
- Log responses to an external system
The deny flag
Each guardrail object carries a deny boolean that controls what happens when the check verdict is false:
deny | Verdict | Outcome |
|---|---|---|
true | false | Request is blocked. Gateway returns HTTP 446 with details. |
false | false | Request continues. Response status is 246 to signal a soft failure. |
| any | true | Request continues normally. |
A
246 status code means the guardrail check failed but the request was not blocked — useful for logging and monitoring without disrupting traffic.Attaching guardrails to a config
Guardrails are defined inside a Portkey config object using theinput_guardrails and output_guardrails keys. Pass the config when constructing the client or on a per-request basis.
Multiple checks in one guardrail
Each guardrail object can hold one check function. Stack multiple objects in the array to run several checks:deny: true check fails, the request is blocked.
Function ID format
Guardrail function IDs follow the pattern<plugin-id>.<functionId>:
default.regexMatch— built-in regex checkdefault.contains— built-in word checkaporia.validateProject— Aporia partner guardrailpangea.textGuard— Pangea AI Guard
default plugin, or Partner Plugins for third-party integrations.
Next steps
Built-in Guardrails
All 21 functions included in the
default plugin — regex, word lists, JSON schema, JWT, and more.Custom Plugins
Write your own guardrail logic in TypeScript and register it with the gateway.
Partner Plugins
Integrate Aporia, Pangea, Patronus, Pillar, and 15+ other security providers.