AI Gateway guardrails
- Applies to
- Gatewayruntime for API, AI, and MCP gateways
- Hosting
- Managed EdgeManaged DedicatedSelf-Hosted
Guardrails are policies in an app's policy chain that inspect prompts and model responses before they reach the model or the caller. Depending on the policy, a guardrail masks what it finds, blocks the request or response, or records a finding and lets the traffic pass. Each app runs its own chain, so a customer-facing chatbot can run strict guardrails while an internal agent runs a lighter set.
This page helps you choose which guardrail to use, shows how they fit together, and gives a starting chain. Each policy's reference page has the full option list.
Choose a guardrail
| You want to | Use | What it inspects | What it does on a match |
|---|---|---|---|
| Keep personal data, financial data, and credentials out of prompts and completions | Data Loss Prevention (DLP) | Prompts and responses, including streaming responses, tool-call arguments, and tool results | Masks, blocks, or logs, per rule and per direction |
| Stop prompts that try to override instructions, redefine the model's role, or arrive through tool output | Prompt Injection Protection | The newest message of each request, including tool results | Blocks the request with a 400 |
| Enforce the detection rules your security team manages in Akamai | Akamai AI Firewall | Prompts and responses, including streaming responses | Blocks content that your Akamai configuration denies |
| Apply rules specific to your organization, such as banned terms or an in-house classifier | A custom policy | Whatever your TypeScript code reads from the request | Whatever your code returns, such as a 400 response |
The guardrails work independently, so you can combine them. For example, run DLP on every app, and add Prompt Injection Protection to apps that accept untrusted input or call tools.
Data Loss Prevention
The DLP policy scans the content of AI requests and responses for sensitive data: system prompts, message text, thinking blocks, tool results, and tool-call arguments. It doesn't inspect structural fields such as model names, temperatures, or token counts. Detection runs inside your gateway with 62 built-in data types, so no content goes to a third-party scanning service.
You write rules for two directions:
inboundcovers what callers send the model.outboundcovers what the model sends back.
Each rule names a data type (such as id-us-ssn), a group (such as pii,
secret, or finance), or a custom detector, and gives it an action:
| Action | Result |
|---|---|
mask | Replaces the match with a placeholder, such as [CONTACT_EMAIL], and the conversation continues |
block | Rejects the request, or ends the response, with an error that names the detected types but never the values |
log | Records a finding and lets the traffic pass. Use it to trial a rule before you enforce it |
off | Excludes one type from a group that a broader rule turns on |
This example masks personal data and secrets in prompts, and blocks credit card numbers in completions:
Code
To redact API keys, tokens, and private keys that appear in AI traffic, use the
secret group or one of its types, such as secret-openai or secret-zuplo.
Streaming responses
The streaming.mode option sets how DLP inspects streaming responses:
| Mode | How it works | Choose it when |
|---|---|---|
stream (default) | Scans the response while it streams, holding back about 64 characters so a value split across chunks is still masked | Callers need tokens as soon as possible |
buffer | Collects the whole response, then scans it once | You need exact detection for values of any length, and a block as a clean 400 error |
off | Passes streaming responses through unscanned. Non-streaming responses are still scanned | You scan only non-streaming traffic |
When a block rule fires in stream mode, the stream ends with the endpoint's
native content-filter events, so OpenAI and Anthropic clients see a normal
content-filter stop.
Configure DLP in the Portal
On the app's Policies tab, click Add policy and add Data Loss Prevention (DLP). The policy's form has separate rule lists for inbound and outbound traffic, an Allow list for values that are never findings, such as a public test card number, Custom detectors for your own regular expressions, and Advanced settings, including Streaming responses.
Prompt Injection Protection
The
Prompt Injection Protection policy
sends the newest message of each request to a small classifier model, and blocks
the request with a 400 when the classifier flags an injection attempt, such as
an instruction override, role redefinition, or system-prompt manipulation. The
request never reaches the model.
The policy screens tool results. Tool output and retrieved documents are the main path for indirect prompt injection, and in an agent loop the newest message is usually a tool result, so the default screens exactly the content that's new on each turn:
| Endpoint | Tool results it inspects |
|---|---|
/v1/chat/completions | tool messages |
/v1/messages | tool_result blocks |
/v1/responses | function_call_output items |
The policy never inspects your application's own instructions: system and
developer messages, Anthropic's top-level system field, and the Responses
instructions field. A hardened system prompt reads like the attacks the
classifier looks for, so inspecting it would block your own configuration. To
inspect more than the newest message, such as when clients send history
assembled outside the gateway, set recentMessageCount.
Choose where the classifier runs
| Option | Set | What happens |
|---|---|---|
| An OpenAI-compatible API | apiKey, and optionally model and baseUrl | The gateway calls the API directly. The default is gpt-4o-mini on https://api.openai.com/v1. Set baseUrl for any compatible host, including a self-hosted model that supports forced tool calling. |
| Another app on the AI Gateway | appId, and model as provider/model | The gateway runs the classifier through that app in-process, with no outbound HTTP call. The classification uses that app's provider credentials, model routing, fallbacks, and budgets, so provider keys stay on one app. |
To run the classifier on another app, create an app for the classifier and give it the Internal Only policy. Internal Only accepts requests the gateway makes itself and rejects requests from the network, so you don't need an API key for the classifier app. Don't add Prompt Injection Protection to the classifier app's own chain.
Every configuration needs either apiKey or appId. Without one, the policy
rejects every request instead of letting prompts through unchecked.
Code
Customize what the classifier flags
The classifierPrompt option replaces the built-in system prompt that defines a
prompt injection, as a string or an array of lines. Say what to flag, and say
what's normal in your application so ordinary requests aren't mistaken for
attacks:
Code
The custom prompt changes what gets flagged, not how the verdict is read: the
inspected text still arrives as a separate message, and the classifier still
answers SAFE or UNSAFE. The policy page shows the built-in prompt to start
from.
Run the classifier in parallel
Set execution to parallel to run the classifier while the rest of the chain
runs. The gateway still enforces the verdict before it sends the request to the
provider, so no prompt goes out unchecked, and a safe prompt doesn't add the
classifier's latency to the request. The default, sequential, classifies the
prompt before the next policy in the chain runs.
Akamai AI Firewall
The Akamai AI Firewall policy sends prompts and completions to Akamai Firewall for AI and blocks content that your Akamai configuration denies. You manage detection rules and their categories in Akamai Control Center. One policy entry covers both directions, including streaming responses, which the gateway checks in batches before it releases them to the caller.
Set configurationId and api-key to connect the policy to your Akamai
configuration. Don't add it to an app that serves as a
Smart Router classifier.
Custom guardrails
When the built-in guardrails don't cover a rule, write a policy in TypeScript and add it to app chains like any other policy. The custom policy quickstart builds a content filter that blocks prompts containing banned terms.
Recommended starting chain
Policies run in the order the chain lists them, and a guardrail that blocks a request answers it, so later entries don't run. This order is a good starting point for an app that needs guardrails:
Code
- DLP first among the guardrails. DLP masks the request before later policies see it, so the prompt injection classifier, Akamai, and the model receive the masked text.
- Guardrails before Semantic Cache. A cache hit returns immediately and later policies don't run, so a guardrail after the cache doesn't inspect requests that the cache answers.
- Prompt Injection Protection and cache hits. In
parallelmode, the gateway serves a cache hit without waiting for the classifier. Keep the defaultsequentialmode when every request, including cache hits, needs a verdict.
For the routing policies in this chain, and alternatives such as Model Load Balancing and Smart Router, see Routing and reliability.
Add guardrails to apps
Add a guardrail on the app's Policies tab: open the Apps tab of your AI Gateway project, select the app, and click Add policy. DLP has its own form. Prompt Injection Protection and Akamai AI Firewall open a settings form built from the policy's options, with a JSON view. Chain changes apply without a deploy.
To start every new app in a pool with the same guardrails, add them to the pool's policy template.
The Add policy dialog lists the policies declared in your gateway's
config/policies.json. If you connected a Git repository and a guardrail is
missing from the list, see
Keep the gateway up to date.
Related resources
- Policy Chains: how a chain runs and how entries inherit options
- Routing and reliability: choose models and fall back when a model fails or a budget runs out
- AI Gateway policies overview: every policy an app can run