Qwen Code Custom API Setup

Add a TokenHub OpenAI-compatible model to Qwen Code settings and choose it safely from the CLI.

What is Qwen Code?

Qwen Code is a command-line coding agent with configurable providers, models, skills, and repository workflows.

This Qwen Code guide uses the current modelProviders.openai configuration with an environment-based TokenHub API key. It keeps credentials out of shared settings and starts with a read-only task. The persistent setup lives in the user settings file under modelProviders.openai, while the real API key remains in the environment referenced by envKey. Keep shared project configuration free of secrets, confirm security.auth.selectedType and the default model, and restart the CLI after changing JSON or environment variables.

Qwen Code can route model requests through TokenHub's OpenAI compatible API gateway. Keep the official installation flow, then replace only the API key, Base URL, and model ID.

The TokenHub endpoint used by this guide is https://us-api.tokenhub.com/v1/chat/completions.

When to use it

Start in a small repository or test project. First ask the tool to read files, explain code, or produce a plan; only then enable edits, terminal commands, or agent automation.

Install or open the tool

Follow the official Qwen Code documentation to install or open the tool. If menus or command names differ, prefer the current official documentation.

For CLI tools, confirm the executable works before adding TokenHub credentials:

qwen --version

Prepare TokenHub credentials

Create a TokenHub API key and choose a model from the TokenHub model list that fits this tool.

export TOKENHUB_API_KEY="sk-..."

Keep the key in your local shell, IDE secret storage, or the secure API key field of the tool. Do not commit it to the repository.

Configure TokenHub persistently

Use the tool configuration file or settings page first. This avoids the common problem where a terminal sees the environment variable but the IDE, plugin, or new window does not.

Provider values

FieldValue
ProviderOpenAI Compatible or Custom
Base URLhttps://us-api.tokenhub.com/v1
API KeyThe value of TOKENHUB_API_KEY
Modelqwen3-coder-plus or another TokenHub model ID

Settings file locations

FieldValue
User config~/.qwen/settings.json
Project config.qwen/settings.json
Provider fieldsmodelProviders.openai, security.auth.selectedType, model.name
{
  "modelProviders": {
    "openai": {
      "protocol": "openai",
      "models": [
        {
          "id": "qwen3-coder-plus",
          "name": "TokenHub / qwen3-coder-plus",
          "baseUrl": "__API_BASE_URL__/v1",
          "envKey": "TOKENHUB_API_KEY"
        }
      ]
    }
  },
  "security": {
    "auth": {
      "selectedType": "openai"
    }
  },
  "model": {
    "name": "qwen3-coder-plus"
  }
}

If your installed version writes a slightly different schema, use its interactive setup first, then keep the same TokenHub values: Base URL, API key, and model ID.

If the tool separates chat, edit, apply, and fast models, use the same TokenHub model for the first test. Split models later by cost, latency, and reasoning strength.

Temporary environment variables for debugging

Use temporary variables only to confirm the key, network, and model name. After validation, move the same values into the persistent configuration above.

export TOKENHUB_API_KEY="sk-..."
export OPENAI_API_KEY="$TOKENHUB_API_KEY"
export OPENAI_BASE_URL="__API_BASE_URL__/v1"
export OPENAI_MODEL="qwen3-coder-plus"

Verify the connection

Test with a read-only prompt first, confirming that the model can read context without changing files. Then test edits, Apply, or agent tasks.

Read the project README and summarize it in three sentences. Do not modify any files.

After a successful prompt, check TokenHub request logs for model name, endpoint, token usage, and billing group.

Troubleshooting

SymptomFix
401 or authentication failureConfirm TOKENHUB_API_KEY is valid and saved in the same terminal, IDE, or client profile.
404 or model not foundUse a model ID that exists in your TokenHub workspace and matches the selected protocol.
Wrong endpointKeep the Base URL exactly as shown. OpenAI-compatible tools usually need /v1; Claude-compatible tools usually do not.
Request timeoutCheck network access to https://us-api.tokenhub.com, proxy settings, and workspace allowlists.
Tool uses another modelRecheck every chat, edit, apply, fast, and autocomplete model slot.

Choose a compatible Qwen Code model

WorkloadModel choiceWhy it fits
Complex coding tasksA tool-capable reasoning modelUse a model with reliable tool use, streaming, and the context length your workflow needs.
Daily implementationA balanced coding modelPrioritize reliable edits and useful latency for normal coding loops.
Background and subagent workA fast, lower-cost modelUse this for iterations, summaries, and simpler work only after validating tool use.
Browse TokenHub models and compatibility

Qwen Code custom API FAQ

Why does Qwen Code still start with its default service?

Check that security.auth.selectedType is openai and that the active model appears in modelProviders.openai.

Should the provider key be tokenhub or openai?

Use the current official modelProviders.openai configuration. TokenHub is connected through its baseUrl and envKey; do not invent an unverified provider key.

Can a project settings file contain a TokenHub API key?

Avoid it. Keep envKey, Base URL, and model ID in shared configuration, while storing the actual key in the local environment or a secret manager.

How do I add a TokenHub model to Qwen Code?

Add the model under modelProviders.openai in ~/.qwen/settings.json, reference TOKENHUB_API_KEY with envKey, and select the same model as the default.

How do I validate Qwen Code agent capabilities?

Begin with a read-only repository question and confirm the request in TokenHub logs, then test tool calls, edits, and commands independently.

References

This setup is based on TokenHub and official documentation, last verified on 2026-09-04.