AI Tool Integration: Gemini CLI

Minimum Version
AI Gateway - 2.2
Incompatible with
on-prem

Overview

Put Kong AI Gateway between Gemini CLI and the Gemini API, so that developers use AI Consumer keys that you can revoke and the organization’s Gemini API key never leaves Kong. You’ll set up:

  • A /coding-agents-gemini endpoint that forwards Gemini CLI’s native Gemini API requests unchanged
  • An AI Consumer key for Gemini CLI instead of a Google AI Studio key on each developer machine
  • Authentication that rejects requests without a valid AI Consumer key before any call to Google

This cookbook covers Gemini CLI with Gemini API key authentication, the mode that GOOGLE_GEMINI_BASE_URL applies to.

Prerequisites

This tutorial uses Kong Konnect. The AI Gateway quickstart script provisions a recipe-scoped AI Gateway and local data plane. The data plane must run AI Gateway 2.2 or later, because the AI Model uses the passthrough format.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
  2. Export your token. You reuse the same token later for kongctl commands:

    export KONNECT_TOKEN='YOUR_KONNECT_PAT'
  3. Set the recipe-scoped AI Gateway name and run the quickstart script:

    export KONNECT_CONTROL_PLANE_NAME='coding-agents-gemini-cli-recipe'
    curl -Ls https://get.konghq.com/ai | bash -s -- -k $KONNECT_TOKEN

    This provisions an AI Gateway named coding-agents-gemini-cli-recipe, a local data plane connected to it, and prints export lines for the rest of the session vars, including AI_GATEWAY_ID. Paste those into your shell when prompted.

This tutorial uses kongctl to manage AI Gateway configuration.

  1. Install kongctl from the kongctl documentation.
  2. Verify it’s installed:

    kongctl version

The AI Model Provider uses your organization’s Google AI Studio API key.

  1. Get a Gemini API key in Google AI Studio.
  2. Export the API key:

    export GEMINI_TOKEN='YOUR-GEMINI-API-KEY'

This tutorial uses Gemini CLI.

  1. Install Gemini CLI from the installation guide.
  2. Verify that it’s installed:

    gemini --version

What this cookbook solves

The Gemini CLI authentication guide has each developer export a Google AI Studio key as GEMINI_API_KEY, and most teams share one organization key this way, which causes the following problems:

Problem

Solution

Each developer machine holds a key that bills the organization’s account. A leaked key works anywhere, and removing one developer means rotating the key for everyone. The AI Model Provider keeps the Gemini API key in Kong. Each developer gets an AI Consumer key, and deleting that credential removes only that developer’s access.
Google’s API key guidance says to treat the key like a password and to call the API from a backend proxy instead of exposing the key in client applications. Kong is that backend proxy. Gemini CLI sends only the AI Consumer key, and the AI Model Provider adds the organization’s key to each upstream request.
When several developers share one key, usage arrives as one total, with no way to see who used which model. The Key Auth AI Auth Strategy maps each key to an AI Consumer, so Konnect analytics show usage for each AI Consumer.
Gemini CLI calls Gemini’s native API, not the OpenAI format, so a gateway that translates formats can’t sit in front of it. The AI Model uses the passthrough format and forwards Gemini CLI’s requests and the Gemini API’s responses unchanged.

Try it out

Apply the configuration, connect Gemini CLI to Kong, then send requests with and without the AI Consumer key and a prompt from Gemini CLI.

Apply the Kong configuration

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

Connect Gemini CLI to Kong

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.

Send a request through the AI Model

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:

401

Send a prompt from Gemini CLI

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:

gemini-ok

Check the results

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.

How it works

Gemini CLI sends native Gemini API requests to GOOGLE_GEMINI_BASE_URL, with the AI Consumer key in x-goog-api-key. Kong authenticates the AI Consumer, removes the prefix /coding-agents-gemini from the path, and forwards the request unchanged to the Gemini API with the organization’s key.

sequenceDiagram
    participant G as Gemini CLI
    participant K as Kong AI Gateway
    participant API as Gemini API

    G->>K: POST /coding-agents-gemini/v1beta/models/MODEL:streamGenerateContent (x-goog-api-key AI Consumer key)
    activate K
    K->>K: Key Auth AI Auth Strategy validates x-goog-api-key (else 401)
    K->>K: AI Model Provider sets x-goog-api-key to the organization's key
    K->>API: POST /v1beta/models/MODEL:streamGenerateContent (unchanged body)
    activate API
    API-->>K: Gemini API response
    deactivate API
    K-->>G: Unchanged response
    deactivate K

Key Auth AI Auth Strategy and AI Consumer

Authentication is an AI Auth Strategy, not an AI Policy. The Gemini API reads API keys from the x-goog-api-key header (Gemini API key documentation), so the Key Auth AI Auth Strategy reads the AI Consumer key from the same header. No header rewrite is needed. On a match, Kong attaches the AI Consumer to the request. The AI Consumer is the identity that rate limits, ACLs, and analytics use.

The following settings configure the Key Auth AI Auth Strategy and the AI Consumer:

Setting

Purpose

key_names: [x-goog-api-key] Kong reads the AI Consumer key from the x-goog-api-key header.
hide_credentials: true Kong removes the AI Consumer key before it forwards the request. The AI Model Provider then sets x-goog-api-key to the organization’s key.
credentials[].api_key The AI Consumer’s key. The field is write-only, so it takes a !secret reference to CONSUMER_API_KEY.

Kong adds X-Consumer-Username and related headers to the upstream request, not to the response. Per-consumer usage appears in Konnect analytics.

AI Model Provider and AI Model

The AI Model Provider holds the organization’s Gemini API key. The AI Model uses the passthrough format, which forwards request and response bodies byte for byte, without format translation or schema validation. Passthrough keeps AI Model Provider authentication, AI Consumer identity, and logging. Kong forwards every path under /coding-agents-gemini, so an AI Consumer key can call each Gemini API endpoint that the organization’s key can call, not only content generation.

The following settings configure the AI Model Provider and the AI Model:

Setting

Purpose

config.auth (AI Model Provider) The x-goog-api-key header with the organization’s key, from GEMINI_TOKEN. Kong adds it to each upstream request.
formats: [{type: passthrough}] Forwards bodies without translation. Passthrough can’t be combined with other formats and needs AI Gateway 2.2 or later.
capabilities: [generate] The AI Model serves content generation.
access.auth_strategies Each request must have a valid AI Consumer key.
targets[].config.type: gemini Selects Gemini’s authentication behavior. With a gemini target, Kong also reads token counts from passthrough responses, for analytics and token-based rate limits.
targets[].config.upstream_url The Gemini API host, with the path /. Kong forwards the client’s request path without the AI Model’s base path, so the API version in the path comes from Gemini CLI.
config.route.paths and strip_path: true Kong matches requests under /coding-agents-gemini and removes the prefix before it forwards the request.
config.route.methods The AI Model accepts POST, GET, and OPTIONS. Content generation requests use POST.
config.route.protocols: [http, https] Accepts HTTP, so Gemini CLI can call the local data plane at http://localhost:8000.
config.max_request_body_size: 16777216 Accepts request bodies up to 16 MB, instead of the 8 MB default. Gemini CLI requests carry the conversation, tool declarations, and file contents.
config.response_streaming: allow Kong passes streamed responses to Gemini CLI.
config.logging.payloads: true Kong logs request and response payloads. Gemini CLI requests contain source code, so set it to false if you only need usage metrics.

Production credentials: This cookbook stores the AI Consumer’s API key and the Gemini API key in environment variables for simplicity. In production, use AI Vaults to reference them from a secret manager, for example AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager, or Azure Key Vault.

Variations and next steps

You can change or extend this configuration in the following ways:

Variation

How

Issue one AI Consumer for each developer Add an AI Consumer and a credential for each developer, so that usage and revocation are per person. Distribute each key through your secrets tooling.
Restrict the request methods Set config.route.methods to [POST] and re-apply, so that the AI Model doesn’t forward GET requests to the Gemini API. Test Gemini CLI after the change, because Kong then rejects each request with another method.
Add per-consumer token budgets Attach the AI Rate Limiting Advanced Policy to give each AI Consumer a token budget. See Passthrough format for the AI Policies that work with passthrough.
Manage Gemini CLI settings centrally The Gemini CLI enterprise guide describes a system settings file that overrides user and project settings, and a wrapper script that enforces it.
Connect other tools The Claude Code and Codex cookbooks use passthrough AI Models for Anthropic and OpenAI. For OpenAI-format tools, see the Cursor cookbook.

Cleanup

The recipe’s kongctl namespace scoped all resources, so this teardown removes only this recipe’s configuration.

Remove the Gemini CLI variables from your shell, so that Gemini CLI calls the Gemini API directly again:

unset GOOGLE_GEMINI_BASE_URL GEMINI_API_KEY

Tear down the local data plane and delete the AI Gateway from Konnect:

export KONNECT_CONTROL_PLANE_NAME='coding-agents-gemini-cli-recipe' && curl -Ls https://get.konghq.com/ai | bash -s -- -d -k $KONNECT_TOKEN

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!