# anyr opencode

> Route OpenCode through AnyRouter with one CLI command, or a custom provider in opencode.json.


# OpenCode

Route [OpenCode](https://opencode.ai) through AnyRouter for one key, unified billing and request logs across every model.

## Setup

::::tabs

:::tab{label="AnyRouter CLI helper"}

```bash
curl -fsSL https://anyrouter.dev/cli.sh | bash
anyr opencode
```

![AnyRouter CLI launcher with the OpenCode target selected](/img/docs/guides/opencode/cli-launch.png)

The first run signs you in (browser, or device code on SSH/headless; `--key`, `--paste` and `--device` force a path, see [Login](/cli/login)). The key is saved to `~/.anyrouter/config.yaml` and shared with the other launchers.

```bash
anyr opencode --model "anthropic/claude-sonnet-4-6"   # pin a model
anyr opencode --model auto -- --help                  # flags after -- go to OpenCode
anyr opencode --dry-run                               # print resolved environment, run nothing
```

The launcher does not write OpenCode's config files. It injects an `OPENCODE_CONFIG_CONTENT` provider for that process only, so your global `opencode.json` is unchanged. See the [configuration reference](/cli/configuration) for all flags.

:::

:::tab{label="Persistent opencode.json"}

1. Create a key (prefix `sk-ar-`) in your [dashboard](https://anyrouter.dev/dashboard/keys) and export it (add to `~/.bashrc` or `~/.zshrc` to persist):

   ```bash
   export ANYROUTER_API_KEY="sk-ar-your-anyrouter-key"
   ```

2. Add the provider to `~/.config/opencode/opencode.json` (global) or `./opencode.json`:

   ```json
   {
     "$schema": "https://opencode.ai/config.json",
     "provider": {
       "anyrouter": {
         "npm": "@ai-sdk/openai-compatible",
         "name": "AnyRouter",
         "options": {
           "baseURL": "https://anyrouter.dev/api/v1",
           "apiKey": "{env:ANYROUTER_API_KEY}"
         },
         "models": {
           "anthropic/claude-sonnet-4-6": {
             "name": "Claude Sonnet 4.6",
             "limit": { "context": 200000, "output": 65536 }
           },
           "z-ai/glm-5.2": {
             "name": "GLM-5.2",
             "limit": { "context": 1000000, "output": 65536 }
           }
         }
       }
     },
     "model": "anyrouter/anthropic/claude-sonnet-4-6"
   }
   ```

3. Run `opencode` and use `/models` to pick an id.

- `baseURL` must end in `/v1`.
- `apiKey` holds the environment variable name (`{env:ANYROUTER_API_KEY}`), not the secret.
- `models` keys are exact AnyRouter ids (see the [Chat Completions catalog](https://anyrouter.dev/models?endpoint=chat-completions)); `model` is `anyrouter/<id>`.
- If you set `limit`, give both `context` and `output`, or omit `limit`.

:::warning
`{env:ANYROUTER_API_KEY}` resolves only if the variable is in the process that launches OpenCode. Desktop launchers that skip your shell rc see an empty key; export it there, or run `opencode` from a terminal.
:::

:::

::::

## MCP tools

`anyr opencode` connects the AnyRouter MCP gateway with the session's key when **Auto-connect in coding agents** is on in the [dashboard](https://anyrouter.dev/dashboard/mcp). Turn it off with `--no-mcp`, `anyr mcp auto off`, or in the dashboard.

## Verify

Run a prompt in OpenCode; the call appears in your [request logs](https://anyrouter.dev/dashboard/logs) within a second or two with model, tokens and cost. `anyr opencode --dry-run` prints the resolved command and inline provider config (the key is never embedded, only `{env:OPENAI_API_KEY}`).

## Troubleshooting

| Symptom | Fix |
|---|---|
| 401 or auth errors | Persistent config: `echo $ANYROUTER_API_KEY` must print a live `sk-ar-` key. CLI: `anyr config get`, or re-run `anyr setup`. |
| Config invalid / missing `limit.output` | OpenCode 1.18+ needs both `limit.context` and `limit.output`. Add `"output": 65536` or drop `limit`. The CLI writes both. |
| Model not found | The models key must be an exact id (`anthropic/claude-sonnet-4-6`, not `claude-sonnet-4-6`); the default `model` is `anyrouter/<id>`. |
| Requests still hit OpenCode Zen or another provider | A global `opencode.json` pointing at Zen, or a `/connect` credential, can win when you launch `opencode` yourself. Check `~/.config/opencode/opencode.json`; AnyRouter models are namespaced `anyrouter/…`. |


## Related

- [OpenCode with AnyRouter — free models, one API key](/docs/guides/opencode.md)
- [Run OpenCode Against AnyRouter](/docs/scenario-examples/run-opencode-on-anyrouter.md)
- [AnyRouter MCP with OpenCode](/docs/mcp/opencode.md)
- [Configuration](/docs/cli/configuration.md)
- [Login with AnyRouter](/docs/cli/login.md)
