Use AI Hub with Qwen Code
Qwen Code is an open-source coding agent for the terminal. It runs on AI Hub through its OpenAI-compatible protocol: set the base URL and your API key, and pick any catalogue model.
1. Install
Qwen Code needs Node.js 22 or later.
npm install -g @qwen-code/qwen-code@latest2. Point it at AI Hub
The quickest setup is three environment variables. OPENAI_MODEL takes a catalogue ID in author/slug form.
export OPENAI_API_KEY="sk-tokenhub-…" export OPENAI_BASE_URL="https://tokenhub-api.belive.sg/api/v1" export OPENAI_MODEL="qwen/qwen3-coder-flash" qwen
To switch between several AI Hub models with /model, declare them in ~/.qwen/settings.json. The key stays in the environment variable named by envKey; Qwen Code never writes it to the file.
{ "modelProviders": { "openai": [ { "id": "qwen/qwen3-coder-flash", "name": "Qwen3 Coder Flash (AI Hub)", "baseUrl": "https://tokenhub-api.belive.sg/api/v1", "envKey": "TOKENHUB_API_KEY" }, { "id": "deepseek/deepseek-v4-pro", "name": "DeepSeek V4 Pro (AI Hub)", "baseUrl": "https://tokenhub-api.belive.sg/api/v1", "envKey": "TOKENHUB_API_KEY" }, { "id": "moonshotai/kimi-k3", "name": "Kimi K3 (AI Hub)", "baseUrl": "https://tokenhub-api.belive.sg/api/v1", "envKey": "TOKENHUB_API_KEY" } ] }, "security": { "auth": { "selectedType": "openai" } }, "model": { "name": "qwen/qwen3-coder-flash" } }
Use the openai protocol for the Qwen Code recipe here. See Messages and Responses for AI Hub's other protocols.
3. Run it
Run qwen in a project directory for the interactive agent, or pass a prompt for a one-shot run in a script or CI job:
qwen -o json "Fix the failing test in test_prices.py and run the tests"Every turn is an ordinary streamed chat completion, so each one appears in your Activity log with its model, tokens and cost.
Choose a model
Choose an available model from Models whose supported_parameters includes tools. Use its exact ID in the client settings and check its context limit before opening a large project.
Cost
- Every turn resends Qwen Code's system prompt, tool definitions and conversation history. Input usage grows with the project context and the number of turns.
- Most of that input repeats the previous turn. Models whose upstream reports cached input show it in
usage.prompt_tokens_details.cached_tokens. - Give the agent its own key with a monthly spend cap, so a runaway session stops at the cap. See Rate limits & caps.