Kong authenticates each request, then runs the three guardrail AI Policies in their built-in priority order: the AI PII Sanitizer Policy (priority 776), the AI Semantic Prompt Guard Policy (775), and the AI Prompt Guard Policy (771). A policy with a higher priority runs first, and the order of the AI Model’s policies list has no effect. Only requests that pass all three reach the LLM.
sequenceDiagram
participant C as Client
participant K as Kong AI Gateway
participant P as PII Anonymizer Service
participant E as Embeddings and Redis
participant L as LLM Provider
C->>K: POST /multi-layer-ai-guardrails (apikey header, prompt)
activate K
K->>K: Key Auth AI Auth Strategy validates apikey (else 401)
K->>P: AI PII Sanitizer Policy sends the prompt
activate P
P-->>K: Prompt with placeholders
deactivate P
K->>E: AI Semantic Prompt Guard Policy embeds the prompt and searches the deny prompts
activate E
E-->>K: Closest deny prompt and distance
deactivate E
K->>K: AI Semantic Prompt Guard Policy blocks close matches (else 400)
K->>K: AI Prompt Guard Policy checks deny patterns (else 400)
K->>K: AI Model Provider adds provider credentials
K->>L: Sanitized request in the provider's native format
activate L
L-->>K: Native response
deactivate L
K-->>C: OpenAI-format response with X-Kong-LLM-Model
deactivate KThe AI Policy priority table lists the priority of each AI Policy type. Because the semantic guard runs before the regex guard, a prompt with a denied keyword still costs one embeddings call.
Authentication is an AI Auth Strategy, not an AI Policy. The Key Auth AI Auth Strategy reads the apikey header and matches the key against the AI Consumer credentials. On a match, Kong attaches the AI Consumer to the request. The AI Consumer is the identity that rate limits, ACLs, and analytics use. Kong rejects an unknown key before any guardrail runs, so unauthenticated traffic never uses the PII service, the embeddings model, or the LLM.
The following settings configure the Key Auth AI Auth Strategy and the AI Consumer:
|
Setting
|
Purpose
|
key_names: [apikey]
|
Kong reads the key from the apikey header. Key Auth compares the whole header value, so it can’t read a key from an Authorization: Bearer header. The note in Run the demo shows the workaround.
|
hide_credentials: true
|
Kong removes the key before it forwards the request. The provider never sees the AI Consumer key.
|
credentials[].api_key
|
The AI Consumer’s key. The field is write-only, so it takes a !secret reference to CONSUMER_API_KEY, even for a demo value.
|
Kong adds X-Consumer-Username and related headers to the upstream request, not to the response. Per-consumer usage appears in Konnect analytics.
The AI PII Sanitizer Policy sends the prompt to the AI PII Anonymizer service, which replaces each detected value with a placeholder, PLACEHOLDER<N>. Identical values get the same placeholder. The policy runs first, so the two prompt guards and the LLM see only the sanitized prompt.
The following settings configure the AI PII Sanitizer Policy:
|
Setting
|
Purpose
|
anonymize: [all_and_credentials]
|
Detects every supported type of personal data, and credentials. List single types, for example email or creditcard, to narrow the scope.
|
host, port, and scheme
|
The address of the AI PII Anonymizer service: PII_SERVICE_HOST, port 8080, over http.
|
redact_type: placeholder
|
Replaces each value with a placeholder. synthetic replaces it with a realistic fake value instead.
|
recover_redacted: false
|
Keeps the placeholders in the response. With true, Kong puts the original values back before it returns the response to the client.
|
stop_on_error: true
|
Stops processing the request when the PII service fails, so an outage doesn’t send unsanitized prompts to the LLM.
|
The AI Semantic Prompt Guard Policy embeds the prompt with the provider’s embeddings model and searches Redis for the closest deny prompt. A match within the distance threshold blocks the request with 400 and the message bad request, the default rejection_mode: none response. The policy catches rephrased requests that no keyword list covers.
The following settings configure the AI Semantic Prompt Guard Policy:
|
Setting
|
Purpose
|
embeddings.auth and embeddings.model
|
The embeddings credentials and model, from the AI Credentials prerequisite and EMBEDDINGS_MODEL. The policy calls the provider directly and doesn’t use the AI Model Provider, so each provider tab has its own auth fields.
|
vectordb.dimensions
|
The vector size of the embeddings model: 3072 for text-embedding-3-large, 1024 for amazon.titan-embed-text-v2:0. If you change EMBEDDINGS_MODEL, change this value.
|
vectordb.threshold and search.threshold
|
The maximum cosine distance for a match. Lower values are stricter. The OpenAI and Azure OpenAI tabs use 0.5: with text-embedding-3-large, 0.75 also blocked the PII example. The AWS Bedrock tab uses 0.75, which this cookbook hasn’t tested with Titan embeddings. Tune the value for each embeddings model. See Embedding-based similarity matching.
|
vectordb.redis.host
|
The Redis Stack instance that stores the deny prompt embeddings, from REDIS_HOST.
|
rules.deny_prompts
|
Examples of prompts to block. The policy blocks prompts close in meaning, so the list doesn’t need every wording.
|
rules.match_all_conversation_history: true
|
Checks earlier user messages too. The default checks only the latest user message.
|
The AI Prompt Guard Policy checks user messages against regular expressions in the data plane, with no external call. A match blocks the request with 400, the default rejection_mode: none response. It enforces terms that must never reach the model, whatever the wording around them.
The following settings configure the AI Prompt Guard Policy:
|
Setting
|
Purpose
|
deny_patterns
|
Case-insensitive patterns for hack, exploit, malware, and weapon. The patterns also match benign words, for example “productivity hacks”. Add allow_patterns to define what’s allowed, and note that a deny match always wins.
|
The AI Model Provider holds the provider credentials. The multi-layer-ai-guardrails-model AI Model references the AI Auth Strategy and the three guardrail AI Policies, and routes the request to one target.
The following settings configure the AI Model Provider and the AI Model:
|
Setting
|
Purpose
|
config.auth (AI Model Provider)
|
The provider credentials. Kong adds them to each upstream request. The fields depend on the provider tab.
|
policies
|
The three guardrail AI Policies. Kong runs them in priority order, not in list order.
|
formats: [{type: openai}]
|
The AI Model accepts OpenAI-format requests. Kong translates each request to the provider’s native format and the response back to OpenAI format.
|
config.route.model
|
The AI Model answers to the CHAT_MODEL value in the body’s model field (body_param: model), which the demo sends. Without a selector, an AI Model accepts only its own name as model.
|
targets[].name and targets[].config
|
The upstream model, from CHAT_MODEL, and provider-specific fields, for example Bedrock’s region or Azure’s deployment_id and api_version.
|
config.route.protocols: [http, https]
|
Accepts HTTP, so you can call the local data plane at http://localhost:8000.
|
config.max_request_body_size: 10485760
|
Accepts request bodies up to 10 MB, instead of the 8 MB default, for long conversation contexts.
|
config.response_streaming: deny
|
Disables streamed responses, so each response is complete. Keep it if you add response checks, for example sanitization_mode: BOTH.
|
config.logging.payloads: true
|
Kong logs request and response payloads. The default is false. Review payload logging against your data retention rules.
|
Production credentials: This cookbook stores the AI Consumer’s API key and the provider credentials in environment variables for simplicity. In production, use AI Vaults to reference them from a secret manager, for example AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager, or Azure Key Vault.
Kong adds the following response headers to allowed requests:
|
Header
|
Description
|
X-Kong-LLM-Model
|
The upstream model that served the request, with the provider name as a prefix, for example openai/gpt-4o.
|
X-Kong-Upstream-Latency
|
Time in milliseconds that Kong waited for the provider to respond.
|
X-Kong-Proxy-Latency
|
Time in milliseconds that Kong spent processing the request, including the PII service call and the embeddings search.
|