Kong authenticates the request on the /chat AI Model. The Datakit Policy on that AI Model calls the selector AI Model, reads the tier from its answer, calls the matching tier’s AI Model, and returns that response to the client.
sequenceDiagram
participant C as Client
participant K as Kong AI Gateway (/chat)
participant S as Selector AI Model
participant T as Tier AI Model
participant L as OpenAI or AWS Bedrock
C->>K: POST /chat (apikey, messages)
activate K
K->>K: Key Auth AI Auth Strategy validates apikey (else 401)
K->>S: Datakit call to /model-selection
activate S
S-->>K: fast or smart
deactivate S
K->>T: Datakit call to /model-based-routing-fast or /model-based-routing-smart
activate T
T->>L: Request with the AI Model Provider's credentials
activate L
L-->>T: Completion
deactivate L
T-->>K: OpenAI-format response with X-Kong-LLM-Model
deactivate T
K-->>C: Datakit exit returns the tier's response
deactivate KKong selects the AI Model for a request before any AI Policy runs, so a policy can’t move a request to another AI Model. The Datakit Policy works around this: it calls the other AI Models over HTTP and returns their response with an exit node. This cookbook hasn’t confirmed this workflow against a live AI Gateway.
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. All four AI Models reference this AI Auth Strategy in access.auth_strategies, so the Datakit Policy forwards the client’s key on each internal call.
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: false
|
Kong keeps the apikey header on the request, because the Datakit Policy reads it and forwards it on the internal calls. With true, Kong removes the key before the Datakit Policy runs, and the internal calls fail with 401. The upstream providers also receive the apikey header, but each AI Model Provider replaces the Authorization header with its own credentials.
|
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 selector AI Model (/model-selection) is the classifier. The AI Prompt Decorator Policy on it adds a system message before the user’s prompt. The message tells the classifier model to answer with one word, fast or smart.
The following settings configure the AI Prompt Decorator Policy and the selector AI Model:
|
Setting
|
Purpose
|
prompts.prepend
|
The system message with the classification rules. Kong adds it before the user’s messages. Edit it to change what counts as simple or complex.
|
llm_format: openai
|
The policy reads and writes OpenAI-format messages, which match the selector AI Model’s formats.
|
policies: [model-based-routing-decorator]
|
Attaches the policy to the selector AI Model only. The tier AI Models receive the prompt without the classification message.
|
targets[].name
|
The classifier model, from OPENAI_SELECTOR_SLM. A small model is enough, because it returns only one word.
|
The Datakit Policy is attached to the /chat AI Model and runs the routing workflow as a graph of nodes. Its call nodes send requests to the data plane’s own proxy listener at http://localhost:8000, at each AI Model’s path plus /chat/completions, so each internal call goes through the same authentication as a client request.
The following nodes make up the workflow:
|
Setting
|
Purpose
|
CLASSIFIER_BODY, FAST_BODY, SMART_BODY
|
jq nodes that build each internal request body: the client’s messages array and the alias of the AI Model to call (classifier, fast, or smart). A node’s result is read by the node’s name, for example FAST_BODY.
|
EXTRACT_AUTH
|
A jq node that copies the apikey header from the request headers, so the internal calls authenticate as the same AI Consumer.
|
CALL_MODEL_SELECTOR
|
A call node that sends CLASSIFIER_BODY and the key to /model-selection/chat/completions.
|
EXTRACT_TIER, IS_SMART
|
jq nodes that read the classifier’s answer and turn it into a boolean. Any answer other than smart selects the fast tier.
|
ROUTE_DECISION
|
A branch node. It runs CALL_SMART and RESPOND_SMART when the input is true, and CALL_FAST and RESPOND_FAST otherwise.
|
CALL_SMART, CALL_FAST
|
call nodes that send SMART_BODY or FAST_BODY and the key to /model-based-routing-smart/chat/completions or /model-based-routing-fast/chat/completions.
|
RESPOND_SMART, RESPOND_FAST
|
exit nodes that return the tier’s body and headers to the client, including X-Kong-LLM-Model. AI Gateway then doesn’t call the /chat AI Model’s own target.
|
The two AI Model Providers hold the OpenAI and AWS credentials. The selector and fast-tier AI Models use the OpenAI AI Model Provider, and the smart-tier AI Model uses the AWS Bedrock AI Model Provider. Clients call only /chat. The other three AI Models exist for the Datakit Policy, but a client with a valid key can also call them directly, because the internal calls use the client’s own key.
The following settings configure the AI Model Providers and the AI Models:
|
Setting
|
Purpose
|
config.auth (AI Model Providers)
|
The provider credentials. OpenAI uses an Authorization header. AWS Bedrock uses access_key_id and secret_access_key. Kong adds them to each upstream request.
|
targets[].config.region (smart tier)
|
The AWS region for Bedrock, from AWS_REGION. The region belongs to the target, not to the AI Model Provider.
|
targets[].name (fast and smart tiers)
|
The upstream models, from OPENAI_FAST_MODEL and BEDROCK_SMART_MODEL. Change them and re-apply to swap models without changing clients.
|
config.route.model
|
The alias each AI Model answers to in the body’s model field: classifier, fast, smart, and auto for /chat. Without a selector, an AI Model accepts only its own name as model.
|
targets (/chat AI Model)
|
An AI Model needs at least one target. The exit nodes return before Kong uses it.
|
config.response_streaming: deny
|
Clients can’t set stream=true on any of the four AI Models. The Datakit Policy needs complete response bodies to read the tier and to return the answer.
|
config.max_request_body_size
|
Accepts request bodies up to 10 MB on the /chat and tier AI Models, and 5 MB on the selector AI Model.
|
config.route.protocols: [http, https]
|
Accepts HTTP, so you can call the local data plane at http://localhost:8000, and so the Datakit Policy can call the other AI Models over HTTP.
|
config.route.strip_path: true
|
Kong removes the route path before it forwards the request upstream.
|
config.logging.payloads: false
|
Kong doesn’t log request and response payloads.
|
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.
The exit node returns the tier AI Model’s response headers. The demo reads the following headers:
|
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-mini.
|
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, excluding the upstream call.
|