OpenCode Setup with AnonRouter

Connect OpenCode to AnonRouter as a custom OpenAI-compatible provider, configure coding models, enable tools, and protect source-code privacy.

OpenCode can use AnonRouter as a custom OpenAI-compatible provider. Add one compatibility-enabled key, declare the models you want in opencode.json, and switch among them from OpenCode's /models picker.

OpenCode, the open source AI coding agent

OpenCode 2 configuration

This guide uses OpenCode 2's native providers, package, and settings fields. Supported OpenCode 1 configurations still work in OpenCode 2. Keep a working V1 configuration until you have tested its replacement, and do not mix V1 and V2 fields inside one provider entry.

Before you begin

You need an AnonRouter account with available credits or a subscription allowance, a compatibility-enabled inference key, and a tool-capable model from the AnonRouter catalog.

This integration uses a static bearer key. It does not perform AnonRouter's private ticket exchange or independently verify hardware attestation. Use a dedicated key with a suitable credit limit for repeated agent requests.

Create an API key

1. Make the key available to OpenCode

Load ANONROUTER_API_KEY from your secret manager or protected environment into the process running OpenCode. Keep it out of the project configuration and Git. The env setting below names the variable; it does not contain the key.

If you use an already-running OpenCode server, configure the variable in that server's environment. Setting it only in a new terminal does not update a background process.

2. Add the provider and model

Merge this into your project's opencode.json. Replace REPLACE_WITH_CATALOG_MODEL_ID with the exact API ID of a currently available, tool-capable model before starting OpenCode:

{
  "$schema": "https://opencode.ai/config.json",
  "model": "anonrouter/coding",
  "providers": {
    "anonrouter": {
      "name": "AnonRouter",
      "env": ["ANONROUTER_API_KEY"],
      "package": "@opencode/ai/providers/openai-compatible",
      "settings": {
        "baseURL": "https://api.anonrouter.ai/v1"
      },
      "models": {
        "coding": {
          "name": "AnonRouter Coding",
          "modelID": "REPLACE_WITH_CATALOG_MODEL_ID"
        }
      }
    }
  }
}

coding is the local model alias. OpenCode displays the model as AnonRouter Coding and sends modelID to AnonRouter. This keeps your OpenCode selection stable when you change the model behind it. The compatible runtime uses Chat Completions; keep the base URL at /v1.

For all projects, use ~/.config/opencode/opencode.json. Project configuration can override global values, while system policies can impose additional limits.

3. Start and select

After setting the environment and configuration, start a fresh local instance:

opencode --standalone

Open /models and choose AnonRouter Coding. Its OpenCode ID is anonrouter/coding. Add more entries to models when you want several choices.

Begin with a harmless text request, then a read-only tool task in a test project. Confirm the model supports tools before giving the agent permission to edit files or run commands.

Existing OpenCode 1 configuration

V1 uses provider, npm, and options. V2 accepts supported V1 settings, but its native format uses the names shown above. Conversion is optional. If you retain an AI SDK runtime while converting, V2's package value uses the aisdk: prefix. The native compatible runtime above does not need that prefix. See OpenCode's migration guide.

Choose models for agentic coding

OpenCode notes that coding agents need models that are strong at both code and tool calling. For each AnonRouter route, verify:

  • Tools are supported.
  • The context window can hold the relevant files, instructions, and tool output.
  • The output limit is sufficient for the edits you expect.
  • The route's privacy and moderation labels match your project.

Model capabilities and ids change over time. Treat the examples as a starting configuration and verify them against the live catalog before relying on them for unattended work.

How tools work

OpenCode describes local actions, such as reading files, editing code, searching, and running shell commands, with OpenAI tool definitions. AnonRouter forwards supported tool definitions and model tool calls. OpenCode executes the selected tools locally and sends their results back in a later request.

AnonRouter does not execute OpenCode tools. Your OpenCode permissions determine what the agent can read or change, so review them separately from the model provider configuration.

Compatibility limits

FeatureStatus
Streaming Chat CompletionsSupported
Tool callingSupported on tool-capable routes
Static bearer authenticationRequires compatibility access on the key
Inline image message arraysNot supported by AnonRouter's current compatibility contract
Legacy text completionsNot supported
OpenAI Responses APINot used by this configuration

Use the Chat Completions runtime shown above. A Responses runtime targets a different API and is not a substitute for this configuration.

Source-code privacy boundary

OpenCode requests may include source files, patches, repository instructions, terminal output, test failures, tool schemas, and earlier conversation context. Anything included in the prompt reaches AnonRouter and the selected model provider.

This setup uses AnonRouter compatibility mode. The compatibility broker temporarily receives key identity and plaintext request together. AnonRouter does not intentionally retain prompts or responses, but this static-key flow is not unlinkable like the native ticket flow.

Use OpenCode's permission controls, exclude secrets and generated artifacts from agent context, and choose an AnonRouter route whose privacy label fits the code you are sending.

Troubleshooting

AnonRouter does not appear under /models

Confirm opencode.json contains the anonrouter provider and at least one entry under models. Check that the OpenCode server can read ANONROUTER_API_KEY. A saved credential alone does not define the model map.

Authentication returns 401 or 403

Confirm the OpenCode server has ANONROUTER_API_KEY set without printing its value. Verify the key has OpenAI-compatible access enabled. Restart the server after changing its environment.

Requests return 404

Use https://api.anonrouter.ai/v1 for settings.baseURL. Do not append /chat/completions.

Model not found

Replace the modelID placeholder with an exact current AnonRouter API ID. The local alias coding is only for OpenCode; it is not sent as the model ID.

File edits or shell tools do not work

Choose a route that advertises tool calling and review OpenCode's local tool permissions. A successful text response alone does not prove the model emitted a valid tool call.

Context-length errors

Reduce the files and history included in the request or select a model with a larger advertised context window. If you add OpenCode limit fields, copy the current model limits rather than guessing them.

OpenCode setup FAQ

Do I need an OpenCode plugin?

No. OpenCode already supports custom OpenAI-compatible providers.

Can I configure several AnonRouter models?

Yes. Add several entries to the provider's models object, then switch among them with /models.

Should I commit opencode.json?

Project configuration can be committed when it contains only provider and model settings. Keep the key in a protected environment variable.

Is this AnonRouter's private ticket flow?

No. OpenCode sends a static bearer key and therefore uses compatibility mode.

Sources and further reading

AnonRouter is an independent service and is not affiliated with OpenCode.

Let your agent handle the setup

One prompt carries the entire setup.