Qwen Code is Alibaba's terminal coding agent, originally forked from Gemini CLI. It speaks several protocols; with Tokens you use its OpenAI protocol, which sends chat completions to https://tokens.bd/v1.
Coming from Gemini CLI?
Gemini CLI can only talk to Gemini-format APIs, so it can't use Tokens. Qwen Code keeps a similar workflow and works with any OpenAI-compatible endpoint, which makes it the practical substitute.
The Tokens CLI doesn't configure Qwen Code, so set it up by hand. It's one file or three variables.
Install Qwen Code#
npm install -g @qwen-code/qwen-code@latest
# or
brew install qwen-codeCreate a key at /dashboard/keys. API keys covers spend caps and model allow-lists.
Export TOKENS_API_KEY#
export TOKENS_API_KEY="tok_live_your_key"$env:TOKENS_API_KEY = "tok_live_your_key" # this window
setx TOKENS_API_KEY "tok_live_your_key" # new windowsOption A: ~/.qwen/settings.json#
Add Tokens models under modelProviders.openai and tell Qwen Code to use the OpenAI protocol:
{
"modelProviders": {
"openai": [
{
"id": "deepseek/deepseek-v4.1-flash",
"name": "DeepSeek V4.1 Flash (Tokens)",
"baseUrl": "https://tokens.bd/v1",
"description": "via Tokens",
"envKey": "TOKENS_API_KEY"
}
]
},
"security": { "auth": { "selectedType": "openai" } },
"model": { "name": "deepseek/deepseek-v4.1-flash" }
}envKey names the environment variable that holds the key, so the file itself has no secret. Qwen Code also accepts an env block in the same file ("env": { "TOKENS_API_KEY": "tok_live_your_key" }), but that puts the key in plain text. If you use it, keep the file out of any repository.
When the same variable is set in several places, Qwen Code takes the shell export first, then a .env file, then the settings env block.
For context window, custom headers and extra request fields, Qwen Code's Model Providers reference documents generationConfig, customHeaders and extra_body. Set the context window to the real value from the model's page in /models.
Option B: environment variables only#
If you don't want a settings file, these three variables are enough:
export OPENAI_API_KEY="$TOKENS_API_KEY"
export OPENAI_BASE_URL="https://tokens.bd/v1"
export OPENAI_MODEL="deepseek/deepseek-v4.1-flash"$env:OPENAI_API_KEY = $env:TOKENS_API_KEY
$env:OPENAI_BASE_URL = "https://tokens.bd/v1"
$env:OPENAI_MODEL = "deepseek/deepseek-v4.1-flash"QWEN_MODEL works as an alias for OPENAI_MODEL. Be aware that other tools read OPENAI_API_KEY and OPENAI_BASE_URL too, so setting them in your shell profile redirects those tools to Tokens as well. Option A avoids that.
You can also run /auth inside qwen and enter the same values interactively.
Anthropic protocol#
Qwen Code also has an anthropic protocol that reads ANTHROPIC_API_KEY, ANTHROPIC_BASE_URL and ANTHROPIC_MODEL. With Tokens that would be ANTHROPIC_BASE_URL=https://tokens.bd (no /v1). The OpenAI protocol is the simpler path and the one shown above.
Switch models#
Add one object per model to the modelProviders.openai array, each with the exact Tokens id from /models or GET /v1/models and the same baseUrl and envKey:
{
"id": "moonshotai/kimi-k3",
"name": "Kimi K3 (Tokens)",
"baseUrl": "https://tokens.bd/v1",
"envKey": "TOKENS_API_KEY"
}The id above is an example; check the exact one in /models. Then run /model inside qwen to switch, or change model.name in the settings file. With environment variables, change OPENAI_MODEL. Choosing a model has guidance for agent work.
Verify it works#
Start qwen in a project folder, run /model to confirm the Tokens model is selected, then send a short prompt. The request should appear in Usage analytics on the dashboard.
To rule out Qwen Code itself, call the endpoint directly:
curl https://tokens.bd/v1/chat/completions \
-H "Authorization: Bearer $TOKENS_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "deepseek/deepseek-v4.1-flash", "messages": [{"role": "user", "content": "Reply with OK"}], "max_tokens": 10}'Troubleshooting#
401 invalid_api_key with a key you just changed. An older value wins somewhere: a shell export beats .env, which beats the settings env block. Check echo $TOKENS_API_KEY (or $env:TOKENS_API_KEY).
401 missing_api_key. The variable named in envKey isn't set in the shell that started qwen. After setx on Windows, open a new terminal.
Requests don't reach Tokens. security.auth.selectedType must be openai, or Qwen Code uses whichever auth method was selected before. Set it in the file, or pick the OpenAI option in /auth.
404 on every request. baseUrl (or OPENAI_BASE_URL) must end in /v1.
404 model_not_found or 403 model_not_allowed_on_key. The id is misspelled, or the key's allow-list excludes the model. Allow-lists can't be edited after creation, so create a new key if needed.
For 402, 429 and 5xx errors, see Troubleshooting.