OpenCode Custom Provider: V2 and V1 Config Guide
Add an OpenCode custom provider for any OpenAI-compatible API: opencode.json for V2 and V1, baseURL, API key via env, model list, key check and error fixes.
GoiAPI · · 16 min read
Ask AI and more

An OpenCode custom provider is an entry in opencode.json that points OpenCode at any API it does not already know: you pick the OpenAI-compatible runtime package, set baseURL, tell OpenCode which environment variable holds the key, and list the model IDs. The catch is that OpenCode now has two config formats. OpenCode 2 uses a providers block with package and settings, while most guides you will find still show the OpenCode 1 format with provider, "npm": "@ai-sdk/openai-compatible" and options. This guide gives you both, explains where the key has to live in V2 (the part most people get wrong), and lists the errors you are likely to see.
What we checked: the config syntax comes from the official OpenCode docs and config schema on December 5, 2026, when the latest OpenCode 2 release on npm was 2.4.5 (package @opencode/cli) and the last OpenCode 1 release was 1.19.6 (package opencode-ai). The 401 and 404 responses quoted below come from our own curl calls to https://api.goiapi.com on the same day. We have not run OpenCode against a live key for this article, so the OpenCode configs are not tested end to end. Run the checks in step 5 before you rely on them.
The working opencode.json (OpenCode 2)
If you know OpenCode already, this is the whole setup. Save it as ~/.config/opencode/opencode.json (on Windows, %USERPROFILE%\.config\opencode\opencode.json):
{
"$schema": "https://opencode.ai/config.json",
"model": "goiapi/glm-5.3",
"providers": {
"goiapi": {
"name": "GoiAPI",
"env": ["GOIAPI_API_KEY"],
"package": "@opencode/ai/providers/openai-compatible",
"settings": {
"baseURL": "https://api.goiapi.com/v1"
},
"models": {
"glm-5.3": { "name": "GLM 5.3" },
"deepseek-v4-pro-0813": { "name": "DeepSeek V4 Pro" },
"deepseek-v4.1-flash": { "name": "DeepSeek V4.1 Flash" },
"gpt-6.1-sol": { "name": "GPT-6.1 Sol" }
}
}
}
}Then give the key to the OpenCode background service and list the models:
opencode service set env GOIAPI_API_KEY sk-...
opencode modelsThe rest of this guide explains each line, the OpenCode 1 equivalent, and what to do when it does not work.
OpenCode 1 vs OpenCode 2: check before you copy a config
OpenCode 2.0 was published on npm on September 11, 2026 and renamed the provider fields. Both versions install the same opencode command, read config from the same locations, and cannot be installed side by side by default. Run opencode --version to see which one you have.
| OpenCode 1 (what older guides show) | OpenCode 2 (current) | |
|---|---|---|
| npm package | opencode-ai | @opencode/cli |
| Provider block | "provider" | "providers" |
| Runtime | "npm": "@ai-sdk/openai-compatible" | "package": "@opencode/ai/providers/openai-compatible" |
| Endpoint | "options": { "baseURL": ... } | "settings": { "baseURL": ... } |
| Credentials | /connect (stored in auth.json) or options.apiKey | Variables listed in env, settings.apiKey, or /connect (stored in SQLite) |
The official "Migrate from V1" page (checked December 5, 2026) says OpenCode 2 still reads supported V1 config and normalizes it in memory without rewriting your file, so a V1 snippet copied from a blog post usually still loads. Two caveats from the same page: keep each provider entirely in one format (V2 does not infer mixed V1 and V2 fields inside a single provider), and note that the config schema now marks the singular provider key as deprecated. For anything new, write the V2 shape.
Step 1: Install OpenCode 2
The install commands from the official docs on December 5, 2026:
# macOS / Linux
curl -fsSL https://opencode.ai/v2/install | bash
# any OS with Node.js, including Windows
npm install -g @opencode/cliHomebrew users can run brew install anomalyco/tap/opencode-v2. The V2 docs say Windows package managers (Chocolatey, Scoop and so on) are not supported; on Windows use npm or the standalone x64 and ARM64 downloads linked from the docs intro. The older commands npm install -g opencode-ai and curl -fsSL https://opencode.ai/install | bash (no /v2) install OpenCode 1.
Step 2: Get an API key
Any OpenAI-compatible endpoint works with these steps. With GoiAPI:
- Create a GoiAPI account with email or Google and verify your email.
- Open Top up in the dashboard and pay with USDT (TRC20 or BEP20) or Binance Pay. No credit card is needed. Each top-up is 5 to 4,000 USDT, and 1 USDT adds 1 USD to your balance. Top-ups are final and non-refundable (see the refund policy), so add only what you plan to use; a small first top-up is enough to test.
- Open API Keys, create a key named "opencode" and copy it right away, because it is shown only once. Keys look like
sk-.... Set a daily spending limit on the key: coding agents send many requests back to back.
Step 3: Write the custom provider config
OpenCode merges config from several places. The two you will use:
- Global, for every project:
~/.config/opencode/opencode.json - Per project:
opencode.jsonor.opencode/opencode.jsonin the project root
Project files override the global file. Keep provider entries in the global file so you never commit them by accident. Both opencode.json and opencode.jsonc work; the JSONC form allows comments and trailing commas. The snippet above is strict JSON, so it works in either.
What each field does:
goiapi: the provider ID. You choose it, and model references use it as a prefix:goiapi/glm-5.3.env: an ordered list of environment variable names that can supply the credential. Only the variable name goes in the file, never the key.package: the runtime.@opencode/ai/providers/openai-compatiblespeaks the Chat Completions format, so requests go tobaseURL+/chat/completions. The migration guide also accepts the prefixed AI SDK formaisdk:@ai-sdk/openai-compatible. There is a separate@opencode/ai/providers/openai-compatible/responsespackage for endpoints that implement the Responses API instead.settings.baseURL: the API root including/v1, without/chat/completions. A missing or doubled/v1is the most common cause of a 404 (our OpenAI-compatible API explainer covers base URL vs endpoint).models: each key is the exact model ID the API expects, as listed on GoiAPI pricing.nameis only the label in the picker. If you want a short alias, use a different key and put the real ID inmodelID, for example"coder": { "modelID": "glm-5.3" }.modelat the root: the default model, inprovider/modelform.
Context and output limits
For a model that is not in OpenCode's catalog, the V2 models docs say OpenCode assumes tool support, text and image input, a 200,000-token context window and a 32,000-token output limit. These are fallbacks, not detected values. OpenCode's automatic compaction uses limit.input capped at limit.context, so set these when you know the real figures:
"glm-5.3": {
"name": "GLM 5.3",
"limit": { "context": 200000, "output": 32000 }
}The Context column on GoiAPI's pricing page is still being filled in, so if you are unsure, keep the defaults. If you hit context errors in long sessions, see Claude Code "prompt is too long" for how context windows fill up.
The OpenCode 1 config you will still find online
If you are on OpenCode 1, or you are comparing with an older tutorial, this is the equivalent V1 config:
{
"$schema": "https://opencode.ai/config.json",
"model": "goiapi/glm-5.3",
"provider": {
"goiapi": {
"npm": "@ai-sdk/openai-compatible",
"name": "GoiAPI",
"options": {
"baseURL": "https://api.goiapi.com/v1",
"apiKey": "{env:GOIAPI_API_KEY}"
},
"models": {
"glm-5.3": { "name": "GLM 5.3" },
"deepseek-v4-pro-0813": { "name": "DeepSeek V4 Pro" },
"gpt-6.1-sol": { "name": "GPT-6.1 Sol" }
}
}
}
}The V1 docs say @ai-sdk/openai-compatible is for /v1/chat/completions, and to switch to @ai-sdk/openai for models served through /v1/responses. For OpenCode, Chat Completions is all you need.
Step 4: Give OpenCode the API key
This is where OpenCode 1 and 2 differ the most, and where most "it still says 401" reports come from.
OpenCode 2: the key must reach the background server
OpenCode 2 has a client-server design. By default every local OpenCode client connects to one shared background server for your user account, and that server makes the model requests. The CLI providers docs say a provider's environment variable must be set on the server process. So an export GOIAPI_API_KEY=... in your shell may never reach a server that was already running. Pick one of these:
Option A: set the variable on the shared service (best for daily use):
opencode service set env GOIAPI_API_KEY sk-...
opencode auth listSetting it stops the running service, and the next OpenCode command starts it with the new value. auth list should then show a connection of type environment. The value is kept in the service settings on your machine, so do this only on a machine you trust. Remove it with opencode service unset env GOIAPI_API_KEY. The key also lands in your shell history; clear that entry afterwards.
Option B: a private server for this session (export in the same terminal, then start standalone):
export GOIAPI_API_KEY="sk-..."
opencode --standaloneOn Windows PowerShell, use $env:GOIAPI_API_KEY = "sk-..." before opencode --standalone.
Option C: read the key from a file. Add apiKey to the provider's settings:
"settings": {
"baseURL": "https://api.goiapi.com/v1",
"apiKey": "{file:~/.secrets/goiapi-key}"
}{env:NAME} works in the same place but still depends on the server's environment. Per the config docs, a missing variable or file is replaced with an empty string, so the request goes out with no key.
If you add a key with /connect or opencode auth login instead, OpenCode 2 stores it as an account in the server's SQLite database, usually ~/.local/share/opencode/opencode.db (run opencode debug paths db to see yours). V2 imports supported credentials from the old auth.json on first run. Don't edit the database by hand; use /connect or the auth commands.
OpenCode 1: /connect or options.apiKey
On OpenCode 1, the documented flow for a custom provider is:
- Run
opencode, type/connect, scroll down and pick Other. - Enter the provider ID. It must match the ID in your config,
goiapihere. - Paste the key. OpenCode 1 stores it in
~/.local/share/opencode/auth.json.
Or skip /connect and keep "apiKey": "{env:GOIAPI_API_KEY}" in options, as in the V1 snippet. Run opencode auth list to confirm what OpenCode sees.
Step 5: Verify the key, then pick a model
First test the key and URL outside OpenCode. Listing models is not billed (see List models via the API):
curl https://api.goiapi.com/v1/models -H "Authorization: Bearer $GOIAPI_API_KEY" -H "Accept-Language: en"In PowerShell, call curl.exe and use $env:GOIAPI_API_KEY. A JSON list that includes glm-5.3 means the key and URL are right. With our test key on December 3, 2026, this returned HTTP 200 and the 9 models on the pricing page. On December 5, 2026 we repeated the call without a key and with a made-up key, and got these exact messages:
401 Missing API key. Send your key in the Authorization: Bearer <key> header or the x-api-key header.
401 The API key is invalid or has been revoked.The first message also comes back when the header says Bearer but the key is empty, which is what an unset {env:...} variable produces. Requests without a valid key get these English messages when the request sends Accept-Language: en; with a valid key, messages follow your account language (see error codes).
Next, from your project folder:
opencode models
opencode run --model goiapi/glm-5.3 "Summarize this repository in three sentences"opencode models should list goiapi/glm-5.3, goiapi/deepseek-v4-pro-0813 and the rest. opencode run sends one prompt without opening the full interface. Inside the TUI, /models switches models. Then open Request history in the GoiAPI dashboard: every request appears there with its token counts and cost.
Which models to use, and what they cost
Prices per 1M tokens in USD from GoiAPI pricing, checked December 5, 2026:
| Model | Reference in OpenCode | Input | Output | Cache read |
|---|---|---|---|---|
| GLM 5.3 | goiapi/glm-5.3 | $0.144144 | $0.432432 | $0.014495 |
| DeepSeek V4 Pro | goiapi/deepseek-v4-pro-0813 | $0.144144 | $0.432432 | $0.014495 |
| DeepSeek V4.1 Flash | goiapi/deepseek-v4.1-flash | $0.0408408 | $0.1225224 | $0.0040664 |
| GLM 5.3 Flash | goiapi/glm-5.3-flash | $0.0408408 | $0.1225224 | $0.0040664 |
| GPT-6 Luna | goiapi/gpt-6-luna | $0.0208 | $0.1248 | $0.002106 |
| GPT-6.1 Sol | goiapi/gpt-6.1-sol | $0.13 | $0.78 | $0.013026 |
| GPT-6.2 Sol | goiapi/gpt-6.2-sol | $0.13 | $0.78 | $0.013026 |
| Claude Opus 5.5 | see the Claude section | $0.442 | $2.21 | $0.0221 |
Each request that returns content has a small minimum charge, and a request that fails without returning content is not charged.
Coding agents resend file contents and the conversation on every step, so input tokens usually dominate. As an illustration only (a calculation from listed prices, not a measurement, ignoring cache discounts), a session that adds up to 2 million input tokens and 100,000 output tokens costs:
glm-5.3ordeepseek-v4-pro-0813: 2 × $0.144144 + 0.1 × $0.432432 ≈ $0.33deepseek-v4.1-flash: 2 × $0.0408408 + 0.1 × $0.1225224 ≈ $0.094gpt-6.1-sol: 2 × $0.13 + 0.1 × $0.78 = $0.338claude-opus-5-5: 2 × $0.442 + 0.1 × $2.21 = $1.105
Real sessions vary with project size and the number of steps, so check Request history after your first one or try the cost calculator. For a deeper price breakdown, see DeepSeek V4 API pricing and GLM 5.3 API pricing. If you are choosing between an open model and Claude for agent work, our DeepSeek vs Claude for coding comparison goes further.
Two findings from a December 3, 2026 run where we sent the same one-sentence question to all 9 models through /v1/chat/completions (one call each, no tools, so a snapshot rather than a benchmark):
deepseek-v4.1-flash,glm-5.3andglm-5.3-flashreturned reasoning tokens. Those count as output and are billed. In one run,deepseek-v4.1-flashwithmax_tokens: 400spent the whole budget on reasoning and returned an empty answer. Don't setlimit.outputvery low for these models.- Both Claude models answered through the OpenAI format, but a later run the same day got repeated
503errors forclaude-haiku-5-5on that format while the Anthropic format kept working. For Claude, use the Anthropic setup below.
Claude in OpenCode through the Anthropic format
GoiAPI's Chat Completions docs recommend the Anthropic format for Claude models. In OpenCode, add a second provider that uses the Anthropic runtime. OpenCode 2 version (not tested end to end):
"goiapi-claude": {
"name": "GoiAPI Claude",
"env": ["GOIAPI_API_KEY"],
"package": "@opencode/ai/providers/anthropic-compatible",
"settings": {
"baseURL": "https://api.goiapi.com/v1"
},
"models": {
"claude-opus-5-5": { "name": "Claude Opus 5.5" },
"claude-haiku-5-5": { "name": "Claude Haiku 5.5" }
}
}Put it inside "providers", next to goiapi. On OpenCode 1 the equivalent is "npm": "@ai-sdk/anthropic" with "options": { "baseURL": "https://api.goiapi.com/v1" }. The V1 docs show https://api.anthropic.com/v1 as the Anthropic base URL, which is why we keep /v1; if you see a 404 for the path /v1/v1/messages, drop /v1 from baseURL. The Anthropic runtime sends the key in the x-api-key header, which GoiAPI accepts alongside Authorization: Bearer (see authentication).
One more note from the OpenCode 1 docs: plugins that let you use a Claude Pro or Max subscription in OpenCode exist, and Anthropic explicitly prohibits that use. An API key is the supported route.
Troubleshooting OpenCode custom provider errors
Errors from OpenCode itself
| Error or symptom | Likely cause | Fix |
|---|---|---|
Model unavailable: goiapi/... (V2) or ProviderModelNotFoundError (V1) | Model ID typo, model missing from models, or the provider has no credential | Use provider/model, match the models key (not modelID), run opencode models |
No model is available for session ... (V2) | No selectable model yet | Finish step 4, then pick a model with /models |
Cannot initialize provider/model: NAME is required to resolve the provider endpoint (V2) | A ${NAME} placeholder is left in baseURL | Write the full URL https://api.goiapi.com/v1 |
Provider missing from /models | Wrong file location, provider vs providers mix-up, or V1 and V2 fields mixed inside one provider | Run opencode debug config to list the config files OpenCode loaded; keep each provider in one format |
ProviderInitError (V1) | Invalid or corrupted config | Recheck the config; the V1 docs suggest clearing ~/.local/share/opencode and running /connect again |
| Config edits have no effect (V2) | The background server still has the old config | Run opencode reload or opencode service restart |
HTTP errors from the API
| Status | Meaning | Fix |
|---|---|---|
| 401 | Missing, empty, wrong or revoked key | On V2, make sure the variable reached the server (step 4) and check opencode auth list. If you ran /connect for goiapi with an older key, that saved account wins over the environment variable; remove it with opencode auth logout. |
| 402 | Not enough balance to hold funds for the request | Top up. Funds are held based on estimated input plus the maximum output tokens, so when your balance is low, a lower maximum output (in OpenCode, likely limit.output; not verified) makes the hold smaller (limits and balance) |
| 403 | The key is not allowed to use this model or to call from this IP | Check the key's model and IP restrictions in the dashboard |
| 404 | Wrong model ID or wrong path | Check the ID and that baseURL has exactly one /v1 |
| 429 | Per-minute limit, your spending limit, or the model is overloaded | Wait for Retry-After. Our test key on December 3, 2026 allowed 60 requests per minute; yours may differ |
| 503 | Maintenance, or the model is not responding on this format | Retry later; for Claude, use the Anthropic provider |
The two path errors we got on December 5, 2026:
POST https://api.goiapi.com/v1/v1/chat/completions
404 The path POST /v1/v1/chat/completions does not exist.
POST https://api.goiapi.com/chat/completions
404 Cannot POST /chat/completionsThe first means /v1 is doubled in baseURL; the second means it is missing. A wrong model ID returns The model {model} does not exist. See the list at /v1/models. Every response carries an x-request-id header: send it through the contact page if you need help, never the key. The full list is in GoiAPI error codes.
To see what OpenCode sent, read ~/.local/share/opencode/log/opencode.log and filter for role=server.
OpenCode Go vs your own API key
OpenCode sells its own access through the OpenCode Console and the Go plans. Per the docs on December 5, 2026: Go is $10 per month, Go Plus $40 per month, and Go Teams $20 per member per month with a 2-member minimum, each with usage limits on a list of open coding models the OpenCode team has tested. You subscribe in the Console, then /connect and choose OpenCode.
A custom provider with your own key differs in three ways: you pay per token instead of a monthly fee, you choose from your gateway's catalog (including GPT and Claude models), and you can set a spending limit per key. If you code every day with open models and Go's limits fit, a flat plan is easier to budget. If your usage is uneven, or you want to pay with USDT, a per-token key fits better. Both can be connected at the same time; each provider shows up separately in /models.
FAQ
Where does OpenCode store API keys?
OpenCode 1 stores keys added with /connect in ~/.local/share/opencode/auth.json. OpenCode 2 stores them as accounts in the server's SQLite database, usually ~/.local/share/opencode/opencode.db, and imports the old auth.json when you upgrade. Keys read from environment variables are not saved as accounts; unset the variable to disconnect.
Does @ai-sdk/openai-compatible still work in OpenCode 2?
Yes, through V1 compatibility or the prefixed form aisdk:@ai-sdk/openai-compatible. The native V2 package is @opencode/ai/providers/openai-compatible, which is what the current docs use in their custom provider example.
Can I use the same key in OpenCode, Codex and Cline?
Yes, a key is not tied to a tool. A separate key per tool is still safer: separate spending limits, a clear view of which tool spent what, and you can revoke one without breaking the others. See our Codex CLI custom provider guide and the Cline, Roo Code, Kilo Code and Cursor setup.
Why does OpenCode still return 401 after I exported the key?
On OpenCode 2 the shared background server was most likely started before you exported the variable, so it never saw it. Run opencode service set env GOIAPI_API_KEY sk-..., or start opencode --standalone from the terminal where the variable is set. If you ran /connect for goiapi with an older key, that saved account wins over the environment variable; remove it with opencode auth logout.
Do I need the /connect step for a custom provider?
On OpenCode 1 it is one of two options; options.apiKey with {env:...} is the other. On OpenCode 2 the custom provider example in the docs uses the env list instead, so a variable on the server, or settings.apiKey, is enough.
Get started
Create an account, top up a small amount with USDT or Binance Pay, paste the config from the top of this guide into ~/.config/opencode/opencode.json, give the key to the server as in step 4, and run the checks in step 5. The quickstart has the same account and key steps with a test call you can run from your terminal.
GoiAPI sells API access to the models mentioned in this article.


