Route Claude CLI traffic through AI Gateway and OpenAI

Incompatible with
on-prem
Tags
Minimum Version
AI Gateway - 2.0
Previous Versions of this page
TL;DR

Create an AI Provider entity to store your OpenAI API key, create an AI Model entity with an Anthropic-compatible format that routes to OpenAI through that provider, then point Claude CLI’s ANTHROPIC_BASE_URL at your local AI Gateway endpoint so all LLM 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 an OpenAI account.
  2. Get an API key.
  3. Export the API key as a variable:
     export OPENAI_API_KEY='<YOUR_OPENAI_API_KEY>'
     export OPENAI_AUTH_HEADER='Bearer $OPENAI_API_KEY'
  1. Install Claude:
     curl -fsSL https://claude.ai/install.sh | bash
  2. Verify the installation:
     claude --version

Create an AI Provider entity

Create an AI Model Provider entity to define your connection and store your authentication credentials:

kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_model_providers:
  - ref: generic-openai
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: generic-openai
    display_name: "generic-openai"
    type: openai
    config:
      auth:
        type: basic
        headers:
        - name: Authorization
          value: !secret {source: !env OPENAI_AUTH_HEADER}
EOF

The AI Model Provider uses the following settings:

  • type: openai: Specifies that this provider connects to the OpenAI service using OpenAI’s standard API format.
  • name: generic-openai: A unique identifier that AI Models will reference to route requests through this provider.
  • config.auth: Stores your OpenAI API key. header_value: !secret {source: !env OPENAI_AUTH_HEADER} loads the value from your environment at apply time instead of embedding it in the YAML, and kongctl redacts it in plan and diff output. AI Gateway securely manages this credential and injects it into upstream requests automatically, eliminating the need for clients to pass API keys.

Create an AI Model entity

Create an AI Model entity to declare which upstream models are available, configure how client requests are routed, and specify which AI Provider to use:

kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_models:
  - ref: my-claude-openai
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: my-claude-openai
    display_name: "my-claude-openai"
    type: model
    formats:
      - type: anthropic
    config:
      route:
        paths:
          - /
        model:
          body_param: model
          values:
            - my-claude-openai
    targets:
      - name: gpt-5-mini
        provider: generic-openai
        config:
          type: openai
    policies: []
    capabilities:
      - generate
EOF

The AI Model uses the following settings:

  • type: model: Specifies this is a synchronous model for request/response workloads.
  • name: my-claude-openai: A unique identifier for this model.
  • formats: [type: anthropic]: Declares that this model accepts requests in Anthropic-compatible format, matching what Claude Code sends natively, even though the upstream model is OpenAI.
  • config.route.paths: [/]: Configures the custom base path where this model’s Routes will be accessible. Setting this to a unique value avoids clashes when you have multiple AI Models.
  • capabilities: [generate]: Enables the text generation capability. For a model using the anthropic format, the generate capability creates a /messages endpoint matching Anthropic’s native Messages API, so combined with your base path, clients send requests to /v1/messages.
  • targets: Specifies which upstream AI Provider model to route requests to. Here, provider: generic-openai references the AI Provider we created earlier, and name: gpt-5-mini specifies which OpenAI model to call upstream.

Run Claude Code

Now, we can start a Claude Code session that points it to the local AI Gateway endpoint:

export ANTHROPIC_BASE_URL=http://localhost:8000/

CLAUDE_CODE_DISABLE_UNKNOWN_MODEL_WINDOW_ENFORCEMENT=1 \
  claude --allowedTools "WebSearch,Read" --model "my-claude-openai"

And ask a question to confirm that requests reach AI Gateway.

Tell me about the Madrid Skylitzes manuscript.

Claude will produce a full-length response to your request:

The Madrid Skylitzes is a remarkable 12th-century illuminated Byzantine
manuscript that represents one of the most important surviving examples
of medieval historical documentation. Here are the key details:

What it is

The Madrid Skylitzes is the only surviving illustrated manuscript of John
Skylitzes' "Synopsis of Histories" (Σύνοψις Ἱστοριῶν), which chronicles
Byzantine history from 811 to 1057 CE - covering the period from the death
of Emperor Nicephorus I to the deposition of Michael VI.

Artistic Significance

- 574 miniature paintings (with about 100 lost over time)
- Lavishly decorated with gold leaf, vibrant pigments, and intricate
detailing
- Depicts everything from imperial coronations and battles to daily life
in Byzantium
- The only surviving Byzantine illuminated chronicle written in Greek

Unique Collaboration

The manuscript is believed to be the work of 7 different artists from
various backgrounds:
- 4 Italian artists
- 1 English or French artist
- 2 Byzantine artists

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!