Skip to content
Sign Up Free
Coding and development

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

FieldValue
API KeyBetterToken API Key
Base URLhttps://bettertoken.ai/v1
ModelYOUR_MODEL_ID

Prerequisites

Sign up and get a BetterToken API Key

Already have an account? Sign in, top up, then create a key in the console.

Install

OpenCode 2.x CLI (OpenCode: installation guide)

Terminal window
npm install -g @opencode/cli@2
opencode --version
OpenCode 1.x CLI

curl

Terminal window
curl -fsSL https://opencode.ai/install | bash

npm

Terminal window
npm install -g opencode-ai

migration guide

Setup options

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

Terminal window
curl -fsSL "https://bettertoken.ai/install-opencode-provider.sh" | bash

Windows

Terminal window
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 bash in the command with bash -s -- --model YOUR_MODEL_ID.
  • Windows: Add -Model YOUR_MODEL_ID to 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 bettertoken provider to ~/.config/opencode/opencode.json and sets the top-level model to your selected model. It replaces any existing bettertoken entry 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, replace opencode.json with 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

Terminal window
$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 $file

macOS / Linux

~/.config/opencode/opencode.json

Terminal window
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.

BetterToken listed under Connected providers with the Config label

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.

Claude Sonnet 5.5 enabled under BetterToken in Models

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.

Settings on the left side of the OpenCode Desktop home screen

Add a custom provider

Select Providers. Click Connect next to Custom provider.

Connect button next to Custom provider in Providers

Enter your BetterToken details

FieldValue
Provider IDbettertoken (all lowercase)
Display nameBetterToken
Base URLhttps://bettertoken.ai/v1
API keyPaste your BetterToken API Key
Display name set to BetterToken and Base URL set to the BetterToken /v1 address

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.

Models section with the Model ID on the left, display name on the right, and Submit below

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.

Model picker showing claude-fable-5 under BetterToken

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:

/connect

Choose 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:

Terminal window
opencode run --format json -m bettertoken/YOUR_MODEL_ID "Reply with only: OK"

OpenCode 2 requires --standalone for this non-interactive run:

Terminal window
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

ErrorFix
401Run /connect again or check apiKey.
404Use only https://bettertoken.ai/v1 as baseURL; do not append /chat/completions.
Model not foundSet 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 appliedCheck whether project opencode.json overrides the global file, then restart OpenCode.

Advanced setup

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.