Route Qwen Code CLI traffic through AI Gateway and OpenAI

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

Create an AI Model Provider for OpenAI and an AI Model with the generate capability, 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
  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'

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

Qwen Code CLI speaks OpenAI’s Chat Completions format 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: 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}
ai_gateway_models:
  - ref: my-qwen-openai
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: my-qwen-openai
    display_name: "my-qwen-openai"
    type: model
    formats: [{ type: openai }]
    config:
      route:
        paths: [/qwen]
        model:
          body_param: model
          values: [my-qwen-openai]
    targets:
      - name: gpt-5-mini
        provider: generic-openai
        config:
          type: openai
    capabilities: [generate]
EOF

This example uses the following settings:

  • targets: Sends requests to gpt-5-mini through the generic-openai provider.
  • capabilities: [generate]: Exposes the model at a /qwen/chat/completions endpoint.

Run Qwen Code CLI

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

export OPENAI_BASE_URL=http://localhost:8000/qwen/chat/completions

qwen --model "my-qwen-openai" --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 OpenAI model.

The Qwen Code CLI requires OPENAI_API_KEY to be set even though the real key lives on the AI Gateway.

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!