Tools & integrationsGuide
Connect Qwen Code to CheapTokens: env vars and settings.json
Point Qwen Code at an OpenAI-compatible API with three env vars or one settings.json entry: base URL, full model id and a key kept out of git.

On this page
Qwen Code can connect to a custom OpenAI-compatible endpoint, so connecting it to CheapTokens takes three values: the base URL https:/, your API key and a full model id such as deepseek-. Export them as OPENAI_API_KEY, OPENAI_BASE_URL and OPENAI_MODEL, or put them into ~/. Both routes come from the Qwen Code authentication docs (page last updated 22 October 2026).
Picture a long session: the agent reads a dozen files, edits three, reruns the tests. Every step resends the context, so you pay for input again and again. That is why a cheap model on a per-token balance suits a terminal agent, and why the setup below is worth ten minutes.
Install Qwen Code#
Qwen Code needs Node.js 22 or newer. The project README (github.com/QwenLM/qwen-code) lists several install routes; two of them:
Install Qwen Code
npm
npm install -g @qwen-code/qwen-code@latestHomebrew
brew install qwen-codeYou start it with qwen. The docs also describe /auth → Custom Provider for connecting a proxy or an unsupported provider by hand. The two routes below are written out line by line, so begin with one of them.
Route 1: three environment variables#
The quickest way. Open a terminal in your project and run:
export OPENAI_API_KEY="sk-your-api-key"
export OPENAI_BASE_URL="https://api.cheaptokens.dev/v1"
export OPENAI_MODEL="deepseek-ai/DeepSeek-V4-Flash-0731"
qwenThe key is a placeholder; paste your own from the CheapTokens dashboard. Exporting OPENAI_API_KEY is also what tells Qwen Code to use OpenAI-compatible auth when you set everything through env vars. OPENAI_MODEL has an alias, QWEN_MODEL.
One detail trips people up: the model id. Use the full id from the CheapTokens docs. The short name you see next to prices, deepseek-, is not accepted by the API, and ids differ between providers.
Which variable does what? Match them:
Match the pairs
Match each variable to its job
Tap an item on the left, then its pair
- OPENAI_API_KEYYour CheapTokens key; exporting it selects OpenAI-compatible auth
- OPENAI_BASE_URLThe endpoint Qwen Code sends requests to
- OPENAI_MODELThe model id sent to the API
- QWEN_MODELAn alias for OPENAI_MODEL
Exports last until you close the terminal. To keep the setup, use the next route.
Route 2: settings.json and a key outside git#
Say you share the repo with a team and nobody wants a key in a config file. Then describe the model in ~/ and keep the key in a separate file.
{
"modelProviders": {
"openai": [
{
"id": "deepseek-ai/DeepSeek-V4-Flash-0731",
"name": "DeepSeek-V4-Flash (CheapTokens)",
"baseUrl": "https://api.cheaptokens.dev/v1",
"description": "DeepSeek-V4-Flash through CheapTokens",
"envKey": "CHEAPTOKENS_API_KEY"
}
]
},
"security": { "auth": { "selectedType": "openai" } },
"model": { "name": "deepseek-ai/DeepSeek-V4-Flash-0731" }
}Four fields carry the weight:
idis the model id sent to the API, so it must be the full id.model.namehas to match one of theidvalues, otherwise Qwen Code has nothing to select.security.set toauth. selectedType "openai"picks the OpenAI-compatible route.envKeynames the variable that holds the key. It is optional and defaults toOPENAI_API_KEY.
The key itself goes into .qwen/.env, which the docs recommend for secrets, and that file stays out of git:
CHEAPTOKENS_API_KEY=sk-your-api-keyThere is a reason to prefer that over the env block in settings.json: values from env have the lowest priority, and a file with a key in it is easy to commit by accident. More on keeping keys safe is in store an API key safely.

After launch, /model shows the configured models and lets you switch. To start with a specific one, run qwen -.
What a coding session costs#
Terminal agents are input-heavy: each step carries the files and history it has gathered so far. Take a 40-step task and assume 30,000 input tokens and 1,000 output tokens per step. That is 40 × 30,000 = 1,200,000 input tokens and 40 × 1,000 = 40,000 output tokens. Here is the whole task on live prices:
| Model | Input $/1M | Output $/1M | Per request | 40 requests | OpenRouter, 40 requests |
|---|---|---|---|---|---|
| GLM-5.3-Flash | $0.02 | $0.04 | $0.00064 | $0.0256 | $0.0526 |
| DeepSeek-V4-Flash | $0.015 | $0.10 | $0.00055 | $0.022 | $0.0633 |
| MiniMax-M2.7 | $0.05 | $0.15 | $0.0017 | $0.066 | $0.408 |
30,000 input and 1,000 output tokens per request. Prices per 1M tokens are current and change together with our tariffs; the OpenRouter column shows reference prices for the same workload.
Counting input and output together, CheapTokens is cheaper than OpenRouter for each model in the table. For this kind of loop, DeepSeek-V4-Flash fits well: a 380K context, up to 32,768 output tokens in one answer, one price at any hour and one balance for every model. Your own numbers will differ, so move the sliders:
Cost calculator
Your own day with a terminal agent
Input tokens per request: 25,000 · Output tokens per request: 800 · Requests per day: 120
| Model | Per day | Per month |
|---|---|---|
| GLM-5.3-Flash$0.02$0.04 | $0.0638 | $1.92 |
| DeepSeek-V4-Flash$0.015$0.10 | $0.0546 | $1.64 |
| MiniMax-M2.7$0.05$0.15 | $0.164 | $4.93 |
Prices are on the pricing page. If you watch usage in another agent, how much Cline costs walks through the same arithmetic.
Other models to try#
Switching is a change of id, nothing else. Take it from the models page or from GET /v1/models:
zai-: 390K context, up to 8,192 output tokens per answer, and a lower total price than OpenRouter.org/ GLM- 5. 3- Flash MiniMaxAI/: 180K context, up to 8,192 output tokens per answer, and a lower total price than OpenRouter.MiniMax- M2. 7
Change OPENAI_MODEL, or add a second entry to modelProviders. and flip between them with /model.
If Qwen Code does not connect#
- Auth type is not picked. With env vars only,
OPENAI_API_KEYmust be exported. With settings.json, check thatselectedTypeis"openai". - Model not found.
model.namemust equal anidcharacter for character, and the id must be the full gateway id. - 401. The key is missing or wrong. If you use
envKey, make sure that variable really exists in.qwen/.envor your shell. - 404. Check that the base URL ends with
/v1and that the id has no typo. The full walkthrough is in API 401 or 404: wrong key, base URL or model id. - A variable seems ignored. Values in
envinside settings.json have the lowest priority, so a variable set elsewhere wins.
The setup for other terminal agents follows the same pattern, for example OpenCode and Aider. Everything else, from keys to the first request, is in the CheapTokens docs.



