Apply the configuration, connect Gemini CLI to Kong, then send requests with and without the AI Consumer key and a prompt from Gemini CLI.
The configuration creates an AI Model Provider with your Gemini API key, a Key Auth AI Auth Strategy, an AI Consumer with an API key, and a passthrough AI Model at /coding-agents-gemini. 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. Gemini CLI sends this key instead of a Google AI Studio 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 Kong configuration. The Gemini API key comes from the prerequisites:
cat <<'EOF' > kong-recipe.yaml
_defaults:
kongctl:
namespace: coding-agents-gemini-cli-recipe
ai_gateway_model_providers:
- ref: coding-agents-gemini-provider
ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
name: coding-agents-gemini-provider
display_name: coding-agents-gemini
type: gemini
config:
auth:
type: basic
headers:
- name: x-goog-api-key
value: !secret {source: !env 'GEMINI_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-goog-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-gemini
ai_gateway: !lookup {id: !env 'AI_GATEWAY_ID'}
name: coding-agents-gemini
display_name: coding-agents (Gemini)
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-gemini
protocols:
- http
- https
methods:
- POST
- GET
- OPTIONS
strip_path: true
targets:
- name: gemini-api
provider: coding-agents-gemini-provider
config:
type: gemini
upstream_url: https://generativelanguage.googleapis.com/
EOF
kongctl apply -f kong-recipe.yaml -o text --auto-approve --pat "${KONNECT_TOKEN}"
rm -f kong-recipe.yaml
Gemini CLI reads its base URL from GOOGLE_GEMINI_BASE_URL when it uses Gemini API key authentication. The URL must use HTTPS unless it points to localhost, so the local data plane’s HTTP port works. Set the base URL to the AI Model’s path, and put the AI Consumer key in GEMINI_API_KEY instead of a Google AI Studio key:
export GOOGLE_GEMINI_BASE_URL='http://localhost:8000/coding-agents-gemini'
export GEMINI_API_KEY="${CONSUMER_API_KEY}"
In headless mode, Gemini CLI reuses a cached authentication method if one exists (Gemini CLI authentication). If you signed in to Gemini CLI with a Google account before, run gemini once in the same shell and select Use Gemini API key.
Call the Gemini API generateContent endpoint through Kong with the AI Consumer key in the x-goog-api-key header:
curl -sS -X POST "http://localhost:8000/coding-agents-gemini/v1beta/models/gemini-2.5-flash:generateContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: ${CONSUMER_API_KEY}" \
-d '{"contents": [{"role": "user", "parts": [{"text": "Reply with exactly: smoke-ok"}]}]}'
Kong returns the Gemini API response unchanged. Your IDs and token counts differ:
{
"candidates": [
{
"content": {
"parts": [
{
"text": "smoke-ok"
}
],
"role": "model"
},
"finishReason": "STOP",
"index": 0
}
],
"usageMetadata": {
"promptTokenCount": 6,
"candidatesTokenCount": 3,
"totalTokenCount": 9
},
"modelVersion": "gemini-2.5-flash",
"responseId": "..."
}
Repeat the request without x-goog-api-key:
curl -sS -o /dev/null -w '%{http_code}\n' -X POST "http://localhost:8000/coding-agents-gemini/v1beta/models/gemini-2.5-flash:generateContent" \
-H "Content-Type: application/json" \
-d '{"contents": [{"role": "user", "parts": [{"text": "hi"}]}]}'
Kong rejects the request before it reaches the Gemini API:
Run a one-shot prompt in the shell where you exported the Gemini CLI variables:
gemini -p "Reply with exactly: gemini-ok" --skip-trust -m gemini-2.5-flash
The flags come from the Gemini CLI reference. -p runs the prompt in non-interactive mode, --skip-trust trusts the current folder for this session so that the folder trust check doesn’t stop the run, and -m selects the model. Gemini CLI prints:
The requests confirm the following:
|
Behavior
|
Evidence
|
|
AI Consumer authentication
|
The request with the AI Consumer key in x-goog-api-key succeeds. Kong matched the key to the coding-agents-consumer AI Consumer.
|
|
Authentication before any upstream call
|
The request without a key gets 401. Kong never contacted the Gemini API.
|
|
Native Gemini API forwarding
|
The response has the Gemini API’s own shape (candidates, usageMetadata), and Gemini CLI works with no change other than its base URL and key.
|
|
The Gemini API key stays in Kong
|
Gemini CLI and the curl requests only held the AI Consumer key. The AI Model Provider added the organization’s key to each upstream request.
|
To review the created entities, and per-consumer usage, open the coding-agents-gemini-cli-recipe AI Gateway in Konnect. For traffic across all your AI Gateways, use the Observability menu.