Apply the configuration, connect Claude Code to Kong, then send requests with and without the AI Consumer key and run a prompt from Claude Code.
The configuration creates an AI Model Provider with your Anthropic API key, a Key Auth AI Auth Strategy, an AI Consumer with an API key, and a passthrough AI Model on /coding-agents-anthropic. 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. Claude Code sends this key to Kong instead of an Anthropic 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 Anthropic credentials come from the prerequisites:
cat <<'EOF' > kong-recipe.yaml
_defaults:
kongctl:
namespace: coding-agents-claude-code-recipe
ai_gateway_model_providers:
- ref: coding-agents-anthropic-provider
ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
name: coding-agents-anthropic-provider
display_name: coding-agents-anthropic
type: anthropic
config:
auth:
type: basic
headers:
- name: x-api-key
value: !secret {source: !env 'ANTHROPIC_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:
- x-api-key
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_models:
- ref: coding-agents-anthropic
ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
name: coding-agents-anthropic
display_name: coding-agents (Anthropic managed)
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-anthropic
protocols:
- http
- https
methods:
- POST
- OPTIONS
strip_path: true
targets:
- name: anthropic-api
provider: coding-agents-anthropic-provider
config:
version: !env 'ANTHROPIC_VERSION'
type: anthropic
upstream_url: https://api.anthropic.com/
EOF
kongctl apply -f kong-recipe.yaml -o text --auto-approve --pat "${KONNECT_TOKEN}"
rm -f kong-recipe.yaml
Claude Code reads its gateway settings from environment variables (Connect Claude Code to an LLM gateway). Point Claude Code at the AI Model and use the AI Consumer key as the API key:
export ANTHROPIC_BASE_URL='http://localhost:8000/coding-agents-anthropic'
export ANTHROPIC_API_KEY="${CONSUMER_API_KEY}"
unset ANTHROPIC_AUTH_TOKEN
These commands set the following values:
ANTHROPIC_BASE_URL sends Claude Code’s requests to the AI Model. Claude Code adds /v1/messages to it.
ANTHROPIC_API_KEY holds the AI Consumer key. Claude Code sends it in the x-api-key header, which the Key Auth AI Auth Strategy reads.
unset ANTHROPIC_AUTH_TOKEN makes sure that Claude Code sends the AI Consumer key. Claude Code uses ANTHROPIC_AUTH_TOKEN before ANTHROPIC_API_KEY when both are set (authentication precedence).
In an interactive session, Claude Code asks once whether to use the API key. Approve it. In non-interactive mode (-p), Claude Code always uses the key.
Call the Messages API through Kong with the AI Consumer key in the x-api-key header, the same header that Claude Code sends:
curl -sS -X POST "http://localhost:8000/coding-agents-anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-H "x-api-key: ${CONSUMER_API_KEY}" \
-d '{
"model": "claude-haiku-4-5-20251001",
"max_tokens": 32,
"messages": [{"role": "user", "content": "Reply with exactly: smoke-ok"}]
}'
Kong returns Anthropic’s Messages API response unchanged. The response looks similar to the following, with different IDs and token counts:
{
"id": "msg_01...",
"type": "message",
"role": "assistant",
"model": "claude-haiku-4-5-20251001",
"content": [
{
"type": "text",
"text": "smoke-ok"
}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 14,
"output_tokens": 6
}
}
Send the same request without the x-api-key header:
curl -sS -o /dev/null -w '%{http_code}\n' -X POST "http://localhost:8000/coding-agents-anthropic/v1/messages" \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{"model": "claude-haiku-4-5-20251001", "max_tokens": 32, "messages": [{"role": "user", "content": "hi"}]}'
Kong rejects the request before it reaches Anthropic:
Run a one-shot prompt with the environment from Connect Claude Code to Kong:
claude -p "Reply with exactly: claude-ok" --output-format text --max-turns 1
Claude Code prints the reply:
The requests and the Claude Code prompt confirm the following:
|
Behavior
|
Evidence
|
|
Kong holds the Anthropic API key
|
The request with only the AI Consumer key gets a Messages API response from Anthropic. No Anthropic key is set on the client.
|
|
AI Consumer authentication
|
The request without x-api-key returns 401, and Anthropic never receives it.
|
|
Claude Code works through Kong
|
claude -p prints claude-ok with ANTHROPIC_BASE_URL pointing at the AI Model.
|
To review the created entities and per-consumer usage, open the coding-agents-claude-code-recipe AI Gateway in Konnect. For traffic across all your AI Gateways, use the Observability menu.