Apply the configuration, put the data plane on a public URL, connect Cursor to Kong, then send requests with and without the AI Consumer key and a prompt from Cursor.
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. Cursor 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-cursor-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
Cursor’s servers call the base URL that you set in Cursor, so localhost doesn’t work. Start an ngrok tunnel to the data plane proxy on port 8000 in a separate terminal and keep it running:
ngrok prints a forwarding URL on your account’s dev domain, for example https://your-assigned-name.ngrok-free.dev. Export it in the terminal where you run the rest of this cookbook:
export KONG_PUBLIC_URL='https://your-assigned-name.ngrok-free.dev'
The tunnel makes every route on the data plane reachable from the internet. The Key Auth AI Auth Strategy rejects requests to /coding-agents without a valid AI Consumer key, but anyone who has the key can use the organization’s OpenAI account through the URL. Stop the tunnel when you finish, and don’t add routes without authentication to this AI Gateway while it runs.
Call Chat Completions through the tunnel with the AI Consumer key as a Bearer token, the way Cursor sends it:
curl -sS -X POST "${KONG_PUBLIC_URL}/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: cursor-ok\"}]}"
Kong returns an OpenAI Chat Completions response. Your IDs and token counts differ:
{
"id": "chatcmpl-...",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "cursor-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 "${KONG_PUBLIC_URL}/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:
In Cursor’s Models settings, the OpenAI provider has an API Key field, a Use API Key switch that appears after you save a key, and a switch that overrides the base URL (Cursor release notes). Print the two values that Cursor needs:
echo "API key: ${CONSUMER_API_KEY}"
echo "Base URL: ${KONG_PUBLIC_URL}/coding-agents"
Configure Cursor:
- Open Cursor Settings > Models.
- Paste the API key into the OpenAI API Key field, and turn on Use API Key.
- Turn on the OpenAI base URL override, and paste the base URL into its field. Include the
/coding-agents path, because the AI Model matches requests on that path.
- In the chat model picker, select a model named exactly the value of
CHAT_MODEL. If the picker doesn’t list it, add a model with that name in Cursor Settings > Models. The AI Model accepts only that name.
Own API keys work only with chat models, so Tab completion keeps using Cursor’s built-in models.
Send a chat prompt from Cursor with the CHAT_MODEL model selected:
Reply with exactly: cursor-ok
Cursor replies with cursor-ok. To see the request that Cursor’s servers sent, open the ngrok web inspection interface at http://localhost:4040. It lists POST /coding-agents/chat/completions.
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.
|
|
Cursor reaches Kong through the public URL
|
Cursor answers with cursor-ok, and the ngrok web inspection interface shows the request from Cursor’s servers.
|
|
The OpenAI key stays in Kong
|
Cursor and the curl requests 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-cursor-recipe AI Gateway in Konnect. For traffic across all your AI Gateways, use the Observability menu.