OpenCode Custom Models: API Key, Base URL, and JSON Setup
Connect OpenCode CLI and Desktop to BetterToken with Messages, Chat Completions, or Responses. Configure Claude caching and verify the request path.
Connect OpenCode with your BetterToken API Key. Use this page for general API Key, Base URL, and opencode.json setup. To select or verify a specific model, see the GLM guide or Kimi K3 guide. Copy the exact Model ID from the model plaza.
Quick start: configure OpenCode 1.x or 2.x automatically.
Key settings
| Field | Value |
|---|---|
| API Key | BetterToken API Key |
| Base URL | https://bettertoken.ai/v1 |
| Model | YOUR_MODEL_ID |
Prerequisites
- Desktop: Download OpenCode Desktop from the official OpenCode site.
- CLI: Install OpenCode 2.x CLI below. To check an existing installation, run
opencode --version. - Create a BetterToken API Key
- Copy a Model ID from the model plaza or key Setup dialog
Already have an account? Sign in, top up, then create a key in the console.
Install
OpenCode 2.x CLI (OpenCode: installation guide)
npm install -g @opencode/cli@2opencode --versionOpenCode 1.x CLI
curl
curl -fsSL https://opencode.ai/install | bashnpm
npm install -g opencode-aiSetup options
Automatic setup (recommended): OpenCode 1.x and 2.x
Use this with the CLI or Desktop on a local server. Install Node.js, run the command, enter your BetterToken API Key, and choose a model. The script sets the default model and sends a billable test request.
The script replaces an existing bettertoken provider entry. To keep its models, choose a protocol, or set a one-hour Claude cache, use manual setup for 2.x. For 1.x, use the Desktop or CLI instructions.
macOS / Linux
curl -fsSL "https://bettertoken.ai/install-opencode-provider.sh" | bashWindows
iwr "https://bettertoken.ai/install-opencode-provider.ps1" -OutFile "$env:TEMP\install-opencode-provider.ps1"; powershell -ExecutionPolicy Bypass -File "$env:TEMP\install-opencode-provider.ps1"Already know the model ID? Skip the selection list:
- macOS / Linux: Replace the final
bashin the command withbash -s -- --model YOUR_MODEL_ID. - Windows: Add
-Model YOUR_MODEL_IDto the end of the command.
Next step. Quit OpenCode completely, then reopen it. In Desktop, select the added model below the message box. In the CLI, run /models to select it.
What the script changes and how to restore your settings
- File written. The script writes the
bettertokenprovider to~/.config/opencode/opencode.jsonand sets the top-levelmodelto your selected model. It replaces any existingbettertokenentry and preserves other providers, MCP, and other settings. - API selection. The script uses
@ai-sdk/openai(Responses) for GPT,@ai-sdk/anthropic(Anthropic Messages) for Claude, and@ai-sdk/openai-compatible(Chat Completions) for other models. - Backup and restore. Before writing, the script saves
opencode.json.bak.<timestamp>in the same directory. To restore, replaceopencode.jsonwith the backup. If your file contains comments, use the manual instructions for your version. - Built-in test. The script sends one test request with your selected model after setup. It leaves any running OpenCode instance open.
Manual setup: OpenCode 2.x (CLI / Desktop)
The CLI and Desktop share a user config when they use the local server. You do not need a Custom provider button or a second key. If Desktop connects to a remote OpenCode server, configure the machine running that server instead.
Open your user config
The commands below open opencode.json in its default location. If you already use opencode.jsonc or a custom config path, edit that file instead. Back up existing settings first. Never commit a file containing your key.
Windows (PowerShell)
%USERPROFILE%\.config\opencode\opencode.json
$root = if ($env:XDG_CONFIG_HOME) { $env:XDG_CONFIG_HOME } else { Join-Path $HOME ".config" }$dir = Join-Path $root "opencode"New-Item -ItemType Directory -Force -Path $dir | Out-Null$file = Join-Path $dir "opencode.json"if (-not (Test-Path $file)) { New-Item -ItemType File -Path $file | Out-Null }notepad $filemacOS / Linux
~/.config/opencode/opencode.json
config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/opencode"mkdir -p "$config_dir"nano "$config_dir/opencode.json"nano: Ctrl+O → Enter → Ctrl+X.
Choose the API your model supports
Check the endpoint in your model guide, then select its tab below. For a new file, copy the full example. In an existing file, add only the provider entry under providers. Keep your other providers and default model. Replace YOUR_BETTERTOKEN_API_KEY with your BetterToken API Key. All three examples can use the same key.
Set the Base URL to https://bettertoken.ai/v1. Each adapter appends its own request path.
Messages (Claude)
{ "$schema": "https://opencode.ai/config.json", "model": "bettertoken-claude/claude-sonnet-5-5", "providers": { "bettertoken-claude": { "name": "BetterToken (Claude Messages)", "package": "@opencode/ai/providers/anthropic-compatible", "settings": { "baseURL": "https://bettertoken.ai/v1", "apiKey": "YOUR_BETTERTOKEN_API_KEY" }, "models": { "claude-sonnet-5-5": { "name": "Claude Sonnet 5.5" } } } }}This adapter appends /messages to the Base URL and sends POST /v1/messages. Do not append /messages or /chat/completions yourself. This configuration uses the default 5-minute cache.
Optional: use the 1-hour cache
Five-minute caching needs no extra settings. For one hour, add "cache": { "ttl": "1h" } inside the existing settings for bettertoken-claude. Keep baseURL, apiKey, and all other fields. One-hour writes cost more than five-minute writes. Remove cache to restore the default.
Chat Completions (DeepSeek)
{ "$schema": "https://opencode.ai/config.json", "model": "bettertoken-chat/deepseek-flash", "providers": { "bettertoken-chat": { "name": "BetterToken (Chat Completions)", "package": "@opencode/ai/providers/openai-compatible", "settings": { "baseURL": "https://bettertoken.ai/v1", "apiKey": "YOUR_BETTERTOKEN_API_KEY" }, "models": { "deepseek-flash": { "name": "DeepSeek V4.1 Flash" } } } }}Responses (GPT)
{ "$schema": "https://opencode.ai/config.json", "model": "bettertoken-responses/gpt-6.2-sol", "providers": { "bettertoken-responses": { "name": "BetterToken (Responses)", "package": "@opencode/ai/providers/openai-compatible/responses", "settings": { "baseURL": "https://bettertoken.ai/v1", "apiKey": "YOUR_BETTERTOKEN_API_KEY" }, "models": { "gpt-6.2-sol": { "name": "GPT-6.2 Sol" } } } }}Select the model and verify the connection (all three protocols)
Save the file, fully quit OpenCode, then reopen it.
CLI: Run opencode, enter /models, and select the full model value from your example.
Desktop: Open Settings → Providers and find your provider under Connected providers. Config means OpenCode loaded the configuration. It does not confirm a successful request.

Enable the model under that provider in Settings → Models. In a new session, select the same provider and model below the message box. The screenshots show Claude with the provider name BetterToken.

Send a short message, such as “Suggest two event ideas.” After the reply, check the model ID and path in the BetterToken request log. The examples produce the entries below. Test requests incur charges.
- Messages:
claude-sonnet-5-5→/v1/messages - Chat Completions:
deepseek-flash→/v1/chat/completions - Responses:
gpt-6.2-sol→/v1/responses
Optional: check Claude Messages cache usage on your bill
In the same session, ask which idea suits first-time attendees. You can follow up immediately with either cache duration. A one-hour write on the first bill confirms the duration. Wait more than five minutes only to test reuse beyond the default window.
In the BetterToken request log, open the billing details for both main replies. Check the model and /v1/messages. Look for writes of the selected duration on the first request and a positive cache-read token count on the follow-up. Even the first request can reuse a shared prefix. Generating a session title can produce a separate charge.
If reads are zero, use the same model, keep a sufficiently long prefix unchanged, and follow up before expiry. If the path is /v1/chat/completions, select the Messages provider above.
For example, with the 1-hour cache enabled in Desktop 2.4.2, a follow-up after about eight minutes read 13,914 cached tokens. Cache billing and usage fields.
Manual setup: OpenCode 1.x
Desktop setup
The screenshots show OpenCode Desktop 1.19.6. This setup uses the OpenAI-compatible Chat Completions API without manually editing a configuration file.
Open settings
On the OpenCode Desktop home screen, click Settings on the left.

Add a custom provider
Select Providers. Click Connect next to Custom provider.

Enter your BetterToken details
| Field | Value |
|---|---|
| Provider ID | bettertoken (all lowercase) |
| Display name | BetterToken |
| Base URL | https://bettertoken.ai/v1 |
| API key | Paste your BetterToken API Key |

Add a model and save
Scroll to Models. In the left field, enter the exact Model ID of a model that supports Chat Completions, such as claude-fable-5. In the right field, enter a display name or repeat the Model ID. To add more models, click Add model. Leave Headers (optional) blank, then click Submit.

Select a model and verify
Click New session. Open the model picker below the message box. Select your model under BetterToken. Send a short message to check that the model replies.

If your 2.x Desktop has no Custom provider button, follow the model setup by protocol.
CLI setup
Option 1: save credentials with /connect
Start OpenCode and run:
/connectChoose Other, enter bettertoken as the provider id, and then enter YOUR_API_KEY. OpenCode stores the credential locally.
Option 2: configure JSON
The global file is ~/.config/opencode/opencode.json. A project can use opencode.json in its root.
{ "$schema": "https://opencode.ai/config.json", "model": "bettertoken/YOUR_MODEL_ID", "provider": { "bettertoken": { "npm": "@ai-sdk/openai-compatible", "name": "BetterToken", "options": { "baseURL": "https://bettertoken.ai/v1" }, "models": { "YOUR_MODEL_ID": { "name": "YOUR_MODEL_ID" } } } }}If you do not use /connect, add "apiKey": "YOUR_API_KEY" under options. Never commit a configuration containing a real key.
This example uses @ai-sdk/openai-compatible for Chat Completions. The SDK package must match the endpoint protocol; consult the official provider guidance before using a different protocol. In macOS testing with a GPT model, OpenCode 1.19.4 and 2.1.4 both completed file-reading tool calls with each SDK: @ai-sdk/openai-compatible used /v1/chat/completions, and @ai-sdk/openai used /v1/responses. All four file-reading cases passed with read allowed and edit and bash denied.
Verify the connection
Run opencode in a project, select PROVIDER_ID/MODEL_ID, and send a short message. A normal response confirms the setup. Restart OpenCode after changing JSON.
For a non-interactive text check with OpenCode 1:
opencode run --format json -m bettertoken/YOUR_MODEL_ID "Reply with only: OK"OpenCode 2 requires --standalone for this non-interactive run:
opencode run --standalone --format json -m PROVIDER_ID/MODEL_ID "Reply with only: OK"Switch models
Check which endpoint the new model supports. Add models that use the same protocol under the same provider’s models. For Chat Completions or Responses instead of Messages, add a separate provider with the matching adapter. Keep the existing provider and reuse your BetterToken API Key. Changing the model ID alone does not switch protocols. Save, restart, and select the model in your session. Change the top-level model only to set a new default.
Common errors
| Error | Fix |
|---|---|
401 | Run /connect again or check apiKey. |
404 | Use only https://bettertoken.ai/v1 as baseURL; do not append /chat/completions. |
| Model not found | Set the top-level model to providerID/modelID, for example bettertoken-claude/claude-sonnet-5-5. Under that provider’s models, use only claude-sonnet-5-5. |
| Configuration not applied | Check whether project opencode.json overrides the global file, then restart OpenCode. |
Advanced setup
Related FAQ
Optional: manage the provider with CC Switch
To manage providers for several tools in one place, see OpenCode setup in CC Switch.
Technical details
Configure the model
Automatic setup writes a provider entry. OpenCode 2.x can read this compatibility format. The manual 2.x examples use native providers entries.

