Skip to content
GuidesAugust 19, 2026

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.

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

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:

curl -fsSL https://anyrouter.dev/setup.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
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):

{
  "$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
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
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
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 docs.anyrouter.dev/cli/opencode. The narrative walkthrough is docs.anyrouter.dev/guides/opencode. To add AnyRouter tools (credits, catalog, keys) inside the same chat, use the MCP recipe at docs.anyrouter.dev/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

Route your first request in 2 minutes

Start free with your own keys, or top up and pay per token. Get $4/mo in credits and free models on Go — $2/mo, or free when you donate a provider key.

Start free