Route Claude CLI traffic through AI Gateway and DashScope

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

Create an AI Model Provider for Alibaba Cloud DashScope and an AI Model with the anthropic format that targets it, then point Claude Code’s ANTHROPIC_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 \
       -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

Get an API key from the Alibaba Cloud DashScope console and export it as the full Authorization header value (including the Bearer prefix):

export DASHSCOPE_AUTH_HEADER="Bearer YOUR_DASHSCOPE_KEY"
  1. Install Claude:
     curl -fsSL https://claude.ai/install.sh | bash
  2. Verify the installation:
     claude --version

Create the AI Model Provider and AI Model entities

DashScope serves the Qwen model family through a native Anthropic-compatible Messages API, so Claude Code can talk to it natively.

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

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.

kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_model_providers:
  - ref: dashscope
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: dashscope
    type: dashscope
    display_name: "Alibaba Cloud DashScope"
    config:
      auth:
        type: basic
        headers:
          - name: Authorization
            value: !secret {source: !env DASHSCOPE_AUTH_HEADER}
ai_gateway_models:
  - ref: claude-code-qwen
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: claude-code-qwen
    display_name: "Claude Code - DashScope Qwen"
    type: model
    enabled: true
    formats:
      - type: anthropic
    config:
      route:
        paths:
          - /
        model:
          body_param: model
          values:
            - qwen-plus
    capabilities:
      - generate
    targets:
      - name: qwen-plus
        provider: dashscope
        config:
          type: dashscope
          international: true
          max_tokens: 8192
          temperature: 1.0
EOF

This example uses the following settings:

  • type: dashscope: Connects to the Alibaba Cloud DashScope API as an AI Model Provider.
  • capabilities: [generate]: For a model using the anthropic format, generate creates a /v1/messages endpoint matching Anthropic’s native Messages API.
  • formats: [{ type: anthropic }]: Accepts Anthropic-format requests to the AI Model entity, matching what Claude Code sends.
  • config.route.model: { body_param: model, values: [qwen-plus] }: The model name Claude Code should send in each request, which you can set with the ANTHROPIC_MODEL variable or --model option.
  • 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.
  • targets[0].config.international: true: Uses DashScope’s international endpoint (dashscope-intl.aliyuncs.com). This is the default. If your DashScope key belongs to a mainland China account, set this to false so requests reach dashscope.aliyuncs.com instead.

Verify traffic through AI Gateway

Before starting Claude Code, confirm the route works by sending an Anthropic Messages API request directly:

curl -X POST "$KONNECT_PROXY_URL/v1/messages" \
     --no-progress-meter --fail-with-body  \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "Authorization: $DASHSCOPE_AUTH_HEADER" \
     --json '{
       "model": "qwen-plus",
       "max_tokens": 16,
       "messages": [
         {
           "role": "user",
           "content": "Reply with just: ok"
         }
       ]
     }'

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 "qwen-plus"

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!