Kong authenticates each request with the Key Auth AI Auth Strategy, sends the prompt text to the guardrail service through the guardrail AI Policy, and forwards only allowed requests to OpenAI with the AI Model Provider’s credentials.
sequenceDiagram
participant C as Client
participant K as Kong AI Gateway
participant G as Guardrail Service
participant L as LLM Provider
C->>K: POST /guardrail-integrations (apikey header, prompt)
activate K
K->>K: Key Auth AI Auth Strategy validates apikey (else 401)
K->>G: Guardrail AI Policy sends the prompt text
activate G
G-->>K: Verdict
deactivate G
K->>K: Guardrail AI Policy allows safe content (else 400)
K->>K: AI Model Provider adds provider credentials
K->>L: Forwarded request
activate L
L-->>K: Response
deactivate L
K-->>C: OpenAI-format response with X-Kong-LLM-Model
deactivate KAuthentication 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. The AI Model references this AI Auth Strategy in access.auth_strategies, so Kong rejects an unknown key before the guardrail AI Policy runs.
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 Azure Content Safety Policy is a dedicated integration with the Azure AI Content Safety text analysis API. You set the URL, the key, and a threshold for each category. The AI Policy builds the request, reads the severity scores, and blocks the request when a score reaches its threshold.
The following settings configure the AI Azure Content Safety Policy:
|
Setting
|
Purpose
|
content_safety_url
|
The full text analysis URL, from AZURE_CONTENT_SAFETY_URL. The AI Policy adds the api-version query parameter from azure_api_version, which defaults to 2023-10-01.
|
content_safety_key
|
The Content Safety key, from AZURE_CONTENT_SAFETY_KEY. kongctl doesn’t accept !secret in AI Policy configuration, so the value comes from a plain !env reference.
|
guarding_mode: INPUT
|
Checks the request only. OUTPUT checks the LLM response, and BOTH checks both.
|
text_source: concatenate_user_content
|
Sends the text of all user messages to Azure.
|
categories[].rejection_level: 2
|
Blocks a request when Azure scores Hate, SelfHarm, Sexual, or Violence at severity 2 or higher. Lower values are stricter.
|
output_type: FourSeverityLevels
|
Azure returns a severity of 0, 2, 4, or 6. EightSeverityLevels returns a severity from 0 to 7.
|
reveal_failure_reason: true
|
Tells the client why Kong rejected the request. Set it to false to hide the moderation details from end users.
|
stop_on_error: true
|
Stops processing the request when the Content Safety API returns an error, so an Azure outage doesn’t let unchecked prompts through.
|
The AI Custom Guardrail Policy calls any HTTP guardrail service that you describe in configuration. params holds reusable values, request builds the HTTP call, functions holds Lua functions that read the service’s response, and response maps the function results to the block decision. The two custom configurations use the same structure for two services with different APIs: Azure scores severity for each category, and Mistral returns a flag for each category.
The following settings configure the AI Custom Guardrail Policy:
|
Setting
|
Purpose
|
params
|
Values that the other sections read with $(conf.params.<key>): the Azure URL and key, or the Mistral key and moderation model. A kongctl !env tag replaces a whole value, so the Azure URL comes from AZURE_CONTENT_SAFETY_URL in full.
|
request.url
|
The guardrail endpoint: $(conf.params.content_safety_url) for Azure, https://api.mistral.ai/v1/moderations for Mistral.
|
request.queries
|
Query parameters. The Azure configuration sets api-version to 2023-10-01.
|
request.headers
|
The service credentials: Ocp-Apim-Subscription-Key for Azure, Authorization: Bearer for Mistral.
|
request.body
|
The JSON body. $(content) is the text that the AI Policy checks. Kong decodes a string value as JSON when it can, so the Azure categories value becomes a JSON array and the outputType value becomes a JSON string.
|
functions
|
Lua functions that take the service response. The Azure function blocks when a category has severity 2 or higher. The Mistral function blocks when Mistral flags any category.
|
response.block and response.block_message
|
Read the decision and the message from the function result, with $(<function>.<field>).
|
guarding_mode
|
INPUT for Azure. BOTH for Mistral, which also checks the LLM response, with text_source: concatenate_all_content.
|
stop_on_error: true and timeout: 10000
|
Stops processing the request when the guardrail call fails, and waits up to 10 seconds for the service.
|
The AI Custom Guardrail reference lists the template variables, for example $(source), which is INPUT or OUTPUT.
The AI Model Provider holds the OpenAI credentials. The guardrail-integrations-model AI Model references the AI Auth Strategy and one guardrail AI Policy, and routes allowed requests to one OpenAI target. Each configuration uses the same AI Model and changes only the AI Policy in policies.
The following settings configure the AI Model Provider and the AI Model:
|
Setting
|
Purpose
|
config.auth (AI Model Provider)
|
The OpenAI credentials. Kong adds them to each upstream request.
|
policies
|
The guardrail AI Policy for the applied configuration. It runs on each request to this AI Model, after authentication.
|
formats: [{type: openai}]
|
The AI Model accepts OpenAI-format requests.
|
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
|
The upstream model, from CHAT_MODEL. Change it and re-apply to swap models without changing clients.
|
config.route.protocols: [http, https]
|
Accepts HTTP, so you can call the local data plane at http://localhost:8000.
|
config.max_request_body_size: 1048576
|
Rejects request bodies larger than 1 MB, lower than the 8 MB default. Raise it for long conversation contexts.
|
config.response_streaming: deny
|
Disables streamed responses. With guarding_mode: BOTH, the Mistral configuration checks the complete LLM response before Kong returns it.
|
config.logging.payloads: true
|
Kong logs request and response payloads. The default is false.
|
Production credentials: This cookbook stores the AI Consumer’s API key, the OpenAI credentials, and the guardrail service keys 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 guardrail call.
|