OpenCode Custom Provider Config: opencode.json and Missing Models

Blog / Developer tools

OpenCode Custom Provider Config: opencode.json and Missing Models

To connect OpenCode to an endpoint that isn't in its built-in list, declare a new provider under the provider field of opencode.json: use the @ai-sdk/openai-compatible adapter, set options.baseURL, and list the model IDs you want under models. If you only want the built-in OpenAI or Anthropic provider to use a different address, overriding its options.baseURL is enough. This post covers both ways to set up a custom OpenCode provider, where config files live and how they merge, how to store API keys, and the order in which to check things when you changed the config but your models don't show up in /models.

Field names below follow the official opencode.ai docs (the Config and Providers sections). OpenCode changes quickly, so check the docs for your version before you start.

Where do config files live, and how do multiple configs merge?

OpenCode supports JSON and JSONC (comments and trailing commas allowed). There are three common locations:

LocationPathBest for
Global config~/.config/opencode/opencode.jsonYour personal providers, key references, default model
Custom pathThe file pointed to by the OPENCODE_CONFIG environment variableTemporarily switching configs, or use in CI
Project configopencode.json in the project rootModel choices, instructions and permissions shared by the team

Project config is found by searching upward from the current directory, up to the nearest Git repository root.

These configs are merged; a later one does not replace an earlier one wholesale. The load order is roughly: global config, then OPENCODE_CONFIG, then project config. If a key appears in both, the later one wins; keys that appear on only one side are kept. So you can define the provider and key in the global config and write only "model": "myrelay/some-model" in the project config, and the two combine into one complete config.

That also means that if you edited the global config and nothing changed, the opencode.json in your project directory is most likely overriding it with the same key.

Method 1: Create a new provider with openai-compatible

Use this when the upstream exposes an OpenAI-format /v1/chat/completions endpoint. Below is a minimal config you can copy as-is. Replace myrelay, the URL and the model ID with your own:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "myrelay": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "My Relay",
      "options": {
        "baseURL": "https://api.example.com/v1",
        "apiKey": "{env:MYRELAY_API_KEY}"
      },
      "models": {
        "your-model-id": {
          "name": "Your Model (display name)"
        }
      }
    }
  },
  "model": "myrelay/your-model-id"
}

What the fields mean:

  • myrelay: the provider ID. You pick the name, and every later reference must match it exactly.
  • npm: the AI SDK adapter package OpenCode uses to send requests. For OpenAI-compatible endpoints use @ai-sdk/openai-compatible. The official docs also note that if the upstream uses /v1/responses, you should use @ai-sdk/openai instead.
  • options.baseURL: normally ends at /v1. Don't include /chat/completions; the SDK appends the path itself.
  • The keys under models: sent to the upstream as-is in the model parameter, so they must match the upstream's model IDs exactly. name only controls the display name in the UI.
  • model: the default model, in the format provider ID/model ID.

If the upstream needs extra request headers, add them under options.headers. To tell OpenCode a model's context and output limits, add limit under the model, for example "limit": { "context": 200000, "output": 65536 }. Fill in the numbers from the upstream's docs; these are just examples.

Method 2: Override only the baseURL of a built-in provider

If the upstream is compatible with the native Anthropic or OpenAI format and you want to keep the built-in provider's model list and behavior, you can change just the address:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "anthropic": {
      "options": {
        "baseURL": "https://api.example.com/v1"
      }
    }
  }
}

The upside is that you don't maintain a models list yourself. The downside is that the model list comes from OpenCode's built-in catalog, so if the upstream doesn't offer one of those models, selecting it will produce an error. Use this table to choose between the two methods:

SituationRecommended method
The upstream only has an OpenAI-compatible chat completions endpointMethod 1
The upstream's model IDs differ from the official namesMethod 1
The upstream fully supports the native Anthropic or OpenAI API, with model IDs matching the official onesMethod 2
You want to keep both the official direct connection and a third-party addressMethod 1, with a separate provider ID

Claude Code and Codex change the endpoint differently from OpenCode. See Custom API endpoints for Claude Code, Codex and OpenCode, Claude Code settings.json explained and Codex CLI config.toml field reference.

Where should the apiKey go?

Don't put a plaintext key in the project's opencode.json; project configs are usually committed to the repo. Pick one of these three approaches:

  1. Environment variable reference: write "apiKey": "{env:MYRELAY_API_KEY}" in the config and export MYRELAY_API_KEY=... in your shell profile. OpenCode substitutes the value when it loads the config.
  2. File reference: "apiKey": "{file:~/.secrets/myrelay-key}". The file should contain only the key, readable by you alone.
  3. Store credentials with /connect: run /connect in OpenCode, choose Other, enter the same provider ID as in your config (such as myrelay), and paste the key. The credentials are saved to ~/.local/share/opencode/auth.json, so the config no longer needs an apiKey. The command-line equivalent is opencode auth login.

A sensible split for teams: the project config contains only the provider structure and {env:...} references, and each person sets the environment variable on their own machine. Before committing, scan with something like git grep -n "sk-" to make sure no key slipped in.

I changed the config but /models doesn't show my custom model. What now?

Check in this order. The usual culprit shows up in the first few steps:

  1. Quit and restart OpenCode. The config is read at startup, and a running session won't notice file changes.
  2. Check the JSON or JSONC syntax. One missing comma or one extra bracket can make the whole config fail to parse. For a .json file, jq . opencode.json is the most direct check (jq doesn't accept comments, so for files with comments use an editor that supports JSONC). With $schema added, your editor will flag misspelled field names.
  3. Verify the provider ID matches everywhere. The key under provider, the prefix in the model field and the ID you entered in /connect must be identical. myrelay and my-relay are treated as two different providers.
  4. Confirm which config is actually being read. When you launch from a project directory, the project config overrides same-named keys in the global one. Running OPENCODE_CONFIG=/path/to/test.json opencode temporarily rules out interference.
  5. Check whether the provider is disabled. If the config has disabled_providers, or you set an allowlist with enabled_providers, your provider ID may be filtered out.
  6. List models from the command line. Run opencode models, or opencode models myrelay to see only that provider. If the model isn't listed, the problem is in the config; if it is listed but requests fail, keep going.
  7. Check that the model ID matches the upstream. If you can select the model but the first request returns 404 or "model not found", the key under models most likely doesn't match the upstream ID. Case, prefix and date suffix all have to match.

Start with --print-logs to send logs to the terminal. Config parse failures, SDK package download failures and similar errors will show up there.

When a request fails, is it the config or the network?

If the model appears in the list but doesn't work, bypass OpenCode and hit the upstream directly with curl:

curl -sS "https://api.example.com/v1/models" \
  -H "Authorization: Bearer $MYRELAY_API_KEY"

curl -sS "https://api.example.com/v1/chat/completions" \
  -H "Authorization: Bearer $MYRELAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"your-model-id","messages":[{"role":"user","content":"ping"}]}'

Interpret the result like this:

curl resultMeaningNext step
Connection timeout, TLS errorNetwork or address problemCheck whether the baseURL host is reachable
401 / 403Invalid key or no permissionCheck that the environment variable is set in the shell that launches OpenCode
404Wrong path or model IDCheck whether baseURL has an extra or missing /v1
200, but OpenCode errorsAdapter package or config problemGo back to the checklist in the previous section

For what each status code means and which ones are worth retrying, see LLM API error codes cheat sheet. For timeouts, the four-layer check in Troubleshooting Claude Code connection timeouts and errors applies to OpenCode as well.

To see whether a host is reachable and how much latency it adds, use the online speed test on the cdnping home page. It runs in your browser and measures the "first connection" (including DNS and TLS handshake) and the "round trip" for each AI API. To tell whether an official service itself is having problems, see API status, which probes each official API entry point from the Cloudflare edge every 5 minutes.

Once it's set up, how do you confirm you're getting the model you expect?

A response coming back doesn't mean the request hit the model you intended. After switching to a third-party address, run a few simple checks: the model field in the response, whether the context length matches the upstream's description, and whether token counts and billing line up. For the details, see How to verify which model an API is actually giving you.

Filling this in for AGI Plan

AGI Plan is a commercial partner of cdnping. AGI Plan supports both the OpenAI and Anthropic API formats and has a step-by-step OpenCode tutorial, and both methods above work with it. Take the endpoint address and model IDs from the tutorial at agiplan.dev and its models page. They aren't repeated here so they won't go stale when versions change. Its availability data is mirrored on cdnping at /en/status/agiplan, where you can check it yourself.

Whichever service you connect to, follow its terms of service and the laws and regulations where you are.

Final checklist

  • The config file is in the right place, and the project config isn't unexpectedly overriding the global one
  • $schema is added and the editor shows no errors
  • The provider ID is identical in provider, model and /connect
  • The npm package matches the upstream API type (chat completions or responses)
  • baseURL ends at /v1, with no specific path
  • The keys under models exactly match the upstream model IDs
  • The key is supplied via {env:...}, {file:...} or /connect, with no plaintext in the repo
  • You restarted after the change, and opencode models lists the new model
  • A direct curl to the upstream returns 200

Related articles

Keep reading