Route Qwen Code CLI traffic through AI Gateway and DashScope

TL;DR

Create an AI Model Provider for Alibaba Cloud DashScope and an AI Model with the generate capability that targets it, then point Qwen Code CLI’s OPENAI_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

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"

Install Node.js 18+ (verify with node --version), then install the Qwen Code CLI:

npm install -g @qwen-code/qwen-code

Create an AI Model Provider and AI Model

DashScope serves the Qwen model family through an OpenAI-compatible Chat Completions API, so Qwen Code CLI 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
    display_name: "Alibaba Cloud DashScope"
    type: dashscope
    config:
      auth:
        type: basic
        headers:
          - name: Authorization
            value: !secret {source: !env DASHSCOPE_AUTH_HEADER}
ai_gateway_models:
  - ref: my-qwen-dashscope
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: my-qwen-dashscope
    display_name: "my-qwen-dashscope"
    type: model
    formats: [{ type: openai }]
    config:
      route:
        paths: [/qwen-dashscope]
        model:
          body_param: model
          values: [my-qwen-dashscope]
    targets:
      - name: qwen-plus
        provider: dashscope
        config:
          type: dashscope
          international: true
    capabilities: [generate]
EOF

This example uses the following settings:

  • targets: Sends requests to qwen-plus through the dashscope provider.
  • targets[0].config.international: true: Uses DashScope’s international endpoint (dashscope-intl.aliyuncs.com); set it to false if your DashScope key belongs to a mainland China account.
  • capabilities: [generate]: Exposes the model at a /qwen-dashscope/chat/completions endpoint.

Run Qwen Code CLI

Run Qwen Code CLI against the model configured in the AI Model entity’s targets:

export OPENAI_API_KEY=sk-placeholder
export OPENAI_BASE_URL=http://localhost:8000/qwen-dashscope/chat/completions

qwen --model "my-qwen-dashscope" --auth-type "openai"

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

Explain the singleton pattern in Python.

Qwen Code CLI returns a response, proxied through AI Gateway to the upstream DashScope Qwen model.

The Qwen Code CLI requires OPENAI_API_KEY to be set even though the real key lives on the AI Gateway, so setting a placeholder is fine.

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!