# Qwen Code

> Add Deference to Qwen Code as an OpenAI-compatible model provider.

Qwen Code reads model providers from `modelProviders` in `settings.json`. The `openai` provider speaks Chat Completions, so the base URL is `https://deference.si/v1`: use the root, not the full path.

## Set up

1. Create a key in [API keys](https://deference.si/keys) and export it as `DEFERENCE_API_KEY`, or add it to `~/.qwen/.env` as `DEFERENCE_API_KEY=sk-df-...`.
2. Add the provider to `~/.qwen/settings.json` (on Windows, `%USERPROFILE%\.qwen\settings.json`).

```json
{
  "modelProviders": {
    "openai": [
      {
        "id": "anthropic/claude-sonnet-5.5",
        "name": "Claude Sonnet 5.5 (Deference)",
        "baseUrl": "https://deference.si/v1",
        "envKey": "DEFERENCE_API_KEY",
        "generationConfig": {
          "contextWindowSize": 200000,
          "samplingParams": { "max_tokens": 32000 }
        }
      }
    ]
  },
  "security": { "auth": { "selectedType": "openai" } },
  "model": { "name": "anthropic/claude-sonnet-5.5" }
}
```

3. Start `qwen`.

Do not put the key in the `env` block of `settings.json` or pass it with `--openai-api-key`. Both keep it where it can leak. `envKey` names the variable, so the key stays in your environment.

## Verify

1. Inside a session, run `/doctor`. It checks the auth and environment.
2. Run `/model`. The entry is listed.
3. Run a one-off prompt.

```bash
qwen -m anthropic/claude-sonnet-5.5 -p "Reply with exactly: gateway-ok"
```

4. Open [Activity](https://deference.si/activity). The request is the top row, with the model you chose.

## Pick a model

* Pick a model with `/model`, `-m`, or `model.name` in the settings.
* Put the limits inside the entry, in `generationConfig`. A `model.generationConfig` at the top level does not apply to provider models.
* Add `"wireApi": "responses"` to an entry to send it to `POST /v1/responses`. The default is `chat-completions`.
* Edits to `modelProviders` apply without a restart.

## Troubleshooting

| You see                                 | Cause                                                                  | Fix                                                  |
| --------------------------------------- | ---------------------------------------------------------------------- | ---------------------------------------------------- |
| `401 invalid_api_key`                   | `DEFERENCE_API_KEY` is not set where Qwen Code starts                  | Export it in that shell, or add it to `~/.qwen/.env` |
| The model is not in `/model`            | The provider key is not `openai`, or the entry is wrapped in an object | Use a bare array under `modelProviders.openai`       |
| Qwen Code asks you to sign in with Qwen | `selectedType` is not set                                              | Set `security.auth.selectedType` to `openai`         |
| `404`                                   | `baseUrl` is the full path, or lacks `/v1`                             | Use `https://deference.si/v1`                        |
| `404 model_not_found`                   | `id` differs from the catalog                                          | Copy the id from the Models page                     |
