← Blog
OpenCodeIntegrationCoding agents

OpenCode Setup with Claude and GPT via a Custom API Key

(prices in the text follow the live price list)

Short answer

To use OpenCode with APIVAI, run /connect, choose Other, enter the provider ID apivai and paste your APIVAI key, then add an apivai provider to opencode.json with "npm": "@ai-sdk/openai-compatible" and "baseURL": "https://api.apivai.com/v1", list model IDs such as claude-sonnet-4-6 and gpt-5.5, and pick apivai/claude-sonnet-4-6 with /models. For Claude in its native format you can instead point the built-in anthropic provider at https://api.apivai.com/v1; in OpenCode this URL keeps the /v1, because the Anthropic SDK it uses only adds /messages. As of 2026-10-10, Claude Sonnet 4.6 costs $1.15 / $5.74 per million input / output tokens through APIVAI, against the official $3.00 / $15.00. You pay per token from a prepaid balance, with no subscription.

What is OpenCode, and why connect your own API?

OpenCode is an open-source AI coding agent that runs in your terminal. It reads your project, edits files, runs commands and works through a task step by step. OpenCode is built on the AI SDK and can talk to many providers, including any OpenAI-compatible API, so you choose where the model requests go and who bills them.

APIVAI gives you one key for Claude and GPT models, billed per token below official list prices for most models, from a balance you top up when you need to. One key works in both the OpenAI format and the Anthropic format, so it fits both of the setups below.

Which provider setup should you use in OpenCode?

Custom provider (OpenAI-compatible)Built-in anthropic provider
npm package@ai-sdk/openai-compatiblebuilt in (Anthropic SDK)
baseURLhttps://api.apivai.com/v1https://api.apivai.com/v1
ModelsClaude and GPTClaude only
What OpenCode calls/v1/chat/completions/v1/messages
Best forOne provider for every modelClaude in its native Messages format

Start with the custom provider: it covers Claude and GPT, and you control exactly which model IDs appear in the picker. Use the built-in anthropic provider if you only use Claude and want OpenCode's native Claude integration. Keep in mind that overriding anthropic sends all requests for that provider to APIVAI.

How do you set up OpenCode with APIVAI step by step?

Step 1: Create an APIVAI key

  1. Sign up at apivai.com with your email.
  2. Top up your balance from $10 by card, crypto, Alipay or WeChat Pay. There is no free trial; you pay only for what you use.
  3. Open the Dashboard, create a key and copy it. You can give the key its own budget limit, so the agent can never spend more than you planned.

Step 2: Store the key with /connect

  1. Start OpenCode in your project and type /connect.
  2. Scroll down to Other and select it.
  3. Enter the provider ID apivai. You will use exactly this ID in the config file.
  4. Paste your APIVAI key when asked for the API key.

OpenCode stores the key in ~/.local/share/opencode/auth.json. The same flow is available outside the TUI as opencode auth login. If you prefer an environment variable, skip this step and add "apiKey": "{env:APIVAI_API_KEY}" to the provider's options below.

Step 3: Add the provider to opencode.json

Put this in opencode.json in your project root, or in ~/.config/opencode/opencode.json to use it in every project:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "apivai": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "APIVAI",
      "options": {
        "baseURL": "https://api.apivai.com/v1"
      },
      "models": {
        "claude-sonnet-4-6": {
          "name": "Claude Sonnet 4.6",
          "limit": { "context": 200000, "output": 16384 }
        },
        "claude-haiku-4-5": {
          "name": "Claude Haiku 4.5",
          "limit": { "context": 200000, "output": 16384 }
        },
        "gpt-5.5": {
          "name": "GPT-5.5",
          "limit": { "context": 128000, "output": 16384 }
        }
      }
    }
  },
  "model": "apivai/claude-sonnet-4-6",
  "small_model": "apivai/claude-haiku-4-5"
}
KeyValue
provider IDapivai (the same ID you entered in /connect)
npm@ai-sdk/openai-compatible
options.baseURLhttps://api.apivai.com/v1
models keysExact model IDs, e.g. claude-sonnet-4-6
limit.output8192 or more (at least 4096)
modelapivai/claude-sonnet-4-6

The baseURL must end in /v1 exactly once: the package adds /chat/completions to it. The keys under models are the IDs sent to APIVAI, and name is only the label in the picker. limit tells OpenCode how much context is left and how long a reply may be. small_model is used for light jobs such as generating session titles, so a cheap model fits there.

To see which model IDs your key can use, run:

curl https://api.apivai.com/v1/models \
  -H "Authorization: Bearer YOUR_APIVAI_KEY"

The model list changes over time, so copy IDs from this response or from the pricing page rather than from old tutorials. Restart OpenCode, run /models and choose a model under APIVAI.

Step 4 (optional): Use the built-in Anthropic provider for Claude

  1. Run /connect, select Anthropic and choose Manually enter API Key, then paste your APIVAI key.
  2. Add the base URL to opencode.json:
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "anthropic": {
      "options": {
        "baseURL": "https://api.apivai.com/v1"
      }
    }
  }
}

Here the /v1 stays. This is the opposite of tools like Claude Code: the Anthropic SDK in OpenCode treats baseURL as the prefix that already includes /v1 (its default is https://api.anthropic.com/v1) and only appends /messages. Entering https://api.apivai.com without /v1 would call /messages and fail with 404. Choose Claude models from the Anthropic list in /models; if an ID you want is missing, add it under provider.anthropic.models. More about this format is on the Claude API proxy page.

Which model should you choose in OpenCode?

A coding agent sends many requests per task, so the price per token matters. Prices per 1M input / output tokens as of 2026-10-10:

Model IDPrice (in / out)Use it for
claude-sonnet-4-6$1.15 / $5.74Default for everyday coding
claude-sonnet-5-5$0.77 / $3.82Everyday coding, newer Sonnet
claude-opus-5-5$1.54 / $7.65Hard bugs, large refactors
claude-haiku-4-5$0.38 / $1.92small_model, quick edits
gpt-5.5$0.77 / $4.56GPT alternative, second opinion
gpt-6-luna$0.0144 / $0.0768Very cheap simple tasks

A practical setup: Claude Sonnet 4.6 or Claude Sonnet 5.5 as model, Claude Haiku 4.5 as small_model, and Claude Opus 5.5 when a task keeps failing. GPT models only work through the custom OpenAI-compatible provider.

How much does OpenCode cost with APIVAI?

Each step of a task resends the session and the files the agent has read, so input tokens add up quickly. Two realistic examples with Claude Sonnet 4.6:

  • One medium task, such as adding a feature across a few files: about 25 requests of 30,000 input and 2,000 output tokens each. Cost: $1.15 with APIVAI, $3.00 at official prices.
  • A working month of three such tasks a day for 22 days (1,650 requests): $75.87 with APIVAI, $198 at official prices.

The same month on Claude Haiku 4.5 would cost $25.15. These estimates ignore prompt caching and count thinking tokens in the output figure, since thinking is billed as output. The Dashboard lists every request with its tokens and cost, so after a day you can replace these assumptions with your own numbers. For a wider comparison, see Claude API pricing in 2026.

How do you fix common OpenCode errors?

  • 401 Unauthorized: run opencode auth list and check that a credential exists for apivai. The ID in /connect must match the provider ID in opencode.json exactly. Also check that the key is not deleted or out of budget.
  • 404 Not Found: check options.baseURL. Both setups use https://api.apivai.com/v1, with no /chat/completions or /messages at the end and no doubled /v1.
  • Model not found: the key under models must be an exact ID such as claude-sonnet-4-6. Check it against GET /v1/models.
  • Provider or models missing in /models: the npm value must be @ai-sdk/openai-compatible, and the JSON must be valid. Restart OpenCode after editing the file.
  • Empty or cut-off replies: thinking is on by default and uses output tokens. Raise limit.output to 8192 or more (at least 4096).
  • 429 Too Many Requests: the default limit is 60 requests per minute per key. Long agent runs can hit it; wait a moment or ask support to raise it.

The general format rules are on the OpenAI-compatible API page and in the docs.

FAQ

Can I use Claude and GPT in OpenCode with one key?

Yes. List both model IDs under the apivai provider and switch between them with /models.

Which baseURL do I enter in OpenCode?

https://api.apivai.com/v1 in both setups. The OpenAI-compatible package adds /chat/completions and the Anthropic SDK adds /messages to it.

Where should opencode.json go?

In the project root for one project, or in ~/.config/opencode/opencode.json for all projects. Project config takes precedence over the global file.

Does APIVAI store my code?

No. The content of requests and responses is not logged. Only usage data such as model, token counts and cost is kept for billing.

Can I use the same key in other tools?

Yes. The same key works in Aider, Claude Code and editors such as Zed. A separate key per tool keeps the Dashboard easy to read.

Create an account, top up from $10 and paste your key into OpenCode.

Ready to start?

Get your API key in 30 seconds. Pay as you go for Claude and GPT.

Get Started