Route Claude CLI traffic through Kong AI Gateway and Anthropic

Deployment Platform
Minimum Version
Kong Gateway - 3.13
TL;DR

Install Claude CLI, configure its API key helper, create a Gateway Service and Route, attach the AI Proxy plugin to forward requests to Claude, enable file-log to inspect traffic, and point Claude CLI to the local proxy endpoint so all LLM requests pass through the AI 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 quickstart script to automatically provision a Control Plane and Data Plane, and configure your environment:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -k $KONNECT_TOKEN --deck-output
    

    This sets up a Konnect Control Plane named quickstart, provisions a local Data Plane, and prints out the following environment variable exports:

     export DECK_KONNECT_TOKEN=$KONNECT_TOKEN
     export DECK_KONNECT_CONTROL_PLANE_NAME=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 requires Kong Gateway Enterprise. If you don’t have Kong Gateway set up yet, you can use the quickstart script with an enterprise license to get an instance of Kong Gateway running almost instantly.

  1. Export your license to an environment variable:

     export KONG_LICENSE_DATA='LICENSE-CONTENTS-GO-HERE'
    
  2. Run the quickstart script:

    curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA 
    

    Once Kong Gateway is ready, you will see the following message:

     Kong Gateway Ready
    

decK is a CLI tool for managing Kong Gateway declaratively with state files. To complete this tutorial, install decK version 1.43 or later.

This guide uses deck gateway apply, which directly applies entity configuration to your Gateway instance. We recommend upgrading your decK installation to take advantage of this tool.

You can check your current decK version with deck version.

For this tutorial, you’ll need Kong Gateway entities, like Gateway Services and Routes, pre-configured. These entities are essential for Kong Gateway to function but installing them isn’t the focus of this guide. Follow these steps to pre-configure them:

  1. Run the following command:

    echo '
    _format_version: "3.0"
    services:
      - name: example-service
        url: http://httpbin.konghq.com/anything
    routes:
      - name: example-route
        paths:
        - "/anything"
        service:
          name: example-service
    ' | deck gateway apply -
    

To learn more about entities, you can read our entities documentation.

For this task, you need an Anthropic API key.

  1. Create an Anthropic Console account.
  2. Generate an API key from the console settings.
  3. Create a decK variable with your API key:
    export DECK_ANTHROPIC_API_KEY='ANTHROPIC API KEY'
    
  1. Install Claude:

     curl -fsSL https://claude.ai/install.sh | bash
    
  2. Create or edit the Claude settings file:

     mkdir -p ~/.claude
     nano ~/.claude/settings.json
    

    Put this exact content in the file:

     {
         "apiKeyHelper": "~/.claude/anthropic_key.sh"
     }
    
  3. Create the API key helper script:

     nano ~/.claude/anthropic_key.sh
    

    Inside, put a dummy API key:

     echo "x"
    
  4. Make the script executable:

     chmod +x ~/.claude/anthropic_key.sh
    
  5. Verify it works by running the script:

     ~/.claude/anthropic_key.sh
    

    You should see only your API key printed.

Configure the AI Proxy plugin

First, configure the AI Proxy plugin for the Anthropic provider.

  • This setup uses the default llm/v1/chat route. Claude Code sends its requests to this route.
  • The configuration also raises the maximum request body size to 512 KB to support larger prompts.

Set llm_format: anthropic to tell Kong AI Gateway that requests and responses use Claude’s native API format. This parameter controls schema validation and prevents format mismatches between Claude Code and the gateway.

echo '
_format_version: "3.0"
plugins:
  - name: ai-proxy
    config:
      logging:
        log_statistics: true
      auth:
        header_name: x-api-key
        header_value: "${{ env "DECK_ANTHROPIC_API_KEY" }}"
      model:
        name: claude-sonnet-4-5-20250929
        provider: anthropic
        options:
          anthropic_version: '2023-06-01'
      llm_format: anthropic
      max_request_body_size: 524288
      route_type: llm/v1/chat
' | deck gateway apply -

Configure the File Log plugin

Now, let’s enable the File Log plugin on the Service, to inspect the LLM traffic between Claude and the AI Gateway. This creates a local claude.json file on your machine. The file records each request and response so you can review what Claude sends through the AI Gateway.

echo '
_format_version: "3.0"
plugins:
  - name: file-log
    config:
      path: "/tmp/claude.json"
' | deck gateway apply -

Verify traffic through Kong

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

ANTHROPIC_BASE_URL=http://localhost:8000/anything \
ANTHROPIC_MODEL=claude-sonnet-4-5-20250929 \
claude

Claude Code asks for permission before it runs tools or interacts with files:

I'll need permission to work with your files.

This means I can:
- Read any file in this folder
- Create, edit, or delete files
- Run commands (like npm, git, tests, ls, rm)
- Use tools defined in .mcp.json

Learn more ( https://docs.claude.com/s/claude-code-security )

❯ 1. Yes, continue
2. No, exit

Select Yes, continue. The session starts. Ask a simple question to confirm that requests reach Kong AI Gateway.

Tell me about 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

Next, inspect the Kong AI Gateway logs to verify that the traffic was proxied through it:

docker exec kong-quickstart-gateway cat /tmp/claude.json | jq

You should find an entry that shows the upstream request made by Claude Code. A typical log record looks like this:

{
  "...": "...",
  "headers": {
    ...
    "user-agent": "claude-cli/2.0.37 (external, cli)",
    "content-type": "application/json",
    ...
  },
  "method": "POST",
  ...
   "ai": {
    "proxy": {
      "usage": {
        "prompt_tokens": 1,
        "completion_tokens_details": {},
        "completion_tokens": 85,
        "total_tokens": 86,
        "cost": 0,
        "time_per_token": 38.941176470588,
        "time_to_first_token": 2583,
        "prompt_tokens_details": {}
      },
      "meta": {
        "request_model": "claude-sonnet-4-20250514",
        "response_model": "claude-sonnet-4-20250514",
        "llm_latency": 3310,
        "plugin_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "request_mode": "stream",
        "provider_name": "anthropic"
      }
    }
  },
  ...
}

This output confirms that Claude Code routed the request through Kong AI Gateway using the claude-sonnet-4-5-20250929 model we selected while starting the Claude Code session.

Cleanup

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d
Something wrong?

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!