Apply the configuration, connect Qwen Code to Kong, then send requests with and without the AI Consumer key and run a prompt from Qwen Code.
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. Qwen Code 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 name 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-qwen-code-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
Qwen Code takes its OpenAI-compatible settings from command-line flags, which take precedence over environment variables and settings files (Qwen Code authentication). This cookbook passes the settings as flags, so nothing persists in your shell profile or ~/.qwen/settings.json. The prompt in Run a prompt from Qwen Code uses these flags:
--auth-type openai selects the OpenAI-compatible protocol.
--openai-base-url is the AI Model path, http://localhost:8000/coding-agents. Qwen Code adds /chat/completions to it.
--openai-api-key is the AI Consumer key from CONSUMER_API_KEY.
--model is the model name, from CHAT_MODEL. The AI Model accepts only that name.
To configure Qwen Code once instead of in each command, set the OPENAI_BASE_URL, OPENAI_API_KEY, and OPENAI_MODEL environment variables, or add a provider entry to ~/.qwen/settings.json (Qwen Code model providers).
Call Chat Completions with the AI Consumer key as a Bearer token, the same way Qwen Code 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: smoke-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": "smoke-ok"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 14,
"completion_tokens": 3,
"total_tokens": 17
}
}
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:
Run a one-shot prompt in headless mode with the flags from Connect Qwen Code to Kong:
qwen --prompt "Reply with exactly: qwen-ok" \
--auth-type openai \
--openai-base-url 'http://localhost:8000/coding-agents' \
--openai-api-key "${CONSUMER_API_KEY}" \
--model "${CHAT_MODEL}"
Qwen Code prints qwen-ok.
The requests and the Qwen Code prompt 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.
|
|
Qwen Code through Kong
|
The qwen prompt prints qwen-ok. Qwen Code sent the CHAT_MODEL name, which the AI Model accepts.
|
|
Provider credentials stay in Kong
|
Qwen Code 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-qwen-code-recipe AI Gateway in Konnect. For traffic across all your AI Gateways, use the Observability menu.