Apply the configuration, connect Open WebUI to Kong, then send requests with and without the AI Consumer key and chat from the browser.
The configuration creates an AI Model Provider with the organization’s OpenAI key, a Key Auth AI Auth Strategy, an AI Consumer with an API key, a global Pre-Function Policy that copies the Bearer token into the apikey header, and an AI Model on /coding-agents. A kongctl namespace scopes every resource, so you can remove this configuration without affecting anything else on the same AI Gateway. See the kongctl documentation for more on namespaces.
Adopt the quickstart AI Gateway into a kongctl namespace so that kongctl can manage it. Adoption adds the KONGCTL-namespace label to the AI Gateway:
kongctl adopt ai-gateway "${KONNECT_CONTROL_PLANE_NAME}" \
--namespace "${KONNECT_CONTROL_PLANE_NAME}" \
--pat "${KONNECT_TOKEN}"
Generate the AI Consumer’s API key. Open WebUI sends this key to Kong instead of an OpenAI key. The credential’s api_key field is write-only, so its value must come from an environment variable:
export CONSUMER_API_KEY="coding-agents-demo-$(openssl rand -hex 8)"
Select your provider. The tab exports CHAT_MODEL, the only model ID that the AI Model accepts and the model that Kong requests from OpenAI, then applies the configuration. Provider credentials come from the prerequisites.
Export the model env var:
export CHAT_MODEL='gpt-4o-mini'
Apply the Kong configuration:
cat <<'EOF' > kong-recipe.yaml
_defaults:
kongctl:
namespace: coding-agents-open-webui-recipe
ai_gateway_model_providers:
- ref: coding-agents-openai-provider
ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
name: coding-agents-openai-provider
display_name: coding-agents-openai
type: openai
config:
auth:
type: basic
headers:
- name: Authorization
value: !secret {source: !env 'OPENAI_TOKEN'}
ai_gateway_auth_strategies:
- ref: coding-agents-key-auth
ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
name: coding-agents-key-auth
display_name: coding-agents key auth
type: key-auth
config:
key_names:
- apikey
hide_credentials: true
ai_gateway_consumers:
- ref: coding-agents-consumer
ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
name: coding-agents-consumer
display_name: coding-agents demo consumer
type: api-key
credentials:
- ref: coding-agents-credential
name: coding-agents-credential
display_name: coding-agents demo API key
type: api-key
api_key: !secret {source: !env 'CONSUMER_API_KEY'}
ai_gateway_policies:
- ref: coding-agents-bearer-to-apikey
ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
name: coding-agents-bearer-to-apikey
display_name: coding-agents Bearer to apikey
type: pre-function
global: true
config:
rewrite:
- |
local auth = kong.request.get_header("Authorization")
if auth and string.sub(string.lower(auth), 1, 7) == "bearer " then
ngx.req.set_header("apikey", string.sub(auth, 8))
end
ai_gateway_models:
- ref: coding-agents-openai
ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
name: coding-agents-openai
display_name: coding-agents (OpenAI-compatible)
type: model
formats:
- type: openai
capabilities:
- generate
access:
auth_strategies:
- coding-agents-key-auth
config:
max_request_body_size: 16777216
response_streaming: allow
logging:
payloads: true
route:
paths:
- /coding-agents
protocols:
- http
- https
methods:
- POST
- GET
- OPTIONS
strip_path: true
model:
body_param: model
values:
- !env 'CHAT_MODEL'
targets:
- name: !env 'CHAT_MODEL'
provider: coding-agents-openai-provider
config:
type: openai
EOF
kongctl apply -f kong-recipe.yaml -o text --auto-approve --pat "${KONNECT_TOKEN}"
rm -f kong-recipe.yaml
Run Open WebUI in Docker with an OpenAI-compatible connection to the AI Model. The following Open WebUI environment variables configure the connection:
OPENAI_API_BASE_URLS and OPENAI_API_KEYS set the connection URL and the AI Consumer key. host.docker.internal lets the container reach the data plane on port 8000 of your host.
OPENAI_API_CONFIGS sets the options of each connection, keyed by the connection’s index. model_ids lists the model IDs of the connection, because the AI Model doesn’t serve a model list.
DEFAULT_MODELS selects the model on a new chat.
ENABLE_OLLAMA_API set to False turns off the default Ollama connection.
WEBUI_AUTH set to False turns off the Open WebUI sign-in for this local test. Open WebUI allows this only on a new installation with no users.
Docker Compose replaces ${CONSUMER_API_KEY} and ${CHAT_MODEL} with the values from your shell (variable interpolation), so the model ID always matches the AI Model. Create the Compose file in an empty directory:
cat <<'EOF' > compose.yaml
services:
open-webui:
image: ghcr.io/open-webui/open-webui:main
ports:
- "3081:8080"
environment:
OPENAI_API_BASE_URLS: "http://host.docker.internal:8000/coding-agents"
OPENAI_API_KEYS: "${CONSUMER_API_KEY}"
OPENAI_API_CONFIGS: "{\"0\": {\"enable\": true, \"model_ids\": [\"${CHAT_MODEL}\"]}}"
DEFAULT_MODELS: "${CHAT_MODEL}"
ENABLE_OLLAMA_API: "False"
WEBUI_AUTH: "False"
extra_hosts:
- "host.docker.internal:host-gateway"
volumes:
- open-webui:/app/backend/data
volumes:
open-webui:
EOF
Start Open WebUI from the same directory, in the shell where CONSUMER_API_KEY and CHAT_MODEL are exported:
Open WebUI saves these connection values in its database at the first start, and uses the saved values instead of the environment variables after a restart. To apply changed values, run docker compose down -v and start Open WebUI again.
Call Chat Completions with the AI Consumer key as a Bearer token, the same way Open WebUI does:
curl -sS -X POST "http://localhost:8000/coding-agents/chat/completions" \
-H "Authorization: Bearer ${CONSUMER_API_KEY}" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"${CHAT_MODEL}\",
\"messages\": [{\"role\": \"user\", \"content\": \"Reply with exactly: open-webui-ok\"}]
}"
The response is an OpenAI Chat Completions object, with different IDs and token counts:
{
"id": "chatcmpl-...",
"object": "chat.completion",
"model": "gpt-4o-mini-2024-07-18",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "open-webui-ok"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 5,
"total_tokens": 20
}
}
Send the same request without the Authorization header:
curl -sS -o /dev/null -w '%{http_code}\n' -X POST "http://localhost:8000/coding-agents/chat/completions" \
-H "Content-Type: application/json" \
-d "{\"model\": \"${CHAT_MODEL}\", \"messages\": [{\"role\": \"user\", \"content\": \"hi\"}]}"
Kong rejects the request before it reaches OpenAI:
Open http://localhost:3081, select the model from CHAT_MODEL, and send Reply with exactly: open-webui-ok. Open WebUI shows the reply open-webui-ok.
The connection settings, under Settings > Connections in the admin section, have a Verify Connection button that calls /models. The AI Model doesn’t serve /models, so the verification fails, but chat still works (Open WebUI: connection verification).
The requests and the Open WebUI chat confirm the following:
|
Behavior
|
Evidence
|
|
AI Consumer authentication with a Bearer token
|
The request with Authorization: Bearer and the AI Consumer key returns a completion. The Pre-Function Policy copied the token into apikey, and the Key Auth AI Auth Strategy matched it to coding-agents-consumer.
|
|
Authentication before any upstream call
|
The request without a key gets 401. Kong never contacted OpenAI.
|
|
One model ID
|
Open WebUI lists only the model from model_ids, and the AI Model accepts it because it equals CHAT_MODEL.
|
|
Provider credentials stay in Kong
|
Open WebUI only held the AI Consumer key. The AI Model Provider added the OpenAI key to each upstream request.
|
To review the created entities, and per-consumer usage, open the coding-agents-open-webui-recipe AI Gateway in Konnect. For traffic across all your AI Gateways, use the Observability menu.