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:
| Location | Path | Best for |
|---|---|---|
| Global config | ~/.config/opencode/opencode.json | Your personal providers, key references, default model |
| Custom path | The file pointed to by the OPENCODE_CONFIG environment variable | Temporarily switching configs, or use in CI |
| Project config | opencode.json in the project root | Model 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/openaiinstead.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 themodelparameter, so they must match the upstream's model IDs exactly.nameonly controls the display name in the UI. model: the default model, in the formatprovider 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:
| Situation | Recommended method |
|---|---|
| The upstream only has an OpenAI-compatible chat completions endpoint | Method 1 |
| The upstream's model IDs differ from the official names | Method 1 |
| The upstream fully supports the native Anthropic or OpenAI API, with model IDs matching the official ones | Method 2 |
| You want to keep both the official direct connection and a third-party address | Method 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:
- Environment variable reference: write
"apiKey": "{env:MYRELAY_API_KEY}"in the config andexport MYRELAY_API_KEY=...in your shell profile. OpenCode substitutes the value when it loads the config. - File reference:
"apiKey": "{file:~/.secrets/myrelay-key}". The file should contain only the key, readable by you alone. - Store credentials with
/connect: run/connectin OpenCode, choose Other, enter the same provider ID as in your config (such asmyrelay), and paste the key. The credentials are saved to~/.local/share/opencode/auth.json, so the config no longer needs anapiKey. The command-line equivalent isopencode 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:
- Quit and restart OpenCode. The config is read at startup, and a running session won't notice file changes.
- Check the JSON or JSONC syntax. One missing comma or one extra bracket can make the whole config fail to parse. For a
.jsonfile,jq . opencode.jsonis the most direct check (jqdoesn't accept comments, so for files with comments use an editor that supports JSONC). With$schemaadded, your editor will flag misspelled field names. - Verify the provider ID matches everywhere. The key under
provider, the prefix in themodelfield and the ID you entered in/connectmust be identical.myrelayandmy-relayare treated as two different providers. - 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 opencodetemporarily rules out interference. - Check whether the provider is disabled. If the config has
disabled_providers, or you set an allowlist withenabled_providers, your provider ID may be filtered out. - List models from the command line. Run
opencode models, oropencode models myrelayto 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. - 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
modelsmost 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 result | Meaning | Next step |
|---|---|---|
| Connection timeout, TLS error | Network or address problem | Check whether the baseURL host is reachable |
| 401 / 403 | Invalid key or no permission | Check that the environment variable is set in the shell that launches OpenCode |
| 404 | Wrong path or model ID | Check whether baseURL has an extra or missing /v1 |
| 200, but OpenCode errors | Adapter package or config problem | Go 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
-
$schemais added and the editor shows no errors - The provider ID is identical in
provider,modeland/connect - The
npmpackage matches the upstream API type (chat completions or responses) -
baseURLends at/v1, with no specific path - The keys under
modelsexactly 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 modelslists the new model - A direct curl to the upstream returns 200