OpenCode setup guide
Three-step setup, default models for each plan, and allowances for open-weight models.
Custom endpoints in OpenCode depend on which major version you run. This guide covers opencode.json baseURL syntax, API key storage, and how OpenCode Zen compares to a fixed-allowance provider.
OpenCode separates the coding agent from the model vendor, so pointing it at a custom gateway, local server, or multi-model API platform only takes a provider block in opencode.json (or opencode.jsonc) and an API key. The main catch is that OpenCode 1.x and OpenCode 2.x use different field names for custom providers and store environment keys differently.
Quick reference: On OpenCode 1.x, set provider.<id>.options.baseURL and options.apiKey: "{env:VAR}" (or authenticate with opencode auth login). On OpenCode 2.x, use providers.<id>.settings.baseURL with env: ["VAR"] and store the secret via opencode service set env. Always include /v1 without a trailing slash when the gateway expects it. If you are deciding between an OpenCode Zen API key and a custom provider, Zen bills pay-as-you-go credits, while CodingPass plugs in as a custom Base URL starting at $10 a month ($60 monthly allowance, metered at official API prices).
OpenCode loads global configuration from ~/.config/opencode/opencode.json (or opencode.jsonc) and merges project-level overrides from opencode.json in your repository root. Before editing the file, run opencode --version to confirm whether you are on 1.x or 2.x.
provider + options.baseURL)OpenCode 1.x uses a singular provider object, an npm adapter package, and nested options for baseURL and apiKey. Below is a minimal working config that connects CodingPass over the Anthropic-compatible adapter for Claude and open-weight models:
{ "$schema": "https://opencode.ai/config.json", "provider": { "codingpass": { "npm": "@ai-sdk/anthropic", "name": "CodingPass", "options": { "baseURL": "https://codingpass.ai/v1", "apiKey": "{env:CODINGPASS_API_KEY}" }, "models": { "claude-sonnet-5-5": { "name": "Claude Sonnet 5.5" }, "claude-opus-5-5": { "name": "Claude Opus 5.5" }, "deepseek-v4.1-flash": { "name": "DeepSeek V4.1 Flash" } } } }}For an OpenAI-compatible gateway or local server (such as Ollama or LM Studio), swap npm to "@ai-sdk/openai-compatible" and point baseURL at that server's /v1 root (for example, http://localhost:11434/v1).
providers + settings.baseURL)OpenCode 2.x renamed the top-level key to providers (plural), replaced npm with package, moved baseURL under settings, and declared environment variable names in an env array:
{ "$schema": "https://opencode.ai/config.json", "model": "codingpass/claude-sonnet-5-5", "providers": { "codingpass": { "name": "CodingPass", "env": ["CODINGPASS_API_KEY"], "package": "@opencode/ai/providers/anthropic", "settings": { "baseURL": "https://codingpass.ai/v1" }, "models": { "claude-sonnet-5-5": { "name": "Claude Sonnet 5.5" }, "claude-opus-5-5": { "name": "Claude Opus 5.5" }, "deepseek-v4.1-flash": { "name": "DeepSeek V4.1 Flash" } } }, "codingpass-openai": { "name": "CodingPass (OpenAI)", "env": ["CODINGPASS_API_KEY"], "package": "@opencode/ai/providers/openai-compatible/responses", "settings": { "baseURL": "https://codingpass.ai/v1" }, "models": { "gpt-6.2-sol": { "name": "GPT-6.2 Sol" }, "gpt-6.1-terra": { "name": "GPT-6.1 Terra" } } } }}baseURL pitfallsMost connection errors after adding a custom provider come down to three URL and protocol details:
/chat/completions, /responses, or /messages to baseURL. The underlying AI SDK appends those paths automatically based on the selected adapter/v1 when required, without a trailing slash: Use https://codingpass.ai/v1 rather than https://codingpass.ai (which returns 404) or https://codingpass.ai/v1/ (a trailing slash can trigger double-slash path mismatches on strict proxies)@ai-sdk/anthropic (v1) and @opencode/ai/providers/anthropic (v2) speak the Anthropic Messages API, preserving prompt caching and extended thinking for Claude models. @ai-sdk/openai-compatible (v1) and @opencode/ai/providers/openai-compatible/responses (v2) target OpenAI-style endpoints. On certain older OpenCode 1.x builds, custom @ai-sdk/anthropic blocks can drop credentials unless options.apiKey is explicitly set; if a third-party gateway still fails authentication on v1, verify options.apiKey or test the OpenAI-compatible adapterOpenCode supports three ways to store API keys. Choose the method that matches your OpenCode version and whether you share configs across machines.
opencode auth login and /connect)For built-in providers (Anthropic, OpenAI, OpenCode Zen, and others), run opencode auth login in your terminal or /connect inside the TUI. When a vendor supports both OAuth and API keys, pass --method key to enter an API key directly:
opencode auth login anthropic --method keyOpenCode writes the credentials to ~/.local/share/opencode/auth.json on macOS and Linux, or C:\Users\<username>\.local\share\opencode\auth.json on Windows.
{env:VAR_NAME})For custom providers in opencode.json, referencing an environment variable keeps secrets out of dotfiles and Git repositories. Export the variable in your shell profile (~/.zshrc or ~/.bashrc) before launching OpenCode 1.x:
export CODINGPASS_API_KEY="cp-live-…"opencode service set env)OpenCode 2 runs a single local background service shared by the terminal UI, desktop app, and web interface. Setting the key directly on that service ensures every client reads the same credential without extra shell configuration:
opencode service set env CODINGPASS_API_KEY cp-live-…Restart OpenCode (or reload config), run /models, pick a model under your custom provider, and send a short prompt:
opencode/modelsIf the model does not appear in /models, check that you used providers on v2 (or provider on v1) and that the JSON has no syntax errors. If requests return 401, confirm the environment variable is exported in the current shell or registered with opencode service set env. If requests return 404, check that baseURL includes /v1 and that the model ID matches the provider catalog—for CodingPass, check the Models page.
When setting up OpenCode for the first time, developers often encounter OpenCode Zen alongside Bring Your Own Key (BYOK) and custom providers. They solve different billing problems inside the same OpenCode client.
OpenCode Zen is OpenCode's hosted pay-as-you-go gateway, managed through the OpenCode Console and routed via https://opencode.ai/zen/v1/. You sign in, load prepaid credits (with optional auto-reload when your balance drops below a threshold), generate a Zen API key, and connect it via /connect → OpenCode Zen (or OPENCODE_API_KEY). Models appear under the opencode/<model-id> namespace and bill per token from your credit balance, alongside a rotating set of free models.
Direct vendor BYOK connects your own Anthropic, OpenAI, or Gemini API keys directly to each vendor. OpenCode charges nothing; you pay each vendor's standard per-token API rates with separate dashboards and billing alerts.
CodingPass connects as a custom provider over a single baseURL (https://codingpass.ai/v1) and one API key, backed by a fixed monthly coding allowance. The Go plan starts at $10 a month for a $60 monthly pool metered at official API prices, with transparent caps ($30 weekly, $10 per 5-hour window) and hard 429 stops so an agent loop cannot run up unexpected charges. It covers Claude Sonnet 5.5, Opus 5.5, GPT, and open-weight models (up to $40 each on Go) under one key. See Usage limits and Pricing for full tier rules.
| Dimension | OpenCode Zen | Direct Vendor BYOK | CodingPass |
|---|---|---|---|
| Billing model | Pay-as-you-go prepaid credits (optional auto-reload) | Pay-as-you-go per vendor | Fixed monthly plan from $10 ($60 pool on Go) |
| Setup in OpenCode | /connect → OpenCode Zen or OPENCODE_API_KEY | opencode auth login or vendor env vars | Custom baseURL in opencode.json + one key |
| Endpoint | https://opencode.ai/zen/v1/ | Each vendor's official API host | https://codingpass.ai/v1 |
| Multi-model access | Curated frontier and free models under one balance | One key and bill per vendor | Claude, GPT, Gemini, Grok, and open-weight models on one key |
| Cost protection | Prepaid balance; watch auto-reload settings | Manual vendor spend limits | Hard 5-hour, weekly, and monthly caps; no overage charges |
| Workload or scenario | Recommended setup | Reason and trade-off |
|---|---|---|
| Occasional coding or testing models with zero monthly commitment | OpenCode Zen API key | Prepaid credits with no recurring subscription; heavy agent sessions can deplete credits quickly if auto-reload is on |
| Daily coding with Claude Sonnet 5.5, Opus 5.5, and open-weight models on a fixed budget | CodingPass custom provider | One Base URL and key, monthly allowance metered at official API prices, no surprise overage bills |
| Company compliance requiring direct vendor contracts | Direct vendor BYOK (opencode auth login) | Requests go straight to your existing Anthropic or OpenAI account; requires managing separate keys and budgets |
| Offline work, strict local privacy, or internal proxy | Custom @ai-sdk/openai-compatible provider | Point baseURL at Ollama, LM Studio, or an internal gateway; limited by local GPU speed and model quality |
provider became providers, npm became package, options became settings, and keys are passed via env plus opencode service set env. Updating those keys in opencode.jsonc restores the provider in /models./models./v1 and omit the trailing slash (for example, https://codingpass.ai/v1). Never append endpoint paths like /chat/completions or /messages, because the SDK adds those automatically.One custom Base URL and key puts Claude, GPT and open-weight models in /models. No surprise charges, cancel anytime.