Configure PII Guardrails
The built-in PII guardrail detects and handles sensitive data without sending content to an external moderation service. It can mask matches before content continues to the upstream model or caller, or block the request or response. Guardrail logs, usage fields, and refusals record detector names and counts rather than matched values; What the Gateway Records describes the surrounding request data that other features can retain.
This guide creates an input guardrail for email addresses and API keys, verifies masking and blocking, adds an organization-specific pattern, and configures replacement and failure behavior.
Prerequisites
Before starting, prepare the following:
- Review Guardrail Policy Behavior for hook points and enforcement modes.
- A working model alias, caller API key, and running gateway that can send Chat Completions requests.
curl.- If you use the Dashboard to configure AISIX Enterprise or AISIX Cloud, sign in with permission to manage guardrails and attachments in the target environment.
- If you use the Admin API to configure AISIX Enterprise or AISIX Cloud, prepare a write-scoped token, the environment ID, and
jq. - If you use AISIX OSS, prepare a complete format-v2
resources.yamlfile that the gateway loads.
Create a PII Guardrail
Before creating a policy, decide which sensitive-data formats to detect and whether a match should continue in masked form or stop traffic:
maskreplaces the match with a redaction token and lets traffic continue.blockrejects the request or response with422 Unprocessable Entity.
The example below masks email addresses and API keys in caller requests before AISIX sends them to the upstream provider. See PII Detector Reference for the complete detector catalog, bank-card formats, masking limitations, and replacement rules.
Export the gateway values used by the verification requests:
# AISIX_PROXY has no trailing slash or endpoint path.
# The local quickstarts use http://127.0.0.1:3000.
export AISIX_PROXY="YOUR_AISIX_GATEWAY_URL"
export AISIX_API_KEY="YOUR_CALLER_API_KEY"
export AISIX_MODEL="gpt-4o-mini"
- AISIX Enterprise or Cloud
- AISIX OSS
- Dashboard
- Admin API
- In the Dashboard, select the environment, then go to Guardrails.
- Select New guardrail.
- Configure the guardrail:
- Name:
pii-redaction-policy - Type: PII redaction
- Hook point: Input only (pre-LLM)
- Enforcement: Block
- Default action: Mask
- Built-in detectors: select only Email address and API keys & tokens
- Scope: Whole environment
- On stream buffer overflow: Fail closed (drop the response — recommended)
- Max buffer bytes: leave blank to use
262144
- Name:

- Select Create guardrail.
- Confirm that
pii-redaction-policyis enabled on the Guardrails page before verifying traffic. Check that its scope is Whole environment.

Export the control-plane connection details:
# AISIX_CP includes /api and has no trailing slash.
# The local AISIX Enterprise quickstart uses http://localhost:8080/api.
export AISIX_CP="YOUR_AISIX_CONTROL_PLANE_API_URL"
export AISIX_TOKEN="YOUR_ADMIN_TOKEN"
export ENV_ID="YOUR_ENVIRONMENT_ID"
Create an input guardrail that masks the email and api_key detectors, and capture its ID:
export GUARDRAIL_ID="$(curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/guardrails" \
-H "Authorization: Bearer $AISIX_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "pii-redaction-policy",
"enabled": false,
"hook_point": "input",
"kind": "pii",
"config": {
"default_action": "mask",
"detectors": [
{ "type": "email" },
{ "type": "api_key" }
]
}
}' | jq -r '.guardrail.id')"
❶ Creating the guardrail disabled keeps it staged while you add and verify its scope. A guardrail with no attachments inspects no traffic. Enable it after attaching it below.
❷ input masks the caller request before AISIX sends it upstream. Use output to redact provider responses, or both to cover both sides. See Guardrail Hook Point.
❸ default_action sets the action for listed detectors. mask replaces each match with a redaction token. Set a detector-level action only when one detector needs to behave differently.
Attach the guardrail to the whole environment. The env scope binds every request in the environment and omits scope_id. To narrow the attachment, use model, mcp_server, api_key, team, or passthrough_route with a matching scope_id:
curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/guardrails/$GUARDRAIL_ID/attachments" \
-H "Authorization: Bearer $AISIX_TOKEN" \
-H "Content-Type: application/json" \
-d '{ "scope_type": "env" }'
Enable the guardrail after its attachment exists:
curl -sS -X PATCH "$AISIX_CP/environments/$ENV_ID/guardrails/$GUARDRAIL_ID" \
-H "Authorization: Bearer $AISIX_TOKEN" \
-H "Content-Type: application/json" \
-d '{"enabled": true}'
The enabled configuration projects to attached gateways automatically. Keep GUARDRAIL_ID set for the update and cleanup steps later in this guide.
Add the guardrail to the resources file that already defines the example model and caller API key:
guardrails:
- id: 1cd238bc-081f-4f9e-a21c-79098843ceaa
name: pii-redaction-policy
enabled: true
hook_point: input
kind: pii
default_action: mask
detectors:
- type: email
- type: api_key
guardrail_attachments:
- id: 8350752d-88e9-4c9b-b97b-3205de84238d
guardrail_id: 1cd238bc-081f-4f9e-a21c-79098843ceaa
scope_type: env
priority: 100
❶ mask replaces every detected span with a redaction token and lets the request continue.
❷ Each detector runs inside the gateway. An individual detector can set action: block when it should override the default.
A guardrail inspects traffic only where an attachment places it. Add a guardrail_attachments entry that names the guardrail, then validate the complete file and reload the gateway.
See Reload a Resources File for the runnable Docker workflow.
For response-side PII redaction on streamed traffic, AISIX holds response content until it can apply the guardrail. See Configure Buffer Limits for the defaults and overflow behavior.
Verify Masking
Verify that AISIX rewrites sensitive data before it reaches the upstream model. Send a request whose prompt contains an email address.
For AISIX Enterprise and AISIX Cloud, projection is asynchronous. If a managed gateway does not reflect the new rule, wait for the latest revision and retry. See Resource Projection for convergence checks.
curl -sSi -X POST "$AISIX_PROXY/v1/chat/completions" \
-H "Authorization: Bearer ${AISIX_API_KEY}" \
-H "Content-Type: application/json" \
--data-binary @- <<EOF
{
"model": "${AISIX_MODEL}",
"messages": [
{
"role": "user",
"content": "email me at alice@example.com about the order"
}
]
}
EOF
The request succeeds with HTTP/1.1 200 OK. AISIX rewrites the prompt before calling the upstream model, so the provider receives the masked prompt:
email me at [EMAIL_REDACTED] about the order
The original value does not reach the upstream provider. Usage records carry per-detector match counts, including detector names but not matched text. A validation error raised before the guardrail runs can still quote the value; see What the Gateway Records.
Block Sensitive Data
Masking lets traffic continue, but some sensitive data should stop the request entirely. In this section, add a second guardrail that blocks a valid mainland China resident ID.
- AISIX Enterprise or Cloud
- AISIX OSS
- Dashboard
- Admin API
- On the Guardrails page, select New guardrail.
- Enter
block-id-cardfor Name and select PII redaction for Type. - Select Input only (pre-LLM) for Hook point, Block for Enforcement, and Block for Default action.
- Under Built-in detectors, select only Mainland China resident ID.
- Select Whole environment for Scope.

- Select Create guardrail.
- Confirm that
block-id-cardis enabled and shows Whole environment before verifying traffic.

Create the blocking guardrail and attach it to the environment:
export BLOCK_GUARDRAIL_ID="$(curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/guardrails" \
-H "Authorization: Bearer $AISIX_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "block-id-card",
"enabled": false,
"hook_point": "input",
"kind": "pii",
"config": {
"default_action": "block",
"detectors": [
{ "type": "china_id_card" }
]
}
}' | jq -r '.guardrail.id')"
curl -sS -X POST "$AISIX_CP/environments/$ENV_ID/guardrails/$BLOCK_GUARDRAIL_ID/attachments" \
-H "Authorization: Bearer $AISIX_TOKEN" \
-H "Content-Type: application/json" \
-d '{ "scope_type": "env" }'
curl -sS -X PATCH "$AISIX_CP/environments/$ENV_ID/guardrails/$BLOCK_GUARDRAIL_ID" \
-H "Authorization: Bearer $AISIX_TOKEN" \
-H "Content-Type: application/json" \
-d '{"enabled": true}'
Keep BLOCK_GUARDRAIL_ID set for cleanup.
Add the blocking guardrail alongside the masking guardrail in resources.yaml:
guardrails:
- id: 1cd238bc-081f-4f9e-a21c-79098843ceaa
name: pii-redaction-policy
enabled: true
hook_point: input
kind: pii
default_action: mask
detectors:
- type: email
- type: api_key
- id: 4470020e-b127-45d6-a23d-c1c135aeea9a
name: block-id-card
enabled: true
hook_point: input
kind: pii
default_action: block
detectors:
- type: china_id_card
guardrail_attachments:
- id: 8350752d-88e9-4c9b-b97b-3205de84238d
guardrail_id: 1cd238bc-081f-4f9e-a21c-79098843ceaa
scope_type: env
priority: 100
- id: 5f6d69e4-f268-4575-b50b-f9adcabbde1b
guardrail_id: 4470020e-b127-45d6-a23d-c1c135aeea9a
scope_type: env
priority: 100
Validate the complete file and reload the gateway.
Verify Blocking
A request whose content includes a valid ID number is rejected:
curl -sSi -X POST "$AISIX_PROXY/v1/chat/completions" \
-H "Authorization: Bearer ${AISIX_API_KEY}" \
-H "Content-Type: application/json" \
--data-binary @- <<EOF
{
"model": "${AISIX_MODEL}",
"messages": [
{
"role": "user",
"content": "my id is 11010519491231002X"
}
]
}
EOF
A blocked response starts with HTTP/1.1 422 Unprocessable Entity and includes this body:
{
"error": {
"message": "request blocked by content policy (guardrail 'block-id-card')",
"type": "content_filter"
}
}
The message is deliberately generic and does not echo the matched value.
Add a Custom Pattern
Built-in detectors cover common data formats, but organizations often have identifiers unique to their own systems. In this section, extend pii-redaction-policy to mask employee IDs such as EMP-123456.
The following examples add an employee ID pattern to pii-redaction-policy while preserving its existing email and API-key detectors.
- AISIX Enterprise or Cloud
- AISIX OSS
- Dashboard
- Admin API
- On the Guardrails page, select Edit on the
pii-redaction-policyrow. - Under Custom patterns, select Add pattern.
- Enter
employee_idfor the pattern name andEMP-[0-9]{6}for the regular expression. - Keep the pattern action set to Default action and leave the replacement blank, then select Save changes.
AISIX masks a matching value such as EMP-123456 as [EMPLOYEE_ID_REDACTED] before forwarding the request.

Replace the existing guardrail's configuration with the expanded detector set:
curl -sS -X PATCH "$AISIX_CP/environments/$ENV_ID/guardrails/$GUARDRAIL_ID" \
-H "Authorization: Bearer $AISIX_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"config": {
"default_action": "mask",
"detectors": [
{ "type": "email" },
{ "type": "api_key" }
],
"custom_patterns": [
{
"name": "employee_id",
"regex": "EMP-[0-9]{6}"
}
]
}
}'
The existing attachment remains in place when the guardrail configuration changes. AISIX masks a matching value such as EMP-123456 as [EMPLOYEE_ID_REDACTED] before forwarding the request.
Add the same pattern to the existing guardrail entry:
guardrails:
- id: 1cd238bc-081f-4f9e-a21c-79098843ceaa
name: pii-redaction-policy
enabled: true
hook_point: input
kind: pii
default_action: mask
detectors:
- type: email
- type: api_key
custom_patterns:
- name: employee_id
regex: 'EMP-[0-9]{6}'
Validate the complete file and reload the gateway. AISIX masks a matching value such as EMP-123456 as [EMPLOYEE_ID_REDACTED] before forwarding the request.
Choose What a Masked Span Becomes
The default redaction token identifies the detector or custom pattern that matched. When an upstream system needs a different placeholder, configure one explicitly. In this section, change the employee ID replacement to six asterisks.
- AISIX Enterprise or Cloud
- AISIX OSS
- Dashboard
- Admin API
- On Guardrails, select Edit on the
pii-redaction-policyrow. - Under Custom patterns, enter
******in the replacement field foremployee_id. - Select Save changes. Clearing the replacement field restores the default token.

To delete a matched span rather than replace it with a token, use the Admin API or resources file. Clearing the Dashboard field cannot create an explicit empty-string replacement.
Update the example guardrail's configuration to add a literal replacement. The payload retains the email and API-key detectors and the employee ID pattern created earlier. Preserve any additional detectors or patterns when adapting it:
curl -sS -X PATCH "$AISIX_CP/environments/$ENV_ID/guardrails/$GUARDRAIL_ID" \
-H "Authorization: Bearer $AISIX_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"config": {
"default_action": "mask",
"detectors": [
{ "type": "email" },
{ "type": "api_key" }
],
"custom_patterns": [
{
"name": "employee_id",
"regex": "EMP-[0-9]{6}",
"replacement": "******"
}
]
}
}'
To delete the matched span instead, set replacement to an explicit empty string in the same payload.
Add a literal replacement to the existing employee ID pattern. Keep the rest of the guardrail, the other guardrail entries, and their attachments in the complete file:
custom_patterns:
- name: employee_id
regex: 'EMP-[0-9]{6}'
replacement: '******'
To delete the matched span instead, set replacement to ''. Validate the complete resources file and reload the gateway.
Replacement text is literal: $1 and other group references are not expanded. When a regex contains capture groups, AISIX replaces only group 1 and keeps the surrounding match. Without a capture group, it replaces the whole match.
See Custom Pattern Replacements for examples, structured tool-argument behavior, and common mistakes. replacement applies only to mask; a blocking pattern rejects traffic instead of rewriting it.
Handle Content AISIX Cannot Scan
Choose whether AISIX should reject or pass a request when its body cannot be read. Because PII detection runs inside the gateway, this situation comes from unreadable content, such as invalid UTF-8, rather than an unavailable moderation service.
By default, AISIX rejects the request with 422 and records the failure tag unscannable_body. Enable fail_open when availability matters more than enforcement. AISIX then forwards the request and records the bypass in its usage record.
- AISIX Enterprise or Cloud
- AISIX OSS
- Dashboard
- Admin API
- On the Guardrails page, select Edit on the
pii-redaction-policyrow. - Enable
fail_open. - Select Save changes.

Set fail_open on the guardrail and keep its other configuration unchanged:
curl -sS -X PATCH "$AISIX_CP/environments/$ENV_ID/guardrails/$GUARDRAIL_ID" \
-H "Authorization: Bearer $AISIX_TOKEN" \
-H "Content-Type: application/json" \
-d '{"fail_open": true}'
Add only fail_open: true to the existing guardrail entry. Keep its hook point, enforcement mode, detectors and custom patterns, and attachments unchanged:
guardrails:
- id: 1cd238bc-081f-4f9e-a21c-79098843ceaa
name: pii-redaction-policy
kind: pii
fail_open: true
Validate the complete file and reload the gateway.
fail_open has three limits:
- PII guardrails have no
output_fail_opensetting, so one value governs both hooks. on_buffer_exceededseparately controls what happens when a buffered response exceedsmax_buffer_bytes.fail_opendoes not override output buffering. An output-hook guardrail still refuses a stream that leaves nothing scannable after buffering.
Remove the Guardrails
Remove both example guardrails after verification when they should no longer inspect environment traffic.
- AISIX Enterprise or Cloud
- AISIX OSS
- Dashboard
- Admin API
- On the Guardrails page, select Delete on the
block-id-cardrow and confirm the deletion.

- Select Delete on the
pii-redaction-policyrow and confirm the deletion.

- Confirm that neither guardrail appears on the Guardrails page.
Delete both guardrails. Their attachments are removed with them:
curl -sS -X DELETE "$AISIX_CP/environments/$ENV_ID/guardrails/$BLOCK_GUARDRAIL_ID" \
-H "Authorization: Bearer $AISIX_TOKEN"
curl -sS -X DELETE "$AISIX_CP/environments/$ENV_ID/guardrails/$GUARDRAIL_ID" \
-H "Authorization: Bearer $AISIX_TOKEN"
Confirm that GET requests for both IDs return 404 Not Found.
Remove pii-redaction-policy, block-id-card, and both matching guardrail_attachments entries from the complete resources file. Validate the file and reload the gateway.
Confirm that the test email and ID number are no longer masked or blocked by these guardrails.
Next Steps
You have configured the built-in PII guardrail and verified mask and block behavior. Use these guides to refine or expand the policy:
- Guardrail Policy Behavior: adjust hook points, scope, and enforcement.
- PII Detector Reference: review detector formats, masking limitations, and custom-pattern replacement rules.
- Presidio Guardrails: use NER entities and anonymization operators from a Presidio service you run.
- Choosing a Guardrail Provider: compare built-in PII detection with remote guardrail services.