export GOOGLE_GEMINI_BASE_URL=http://localhost:8000/gemini
gemini --model "my-gemini-model"And ask a question to confirm that requests reach AI Gateway.
Tell me about the prisoner's dilemma.Create an AI Model Provider entity to store your Gemini API key and an AI Model entity that routes to it, then point Gemini CLI’s GOOGLE_GEMINI_BASE_URL at your local AI Gateway endpoint so all requests pass through the gateway for monitoring and control.
This is a Konnect tutorial and requires a Konnect personal access token.
Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
Export your token to an environment variable:
export KONNECT_TOKEN='YOUR_KONNECT_PAT'Run the AI Gateway quickstart script to automatically provision a control plane and data plane in Kong Konnect, and configure your environment:
curl -Ls https://get.konghq.com/ai | bash -s -- -k $KONNECT_TOKEN This sets up a AI Gateway control plane named ai-quickstart, provisions a local data plane, and prints out the following environment variables export:
export AI_GATEWAY_ID=your-gateway-id
export KONNECT_TOKEN=$KONNECT_TOKEN
export KONNECT_CONTROL_PLANE_NAME=ai-quickstart
export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
export KONNECT_PROXY_URL='http://localhost:8000'Copy and paste these into your terminal to configure your session.
This tutorial uses kongctl to manage Konnect resources programmatically. We recommend keeping kongctl up to date with the latest version (1.13.0).
Verify the installation:
kongctl versionexport GEMINI_API_KEY='YOUR_GEMINI_API_KEY'This tutorial uses the Google Gemini CLI. Install Node.js 18+ if needed (verify with node --version), then install the Gemini CLI:
npm install -g @google/gemini-cliVerify the installation:
gemini --versionGemini CLI expects to talk to Google’s Gemini API directly, authenticating with an API key. Distributing that key to every developer machine running the CLI exposes credentials and makes rotation difficult. Routing Gemini CLI through AI Gateway instead removes this requirement: developers authenticate against the gateway, and AI Gateway injects the real credential upstream.
Create an AI Model Provider that stores your Gemini API key as the x-goog-api-key header, so AI Gateway injects it into every upstream request and Gemini CLI never handles the real key. Then create an AI Model using Gemini’s native format, exposed at /gemini and routed to gemini-3.5-flash through that provider: the generate capability serves Gemini’s generateContent and streamGenerateContent endpoints, and config.route.model lets Gemini CLI request the model by the alias my-gemini-model instead of the real upstream name. Create both entities in a single kongctl apply command so the model can reference the provider:
kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_model_providers:
- ref: my-gemini-account
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: my-gemini-account
display_name: "my-gemini-account"
type: gemini
config:
auth:
type: basic
headers:
- name: x-goog-api-key
value: !secret {source: !env GEMINI_API_KEY}
ai_gateway_models:
- ref: my-gemini-model
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: my-gemini-model
display_name: "my-gemini-model"
type: model
enabled: true
formats: [{ type: gemini }]
config:
route:
paths: [/gemini]
model:
path_param: model_name
values: [my-gemini-model]
max_request_body_size: 4194304
capabilities: [generate]
targets:
- name: gemini-3.5-flash
provider: !ref my-gemini-account#name
config:
type: gemini
EOFMake sure you authenticate using a Gemini API key
Point GOOGLE_GEMINI_BASE_URL at the local proxy endpoint where LLM traffic from Gemini CLI routes and start a Gemini CLI session:
export GOOGLE_GEMINI_BASE_URL=http://localhost:8000/gemini
gemini --model "my-gemini-model"And ask a question to confirm that requests reach AI Gateway.
Tell me about the prisoner's dilemma.Expected output shows the model’s response to your prompt, proxied through AI Gateway to the Gemini model configured in the AI Model entity’s targets.
To clean up all AI Gateway resources created in this guide, run:
curl -Ls https://get.konghq.com/ai | bash -s -- -d