Route Google Gemini CLI traffic through AI Gateway

Incompatible with
on-prem
Tags
Minimum Version
AI Gateway - 2.0
TL;DR

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.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

    export KONNECT_TOKEN='YOUR_KONNECT_PAT'
  3. 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).

  1. Install kongctl from developer.konghq.com/kongctl.
  2. Verify the installation:

    kongctl version
  1. Create a Gemini API key in Google AI Studio.
  2. Export the API key as a variable:
    export 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-cli

Verify the installation:

gemini --version

Create an AI Model Provider and AI Model

Gemini 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
EOF

Run Gemini CLI

Make 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.

Cleanup

To clean up all AI Gateway resources created in this guide, run:

curl -Ls https://get.konghq.com/ai | bash -s -- -d

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!