Kong selects the AI Model by path and by the model field. On compressed, the AI Prompt Compressor Policy sends the conversation to Headroom and replaces the messages with the ones that Headroom returns. Then Kong sends the request to OpenAI with the AI Model Provider’s key. On uncompressed, Kong sends the original messages.
sequenceDiagram
participant A as Agent
participant K as Kong AI Gateway
participant H as Headroom
participant O as OpenAI
A->>K: POST /compressed/chat/completions (apikey, x-session-id, model compressed)
activate K
K->>H: AI Prompt Compressor Policy sends POST /v1/compress (messages, session ID, proxy token)
activate H
H-->>K: Compressed messages (else 500 while stop_on_error is true)
deactivate H
K->>K: Key Auth AI Auth Strategy accepts the apikey (else 401)
K->>O: Compressed messages with the OpenAI key
activate O
O-->>K: Completion and token usage
deactivate O
K-->>A: OpenAI-format response
deactivate K
A->>K: POST /uncompressed/chat/completions (apikey, model uncompressed)
activate K
K->>K: Key Auth AI Auth Strategy accepts the apikey (else 401)
K->>O: Original messages with the OpenAI key
activate O
O-->>K: Completion and token usage
deactivate O
K-->>A: OpenAI-format response
deactivate KIn a test on runtime 2.2.0, Kong also sent requests with an invalid or missing key to Headroom, then returned 401 without calling OpenAI.
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. Both AI Models reference this AI Auth Strategy in access.auth_strategies, so one key works for both endpoints.
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. OpenAI 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 Prompt Compressor Policy sends the messages of each request to Headroom and forwards the messages that Headroom returns. Headroom decides what to compress. The policy also sends a session ID, so Headroom can recognize the turns that it already compressed for a conversation and return them unchanged.
The following settings configure the AI Prompt Compressor Policy:
|
Setting
|
Purpose
|
provider: headroom
|
Sends the whole conversation to Headroom. compression_ranges and message_type apply only to the LLMLingua provider. With headroom, they don’t change what Kong sends.
|
compressor_url
|
The Headroom endpoint, from HEADROOM_COMPRESSOR_URL. Kong sends requests to this exact URL, so include the /v1/compress path. Without the path, Konnect accepts the configuration, but requests to /compressed fail with 500. By default, Headroom keeps sessions in memory in one process, so point the URL at one instance.
|
headroom.proxy_token
|
The Headroom proxy token, from HEADROOM_PROXY_TOKEN. It’s required when compressor_url isn’t a loopback address, which includes the quickstart setup. Without it, kongctl apply fails with headroom.proxy_token is required when compressor_url is not a loopback address (localhost, 127.0.0.0/8, ::1). The field isn’t write-only: the Konnect API returns its value in plain text.
|
headroom.session_id_headers (default)
|
The headers that identify a conversation. The default list includes x-session-id, which the demo sets to one value for each conversation. Without a session header, the policy builds the session ID from the model, the system message, and the first user message. Each session ID is also scoped to the AI Consumer.
|
timeout: 45000
|
The time in milliseconds that the policy waits for Headroom. Keep it higher than the Headroom per-session turn lock (10 seconds) plus the Headroom compression timeout (30 seconds by default). The default, 10000, is lower than that sum.
|
stop_on_error: true
|
When the Headroom call fails, Kong returns 500 with {"error":{"message":"failed to compress prompts: Failure when calling the Headroom compression service"}} and doesn’t call OpenAI.
|
global: false
|
The policy applies only to the AI Models that list it in policies.
|
Headroom picks a compression method for each message from its content. With Headroom 0.41.0, OpenAI received the following text for message 3 in step 1, 12,021 characters instead of the 25,540 characters of JSON:
"[200]{id:int,latency_ms:int,level:string,msg:string,service:string,ts:string}\n0,56,INFO,request completed,search,2026-10-18T09:00:00Z\n1,184,INFO,request completed,payments,2026-10-18T09:01:07Z\n...
The script sent the tool result as a JSON string, so Headroom returned the table as a JSON string too: it starts with " and separates lines with \n. The header line lists each field and its type once, in alphabetical order, and each record follows on its own line. All 200 records are present, including the three errors:
37,189,ERROR,card processor returned 502,payments,2026-10-18T09:37:19Z
118,67,ERROR,upstream timeout after 30000ms,checkout,2026-10-18T09:58:46Z
164,160,ERROR,connection reset by peer,auth,2026-10-18T09:44:08Z
In tests with Headroom 0.41.0, Headroom also showed the following behavior:
- It didn’t change the latest user message or short inputs.
- It didn’t compress the outputs of tools with names such as
read_file, Read, Grep, web_fetch, and web_search.
- It reduced a JSON array of nested objects to a subset of its records, with no marker in the text. In a request with 30 nested order records, the model received 15. With the tool listed in
HEADROOM_PROTECT_TOOL_RESULTS, the model received all 30, and the request was 23% smaller.
- With the same session ID, it returned the earlier turns of a conversation with the same bytes as on the previous turn. This keeps the start of the prompt the same for the OpenAI prompt cache.
gpt-4o-mini misread the log table, with the integer columns id and latency_ms next to each other, in three of three runs. gpt-4o and gpt-4.1-mini read it correctly. Check the answers of your model, not only the token counts.
The AI Model Provider holds the OpenAI API key and adds it to each request to OpenAI. The two AI Models, compressed and uncompressed, have their own paths and one target each, which calls the same OpenAI model through the AI Model Provider. Only compressed lists the AI Prompt Compressor Policy, so compression is the only difference between the endpoints.
The following settings configure the AI Model Provider and the AI Models:
|
Setting
|
Purpose
|
config.auth (AI Model Provider)
|
The OpenAI API key, from OPENAI_TOKEN. Kong adds it to each request to OpenAI. The field is write-only, so Konnect never returns it.
|
name
|
compressed or uncompressed. The AI Models don’t set config.route.model, so the request’s model field must be the AI Model name, not the OpenAI model. Another value, including gpt-4.1-mini, returns 500 with failed the initial dns/balancer resolve for 'ai-gateway.upstream.local'.
|
policies
|
[headroom-compressor] on compressed attaches the AI Prompt Compressor Policy by name. [] on uncompressed sends requests to OpenAI without a call to Headroom.
|
access.auth_strategies
|
Both AI Models require a key from the Key Auth AI Auth Strategy.
|
formats: [{type: openai}] and capabilities: [generate]
|
The AI Models accept OpenAI-format chat completion requests.
|
config.route.paths
|
/compressed or /uncompressed. The generate capability adds /chat/completions, so the endpoints are /compressed/chat/completions and /uncompressed/chat/completions.
|
targets[].name
|
The OpenAI model that both AI Models call, from CHAT_MODEL.
|
config.route.protocols: [http, https]
|
Accepts HTTP, so you can call the local data plane at http://localhost:8000.
|
config.route.strip_path: true
|
Kong removes /compressed or /uncompressed from the path before it forwards the request.
|
config.max_request_body_size: 10485760
|
Accepts request bodies up to 10 MB, instead of the 8 MB default, for conversations with large tool results.
|
config.response_streaming: allow
|
Clients can request streamed responses. Compression applies to the request, so OpenAI bills a streamed request through /compressed for the compressed input.
|
Production credentials: This cookbook stores the AI Consumer’s API key, the OpenAI API key, and the Headroom proxy token 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:
|
Header
|
Description
|
X-Kong-LLM-Model
|
The upstream model that served the request, with the provider name as a prefix, for example openai/gpt-4.1-mini.
|
X-Kong-Proxy-Latency
|
Time in milliseconds that Kong spent processing the request, excluding the OpenAI call. On /compressed, it includes the Headroom call.
|