Apply the configuration, send requests with and without the AI Consumer key, then connect LibreChat to Kong and chat through it.
The configuration creates an AI Model Provider with your 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 at /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}"
Export the AI Consumer’s API key and the model. LibreChat sends this key instead of an OpenAI key. The credential’s api_key field is write-only, so its value must come from an environment variable. CHAT_MODEL is the only model name that the AI Model accepts, and the upstream model that Kong sends to OpenAI:
export CONSUMER_API_KEY="coding-agents-demo-$(openssl rand -hex 8)"
export CHAT_MODEL='gpt-4o-mini'
Apply the Kong configuration. The OpenAI key comes from the prerequisites:
cat <<'EOF' > kong-recipe.yaml
_defaults:
kongctl:
namespace: coding-agents-librechat-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
Call Chat Completions through Kong with the AI Consumer key as a Bearer token, the way LibreChat sends it:
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: smoke-ok\"}]}"
Kong returns OpenAI’s Chat Completions response. Your IDs and token counts differ:
{
"id": "chatcmpl-...",
"object": "chat.completion",
"model": "gpt-4o-mini-2024-07-18",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "smoke-ok"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 14,
"completion_tokens": 3,
"total_tokens": 17
}
}
Repeat the 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:
LibreChat connects to OpenAI-compatible APIs through a custom endpoint in librechat.yaml. LibreChat recommends a custom endpoint over the OPENAI_REVERSE_PROXY variable, which its environment reference marks as deprecated.
Create the LibreChat configuration. The models.default value must equal CHAT_MODEL. If you exported another model, change it:
cat <<'EOF' > librechat.yaml
version: 1.3.19
endpoints:
custom:
- name: 'Kong'
apiKey: '${CONSUMER_API_KEY}'
baseURL: 'http://host.docker.internal:8000/coding-agents'
models:
default: ['gpt-4o-mini']
fetch: false
EOF
Create the Compose file. It runs LibreChat and MongoDB, mounts librechat.yaml into the LibreChat container, and passes CONSUMER_API_KEY from your shell to LibreChat:
cat <<'EOF' > librechat-compose.yaml
services:
librechat:
image: registry.librechat.ai/librechat-ai/librechat-dev:latest
ports:
- "3080:3080"
environment:
HOST: "0.0.0.0"
PORT: "3080"
MONGO_URI: "mongodb://mongo:27017/LibreChat"
ALLOW_EMAIL_LOGIN: "true"
ALLOW_REGISTRATION: "true"
CONSUMER_API_KEY: "${CONSUMER_API_KEY}"
volumes:
- ./librechat.yaml:/app/librechat.yaml
extra_hosts:
- "host.docker.internal:host-gateway"
depends_on:
- mongo
mongo:
image: mongo:8.0.20
volumes:
- librechat-mongo:/data/db
volumes:
librechat-mongo:
EOF
Start LibreChat in the shell where you exported CONSUMER_API_KEY:
docker compose -f librechat-compose.yaml up -d
LibreChat generates its encryption and session secrets (CREDS_KEY, CREDS_IV, JWT_SECRET, and JWT_REFRESH_SECRET) when they’re blank (LibreChat environment reference). In production, set fixed values.
Open http://localhost:3080, register an account, and sign in. Select the Kong endpoint and the gpt-4o-mini model, then send:
Reply with exactly: librechat-ok
LibreChat shows the reply librechat-ok.
The requests confirm the following:
|
Behavior
|
Evidence
|
|
AI Consumer authentication with a Bearer token
|
The request with Authorization: Bearer and the AI Consumer key succeeds. The Pre-Function Policy copied the key into apikey, where the Key Auth AI Auth Strategy reads it.
|
|
Authentication before any upstream call
|
The request without a key gets 401. Kong never contacted OpenAI.
|
|
LibreChat chats through Kong
|
LibreChat answers through the Kong endpoint. The container has no OpenAI key, only CONSUMER_API_KEY.
|
|
The OpenAI key stays in Kong
|
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-librechat-recipe AI Gateway in Konnect. For traffic across all your AI Gateways, use the Observability menu.