Apply the configuration, connect Codex CLI to Kong, then send requests with and without the AI Consumer key and run a prompt from Codex.
The configuration creates an AI Model Provider with your OpenAI API key, a Key Auth AI Auth Strategy, an AI Consumer with an API key, a global Pre-Function Policy that moves a Bearer token to the apikey header, and a passthrough AI Model on /coding-agents-responses. 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. Codex 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)"
Apply the configuration. The OpenAI credentials come from the prerequisites:
cat <<'EOF' > kong-recipe.yaml
_defaults:
kongctl:
namespace: coding-agents-codex-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-responses
ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
name: coding-agents-openai-responses
display_name: coding-agents (OpenAI Responses)
type: model
formats:
- type: passthrough
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-responses
protocols:
- http
- https
methods:
- POST
- GET
- OPTIONS
strip_path: true
targets:
- name: openai-responses
provider: coding-agents-openai-provider
config:
type: openai
upstream_url: https://api.openai.com/
EOF
kongctl apply -f kong-recipe.yaml -o text --auto-approve --pat "${KONNECT_TOKEN}"
rm -f kong-recipe.yaml
Codex reaches a gateway through a custom model provider. Put the provider in a profile file, so that your default Codex configuration doesn’t change. Codex reads model_provider and model_providers only from user-level files, not from a project’s .codex/config.toml.
Create the profile file:
mkdir -p ~/.codex
cat <<'EOF' > ~/.codex/kong.config.toml
model = "gpt-4o-mini"
model_provider = "kong"
[model_providers.kong]
name = "Kong AI Gateway"
base_url = "http://localhost:8000/coding-agents-responses/v1"
env_key = "CODEX_API_KEY"
wire_api = "responses"
EOF
The profile sets the following values:
model is the model that Codex requests. Kong passes it to OpenAI unchanged, so use a model that your OpenAI account can access.
base_url includes the /v1 API path, as OpenAI’s gateway guide shows. Codex adds /responses to it.
env_key names the environment variable that holds the API key. Codex sends its value in the Authorization header as a Bearer token.
wire_api = "responses" selects the Responses API, the only value that Codex supports.
Export the AI Consumer key under the name in env_key:
export CODEX_API_KEY="${CONSUMER_API_KEY}"
Call the Responses API through Kong with the AI Consumer key as a Bearer token, the way Codex sends it:
curl -sS -X POST "http://localhost:8000/coding-agents-responses/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${CONSUMER_API_KEY}" \
-d '{
"model": "gpt-4o-mini",
"input": "Reply with exactly: smoke-ok"
}'
Kong returns OpenAI’s Responses API response unchanged. The response looks similar to the following shortened example, with different IDs and token counts:
{
"id": "resp_...",
"object": "response",
"status": "completed",
"model": "gpt-4o-mini-2024-07-18",
"output": [
{
"type": "message",
"role": "assistant",
"content": [
{
"type": "output_text",
"text": "smoke-ok"
}
]
}
],
"usage": {
"input_tokens": 13,
"output_tokens": 4,
"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-responses/v1/responses" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-4o-mini", "input": "hi"}'
Kong rejects the request before it reaches OpenAI:
Run a one-shot prompt with the kong profile from Connect Codex CLI to Kong. The --skip-git-repo-check flag lets Codex run outside a Git repository:
codex exec --profile kong --skip-git-repo-check "Reply with exactly: codex-ok"
Codex prints codex-ok as the final response. Codex can also warn that it has no model metadata for gpt-4o-mini and uses fallback metadata. The request still goes through Kong.
The requests and the Codex prompt confirm the following:
|
Behavior
|
Evidence
|
|
Kong holds the OpenAI API key
|
The request with only the AI Consumer key gets a Responses API response from OpenAI. No OpenAI key is set on the client.
|
|
Bearer token authentication
|
The request with Authorization: Bearer succeeds, because the Pre-Function Policy moves the token to the apikey header.
|
|
AI Consumer authentication
|
The request without a key returns 401, and OpenAI never receives it.
|
|
Codex works through Kong
|
codex exec --profile kong prints codex-ok through the Responses API endpoint on the AI Model.
|
To review the created entities and per-consumer usage, open the coding-agents-codex-recipe AI Gateway in Konnect. For traffic across all your AI Gateways, use the Observability menu.