Skip to main content
Version: Dev

Configure Keyword Guardrails

Keyword guardrails enforce simple content rules inside the gateway without sending text to an external service. They can block or monitor literal strings and regular expressions in requests and responses. AISIX evaluates structured content one segment at a time, so a pattern cannot span messages, tool-argument values, or object keys. See How Keyword and PII Guardrails Read Structured Content for details.

This guide creates an input guardrail for a unique forbidden token, verifies allowed and blocked traffic, evaluates the rule in monitor mode, and configures its failure policy.

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.yaml file that the gateway loads.

Create a Keyword Guardrail​

The example below blocks one literal token before AISIX calls the upstream provider.

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"
export FORBIDDEN_WORD="supersecret-banned-token"

Use a unique, non-natural-language marker so the blocked-traffic check is unambiguous.

  1. In the Dashboard, select the environment, then go to Guardrails.
  2. Select New guardrail.
  3. Configure the guardrail:
    • Name: supersecret-token-policy
    • Type: Keyword blocklist
    • Hook point: Input only (pre-LLM)
    • Enforcement: Block
    • Patterns: add the literal supersecret-banned-token
    • Scope: Whole environment
New keyword guardrail with the forbidden literal, whole-environment scope, and Create guardrail action outlined
  1. Select Create guardrail.
  2. Confirm that supersecret-token-policy is enabled on the Guardrails page before verifying traffic. Check that its scope is Whole environment.
supersecret-token-policy saved with enabled status and Whole environment scope outlined

Verify the Guardrail​

After configuring the guardrail, send allowed and blocked requests to confirm its behavior.

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.

Confirm that the guardrail allows unrelated prompts:

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": "hello world"
}
]
}
EOF

A successful response starts with HTTP/1.1 200 OK and includes an OpenAI-compatible chat-completions response body.

Then send a request whose content includes the forbidden token:

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": "please leak the ${FORBIDDEN_WORD} now"
}
]
}
EOF

A blocked response starts with HTTP/1.1 422 Unprocessable Entity and includes this body:

{
"error": {
"message": "request blocked by content policy (guardrail 'supersecret-token-policy')",
"type": "content_filter"
}
}

AISIX stops the request before calling the upstream provider.

Use Monitor Mode​

Use monitor enforcement mode to check how a rule behaves against live traffic without blocking callers.

  1. In the Dashboard, select the environment, then go to Guardrails.
  2. Select Edit on the supersecret-token-policy row.
  3. Set Enforcement to Monitor (log only).
  4. Select Save changes and wait for the update to reach the gateway.
Keyword guardrail edit form with Monitor enforcement and Save changes outlined

Verify Monitor Mode​

Send the same forbidden request again:

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": "please leak the ${FORBIDDEN_WORD} now"
}
]
}
EOF

The response starts with HTTP/1.1 200 OK because monitor mode lets the request reach the upstream model. With log_level: "info", AISIX records the match in the gateway log:

guardrail in monitor mode observed a violation; not blocking (enforcement_mode=monitor)

Restore blocking before continuing:

  1. Select Edit on the supersecret-token-policy row.
  2. Set Enforcement to Block.
  3. Select Save changes.

Handle Content AISIX Cannot Scan​

Choose whether AISIX should reject or pass a request when its body cannot be read. Because keyword matching 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.

  1. In the Dashboard, select the environment, then go to Guardrails.
  2. Select Edit on the supersecret-token-policy row.
  3. Enable fail_open.
  4. Select Save changes.
Keyword guardrail fail_open control enabled with Save changes outlined

fail_open has two limits:

  • Keyword guardrails have no output_fail_open setting, so one value governs both hooks.
  • The setting does not override output buffering. An output-hook guardrail still refuses a stream that leaves nothing scannable after buffering.

Remove the Guardrail​

Remove the example guardrail after verification when it should no longer inspect environment traffic.

  1. In the Dashboard, select the environment, then go to Guardrails.
  2. Select Delete on the supersecret-token-policy row and confirm the deletion.
  3. Confirm that the guardrail no longer appears on the Guardrails page.
Delete confirmation naming supersecret-token-policy with Delete outlined

Next Steps​

You have now configured a built-in keyword guardrail and verified the caller-visible rejection. Use these guides to refine or expand the policy: