# Use OpenCode with AnyRouter: CLI launcher or opencode.json

> Point the OpenCode coding agent at AnyRouter in one command, or add a custom Chat Completions provider in opencode.json. One sk-ar- key reaches every catalog model, with unified billing and request logs.

- Date: August 19, 2026
- Category: guides
- HTML: https://anyrouter.dev/blog/opencode-anyrouter

## What this is

OpenCode is an open-source coding agent for the terminal (and a desktop/IDE app). By default it talks to whatever providers you /connect. OpenCode Zen, OpenAI, Anthropic, and so on. Point it at AnyRouter instead and every session shares one sk-ar- key, one bill, and one request log with Claude Code, Codex, and the rest of your stack.

OpenCode speaks OpenAI Chat Completions. AnyRouter serves that dialect at https://anyrouter.dev/api/v1, so this is a config-only change: no OpenCode fork, no rebuild.

OpenCode the agent is not OpenCode Zen. Zen is a model API you can attach as BYOK. This guide is about driving the OpenCode TUI through AnyRouter.

[Full setup guide](https://anyrouter.dev/docs/guides/opencode)

## Fastest path: the anyr CLI

Install OpenCode first (`curl -fsSL https://opencode.ai/install | bash`, or `npm install -g opencode-ai`). Then one command logs you in and launches OpenCode with AnyRouter already selected:

```bash
curl -fsSL https://anyrouter.dev/cli.sh | bash
anyr login
anyr opencode
anyr opencode --model "anthropic/claude-sonnet-4-6"
anyr opencode --model "z-ai/glm-5.2"
anyr opencode --dry-run
```

_Launch OpenCode through AnyRouter._

![AnyRouter CLI launcher with the OpenCode target selected](https://anyrouter.dev/img/docs/guides/opencode/cli-launch.webp)

_The CLI target list includes OpenCode. The first run signs you in; later runs reuse ~/.anyrouter/config.yaml._

The launcher does not write OpenCode's config files. It injects a session-only AnyRouter provider (correct base URL plus catalog context/output limits) and sets OPENAI_API_KEY so the key interpolates at runtime. Quit OpenCode and your global opencode.json is unchanged. The same stored key also powers `anyr claude` and `anyr codex`.

Anything after `--` forwards to OpenCode verbatim (`anyr opencode -- --help`).

## Persistent config: opencode.json

When you want a bare `opencode` command to use AnyRouter every time, register it as a custom `@ai-sdk/openai-compatible` provider. Export the key in your shell, then add this to `~/.config/opencode/opencode.json` (every project) or `./opencode.json` (this repo):

```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"
}
```

_Custom AnyRouter provider. apiKey is the env var name, not the secret._

- Create the key in the dashboard. Every inference key starts with sk-ar-.
- export ANYROUTER_API_KEY before launching. `{env:ANYROUTER_API_KEY}` is the variable name, never paste the secret into the JSON.
- Model map keys must be exact AnyRouter ids. OpenCode's default is `anyrouter/<id>`.
- If you set `limit`, OpenCode 1.18+ requires both `context` and `output`. Omit `limit` entirely if you do not know the windows.

![opencode.json with the AnyRouter custom provider](https://anyrouter.dev/img/docs/guides/opencode/json-provider.webp)

_A persistent provider block so `opencode` talks to AnyRouter without the CLI wrapper._

Then run `opencode`, open `/models`, and pick an `anyrouter/…` id.

![OpenCode TUI with an AnyRouter model selected](https://anyrouter.dev/img/docs/guides/opencode/tui-session.webp)

_The TUI labels the provider AnyRouter. Switch models with /models._

## Verify it billed through AnyRouter

Send any prompt, then open request logs. The row should show the model id, tokens, cost, and latency within a second or two.

![AnyRouter request logs showing an OpenCode session](https://anyrouter.dev/img/docs/guides/opencode/request-logs.webp)

_If the row is there, OpenCode is routing through AnyRouter. Not Zen or a leftover OpenAI key._

- 401. The process does not have a live sk-ar- key. In persistent config, `echo $ANYROUTER_API_KEY`. With the CLI, `anyr config get`.
- Model not found. The map key must match a Chat Completions catalog id, not a Zen or Anthropic-native alias.
- Wrong provider answering. A /connect login for Zen can win when you launch `opencode` directly. Pick `anyrouter/…` in /models, or use `anyr opencode`.

## Where to go from here

Canonical CLI flags live at [anyrouter.dev/docs/cli/opencode](https://anyrouter.dev/docs/cli/opencode). The narrative walkthrough is [anyrouter.dev/docs/guides/opencode](https://anyrouter.dev/docs/guides/opencode). To add AnyRouter tools (credits, catalog, keys) inside the same chat, use the MCP recipe at [anyrouter.dev/docs/mcp/opencode](https://anyrouter.dev/docs/mcp/opencode).

The same AnyRouter key also launches Claude Code and Codex. One profile, every agent.

Launch OpenCode through AnyRouter. One key, the catalog, request logs.

[Open the CLI](https://anyrouter.dev/cli)
