Route Claude CLI traffic through AI Gateway and Azure

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

Install Claude Code, create an AI Model Provider for your Azure AI Foundry Claude deployment, add a policy to strip Anthropic-only request fields Azure doesn’t support, create an AI Model that targets it, then point Claude Code’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 \
       -e KONG_NGINX_HTTP_CLIENT_BODY_BUFFER_SIZE=2m

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

This tutorial uses a Claude model deployed on Azure AI Foundry. Azure AI Foundry serves Claude models through a native Anthropic-compatible endpoint, not the Azure OpenAI API, so you need a Foundry resource with a Claude model deployment rather than an Azure OpenAI resource.

  1. Create an Azure AI Foundry resource if you don’t already have one.
  2. In the Azure AI Foundry portal, go to Model catalog, find a Claude model (for example, Claude Sonnet 4.6), and deploy it.
    1. Note the deployment name you choose, you’ll reference it later.
  3. Once deployed, export the following environment variables:

     export AZURE_AI_FOUNDRY_TOKEN='YOUR_AZURE_AI_FOUNDRY_API_KEY'
     export AZURE_AI_FOUNDRY_UPSTREAM_URL='https://YOUR_RESOURCE_NAME.services.ai.azure.com/anthropic'

    AZURE_AI_FOUNDRY_UPSTREAM_URL must end at /anthropic. Do not append /v1/messages. AI Gateway appends the rest of the Anthropic Messages API path automatically.

  1. Install Claude:
     curl -fsSL https://claude.ai/install.sh | bash
  2. Verify the installation:
     claude --version

Create an AI Model 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: azure-claude
    name: azure-claude
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    type: anthropic
    config:
      auth:
        type: basic
        headers:
          - name: x-api-key
            value: !secret {source: !env AZURE_AI_FOUNDRY_TOKEN}
EOF

ai-quickstart references the AI Gateway created by the quickstart script in the prerequisites above, instead of creating a new one.

The AI Model Provider uses the following settings:

  • type: anthropic: Specifies that this provider speaks Anthropic’s native Messages API format. Azure AI Foundry serves Claude models through this same native API, so don’t use type: azure.
  • config.auth.headers[0].value: !secret {source: !env AZURE_AI_FOUNDRY_TOKEN}: Loads the API key from your environment at apply time so it is not embedded in the config, and kongctl redacts it in plan and diff output.

Create AI Policy and AI Model entities

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

Create an AI Policy entity using request transformer to remove extra fields that Azure AI Foundry’s Claude API does not support.

kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_policies:
  - ref: claude-code-compat
    name: claude-code-compat
    display_name: claude-code-compat
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    type: request-transformer-advanced
    enabled: true
    global: false
    config:
      add:
        headers:
          - "anthropic-version:2023-06-01"
      remove:
        headers:
          - anthropic-beta
        querystring:
          - beta
        body:
          - output_config
          - context_management
          - mcp_servers
          - container
          - service_tier
ai_gateway_models:
  - ref: claude-code-azure-sonnet
    display_name: claude-code-azure-sonnet
    name: claude-code-azure-sonnet
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    type: model
    enabled: true
    formats:
      - type: anthropic
    config:
      route:
        paths:
          - /
        model:
          body_param: model
          values:
            - claude-code-azure-sonnet
    capabilities:
       - generate
    policies:
      - !ref claude-code-compat#name
    targets:
      - name: claude-sonnet-4-6
        provider: azure-claude
        config:
          type: anthropic
          upstream_url: !env AZURE_AI_FOUNDRY_UPSTREAM_URL
EOF

The AI Policy uses the following settings:

  • type: request-transformer-advanced: Modifies requests before AI Gateway forwards them upstream.
  • config.add.headers: Adds the anthropic-version header Azure AI Foundry’s native Anthropic endpoint requires. Claude Code doesn’t send this header itself, and Foundry rejects requests without it with a 400.
  • config.remove.headers / config.remove.querystring / config.remove.body: Strips Anthropic-beta-only fields — the anthropic-beta header, beta query string, and body fields like mcp_servers and container — that Claude Code sends but that Azure AI Foundry’s Claude deployment doesn’t support.
  • name: claude-code-compat: The identifier you use to attach the policy.
  • targets.name:: The name of your own Claude deployment in Azure AI Foundry

Claude Code beta features vary by version and may add other incompatible fields over time. If you still see a 400 error mentioning an unexpected field after applying this Policy, add that field to the appropriate remove list and re-apply.

The AI Model uses the following settings:

  • name/display_name: claude-code-azure-sonnet: The identifier you pass to claude --model. Claude Code uses this, not the upstream target name, to select the model.
  • formats: [type: anthropic]: Declares that this model accepts requests in Anthropic-compatible format, matching what Claude Code sends natively.
  • config.route.paths: [/]: Configures the base path where this model’s routes are accessible.
  • capabilities: [generate]: Enables text generation. For a model using the anthropic format, generate creates a /messages endpoint matching Anthropic’s native Messages API, so combined with your base path, clients send requests to /v1/messages.
  • policies: Attaches the claude-code-compat policy created in the previous step, so its header and body transformations apply to every request sent through this model.
  • targets: Specifies which upstream model to route requests to. provider: azure-claude references the AI Provider created earlier, and name: claude-sonnet-4-6 must match the name of your Claude deployment in Azure AI Foundry.
  • targets[0].config.upstream_url: The base Azure AI Foundry endpoint from the prerequisites, ending at /anthropic. AI Gateway appends the rest of the Anthropic Messages API path automatically.

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 "claude-code-azure-sonnet"

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

Tell me about the Madrid Skylitzes manuscript.

Claude Code might prompt you approve its web search for answering the question. When you select Yes, 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!