Skip to content

OpenCode Provider: Change Base URL and API Key

Updated · TokenomixAI

Changing the OpenCode provider means editing one object in opencode.json: add a provider entry with a base URL and API key, then pick a model. With an OpenAI-compatible gateway, one API key opens many text and code models instead of one key per tool. This page shows how it works, what to check, and how to roll back.

One API key and one OpenAI-compatible base URL are enough to move OpenCode between models; only the provider entry in opencode.json changes.

How an OpenCode provider entry and base URL work

OpenCode doesn't ship with a fixed model list. When a session starts, it reads opencode.json, takes the provider entry you selected, and sends chat requests to that entry's baseURL in the OpenAI chat completions format. The apiKey in the same entry is used as the credential.

That's where a multi-model gateway fits. One base URL holds many text and code models, and the model name travels in the request body rather than in a piece of the URL. The tool doesn't need to know which model sits behind the gateway; it only needs a compatible base URL and a valid key.

So switching providers means changing two values, the base URL and the API key, plus adjusting the model name. Shortcuts, plugins and workflows in OpenCode stay where they are. For OpenAI-compatible clients the base URL is https://api.tokenomixai.com/v1.

One exception trips people up: Claude Code uses https://api.tokenomixai.com without /v1, because Claude Code appends /v1/messages itself. Copy an OpenCode base URL straight into Claude Code and the path doubles, so the request fails. The key format itself is covered in OpenAI Compatible API Key.

Add a new OpenCode provider in opencode.json

The provider entry in opencode.json is an object, not an array. Adding a provider means adding one key to the provider object, and the official installer uses the name tokenomixai for it. Each entry carries npm set to @ai-sdk/openai-compatible and an options block with baseURL and apiKey.

{
  "provider": {
    "tokenomixai": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "https://api.tokenomixai.com/v1",
        "apiKey": "sk-tkx-..."
      }
    }
  }
}

The file you edit lives at ~/.config/opencode/opencode.json on macOS and Linux, or %USERPROFILE%\.config\opencode\opencode.json on Windows. If the file already holds other providers, MCP servers or plugins, add the new entry and leave the existing objects in place.

Before editing by hand, copy the file to another name so you can undo the change. The installer makes a backup first, so you can skip that step when you use it. The list of models you can select lives in docs; don't guess a model ID.

Change the OpenCode provider with one command

If you'd rather not edit JSON yourself, the one-command installer adds the tokenomixai provider to the provider object, stores the key, and leaves other providers, MCP and plugins untouched. The script asks for the key in the terminal, and the key never goes into a URL.

On macOS or Linux:

bash <(curl -fsSL https://tokenomixai.com/install/opencode.sh)

On Windows PowerShell:

irm https://tokenomixai.com/install/opencode.ps1 | iex

When the script finishes, open OpenCode and pick a model from the list. If this is your first session, the walkthrough from opening a project to reading a diff is in How to Use OpenCode.

How to roll back to your old provider

Restore puts opencode.json back to the state it was in before the installer ran, including the provider you used before. It runs the same script with an extra flag.

macOS and Linux:

bash <(curl -fsSL https://tokenomixai.com/install/opencode.sh) --restore

Windows PowerShell:

$env:RESTORE="1"; irm https://tokenomixai.com/install/opencode.ps1 | iex

Claude Code follows the same pattern, only the script name differs: claude.sh or claude.ps1. If you edited the JSON by hand without making a copy, restore can't help and you'll have to rebuild the provider entry from your own notes.

Check quota, 429 and 401 after switching

After the switch, check remaining quota before a long session. The quota endpoint returns status, remainingTokens, maxTokens, usagePercent, expiresAt, and low, which is true at 10 percent or less.

curl https://api.tokenomixai.com/v1/quota -H "Authorization: Bearer sk-tkx-..."

Quota is counted in tokens, not time windows, so there is no 5-hour limit locking the key. The key keeps working while tokens remain and before expiresAt. What does exist is a rate limit of 600 requests per minute per account, shared by all keys on that account; above it the API returns 429 with a Retry-After header.

A 401 after switching usually means the key was pasted wrong or the Authorization header isn't being sent. Check the base URL too: OpenAI-compatible clients need /v1, while Claude Code must not have it. One key works across several tools that accept a base URL, so you don't need a separate key per tool.

When tokens run out, a top-up adds new tokens to what's left, and validity follows the latest top-up. The key stays the same, so there's nothing to reconfigure in OpenCode or Claude Code. The gateway logs only request metadata such as model, token count, time and status; prompts and completions are not stored.

Other tools that take the same base URL

The same pattern applies in any tool that lets you set a base URL. Only the field name changes; the values are still a base URL and an API key.

ToolWhat you fill in
OpenCodeprovider entry in opencode.json: baseURL and apiKey
Claude CodeANTHROPIC_BASE_URL without /v1, plus ANTHROPIC_AUTH_TOKEN
Cursor, Cline, n8nbase URL https://api.tokenomixai.com/v1 and the key in the tool's provider settings
OpenAI SDK scripts or curlthe same base URL, key in the Authorization header

For Claude Code, the full walkthrough for ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN is in Claude Code Different Model. Remember that the value has no /v1 in it.

Frequently asked questions

Does each model need its own API key?

No. One key covers many text and code models, and the model is chosen by name in the request, not by a different key. Which models are available lives on the docs page because the list changes.

Why is the Claude Code base URL different from OpenCode's?

Claude Code appends /v1/messages to the ANTHROPIC_BASE_URL value itself. If you write https://api.tokenomixai.com/v1 there, the final path doubles and the request fails. OpenAI-compatible clients are the reverse: /v1 has to be present.

Will changing the provider delete my MCP servers and plugins?

The installer only adds or replaces the provider entry in the provider object, and it makes a backup first. MCP and plugin configuration in the same file is left alone, and restore returns the file to its earlier state.

Are my prompts stored at the gateway?

No. Only request metadata is recorded: model, token count, time and status. Prompt and completion content is not stored.

Can one key run in several tools at once?

Yes, as long as each tool lets you set a base URL. The 600 requests per minute rate limit is counted per account and shared across all its keys, so watch usage if you run several agents in parallel.

More articles