MegaBrain University · guide

OpenCode Custom Provider: Use Claude, GPT-6, Kimi and GLM via an OpenAI-Compatible API

Updated · 10 min setup

Short answer

To use OpenCode with MegaBrain, add a provider to opencode.json with "npm": "@ai-sdk/openai-compatible", options.baseURL https://getmegabrain.com/api/gateway/v1, options.apiKey: "{env:MEGABRAIN_API_KEY}" and the models you want, then set "model": "megabrain/anthropic/claude-sonnet-5.5". OpenCode then codes with Claude, GPT-6, Kimi, GLM or DeepSeek on one key.

Connection settings
Base URL (OpenAI-compatible)https://getmegabrain.com/api/gateway/v1
Base URL for Claude Code and the Anthropic SDKhttps://getmegabrain.com/api/anthropic
KeyAuthorization: Bearer <key> — the API key page in your dashboard
Default modelanthropic/claude-sonnet-5.5
Free model for testingmb-auto/free

What is OpenCode

OpenCode is an open-source AI coding agent. It runs in the terminal (TUI), as a desktop app and in IDEs, edits files, runs commands, uses subagents and MCP servers, and has a server mode that other apps (OpenWork, for one) build on.

OpenCode works with many providers, and any OpenAI-compatible API can be added as a custom provider. With MegaBrain you get every major coding model behind one key: Claude for hard refactors, GPT-6 as a second opinion, Kimi, GLM and DeepSeek for cheap high-volume edits.

Setup, step by step

  1. Step 1. Get a MegaBrain API key

    Sign up with GitHub, Google or email, add funds on the Balance page and create a key on the API key page. The key works for every text, image and video model, and each request is paid from your balance — no subscription.

    Put the key in an environment variable so it never lands in a config file you share:

    # ~/.zshrc or ~/.bashrc
    export MEGABRAIN_API_KEY="your-megabrain-key"
    curl https://getmegabrain.com/api/gateway/v1/chat/completions \
      -H "Authorization: Bearer $MEGABRAIN_API_KEY" \
      -H "Content-Type: application/json" \
      -d '{"model": "anthropic/claude-sonnet-5.5", "messages": [{"role": "user", "content": "Say OK"}]}'

    Check the key before you touch the agent. A JSON reply with choices means the key, the balance and the model all work:

  2. Step 2. Install OpenCode

    Any one of:

    curl -fsSL https://opencode.ai/v2/install | bash
    # or
    npm i -g @opencode/cli@latest
    # or (macOS)
    brew install opencode
  3. Step 3. Add MegaBrain to opencode.json

    Put this in ~/.config/opencode/opencode.json (all projects) or opencode.json in a project root. Only models listed under models can be selected:

    {
      "$schema": "https://opencode.ai/config.json",
      "model": "megabrain/anthropic/claude-sonnet-5.5",
      "provider": {
        "megabrain": {
          "npm": "@ai-sdk/openai-compatible",
          "name": "MegaBrain",
          "options": {
            "baseURL": "https://getmegabrain.com/api/gateway/v1",
            "apiKey": "{env:MEGABRAIN_API_KEY}"
          },
          "models": {
            "anthropic/claude-sonnet-5.5": {
              "name": "Claude Sonnet 5.5",
              "limit": { "context": 1000000, "output": 32000 }
            },
            "anthropic/claude-opus-5.5": {
              "name": "Claude Opus 5.5",
              "limit": { "context": 1000000, "output": 32000 }
            },
            "openai/gpt-6.1-sol": {
              "name": "GPT-6.1 Sol",
              "limit": { "context": 1050000, "output": 32000 }
            },
            "moonshotai/kimi-k3.1-pro": {
              "name": "Kimi K3.1 Pro",
              "limit": { "context": 1048576, "output": 32000 }
            },
            "z-ai/glm-5.3": {
              "name": "GLM 5.3",
              "limit": { "context": 1048576, "output": 32000 }
            }
          }
        }
      }
    }

    This is the classic (V1) config shape. OpenCode V2 documents a new shape (providers, package, settings.baseURL) but still reads this one and converts it in memory, and OpenWork and the Kilo CLI read the same file — so one config works everywhere.

  4. Step 4. Start OpenCode and pick the model

    Run opencode in your project. The default model comes from "model"; switch with /models (MegaBrain models are grouped under the provider name). Ask for something that touches files — “add a test for utils/date.ts” — to confirm tool calls work.

Which model to choose

Always set limit.context and limit.output: OpenCode uses them to decide when to compact the conversation. Claude Sonnet 5.5 is the default for coding; use Opus 5.5 for large refactors and Kimi, GLM or DeepSeek Flash for cheap repetitive edits.

Model IDWhen to use it
anthropic/claude-sonnet-5.5The default for an agent: reliable tool calls, holds long tasks, 1M-token context.
anthropic/claude-opus-5.5Hard multi-step work, code review and analysis where a mistake costs more than tokens.
openai/gpt-6.1-solOpenAI's flagship: the alternative to Claude for code and reasoning.
google/gemini-3.8-flashFast and cheap: long documents, spreadsheets, inbox triage.
moonshotai/kimi-k3.1-proA strong open-weight model for agents and code.
z-ai/glm-5.3Open-weight model for code and tools with good price-to-quality.
deepseek/deepseek-v4.1-flashHigh-volume routine work: classification, drafts, summaries.
mb-auto/freeFree, for checking your setup. 128K context, weaker at tool calls; do not send confidential data.

Every model with its price and context window is in the catalog. They all work with the same key.

What you can do

Subagents

Split work between agents — one plans, one codes, one reviews — and give each its own model from the same MegaBrain provider.

MCP servers

Connect databases, issue trackers and internal docs through MCP so the agent works with real project context.

Server mode

opencode serve exposes the agent over HTTP — the basis for internal tools and CI bots.

Terminal, desktop and IDE

The same config drives the TUI, the desktop app (brew install --cask opencode-desktop) and editor integrations.

Business use cases

Cheap bulk changes across a codebase

Renames, dependency upgrades and lint fixes across hundreds of files: run them on DeepSeek V4.1 Flash or GLM 5.3 and keep Claude for review.

Task for the agent:

Replace every use of moment.js in src/ with date-fns. Keep behaviour identical, update the tests, run npm test and fix failures. Commit nothing; list the changed files at the end.

Second-opinion code review

Ask a different lab’s model to review a diff — GPT-6.1 Sol reviewing Claude’s work catches different mistakes — without a second vendor account.

Internal tools by non-engineers

Ops and marketing build small scripts and dashboards with OpenCode on a capped balance, while engineering keeps its own keys.

Agents that generate media

Build features on MegaBrain’s image and video API with the same key the agent already uses — product photos, previews, thumbnails.

Troubleshooting

401 Unauthorized

The key is missing or mistyped, or the agent did not read the environment variable (restart the agent after export). Test the key with the curl command from step 1.

402 Payment Required

The balance is empty. Add funds on the Balance page, or switch to mb-auto/free to keep testing.

Model not found

The model ID must match the catalog exactly, including the lab prefix: anthropic/claude-sonnet-5.5, not claude-sonnet-5.5. The base URL must end in /v1.

The model is not in /models

Every model must have an entry under models. Check the provider key (megabrain) and that the JSON parses.

Long sessions fail with a context error

Set limit.context for the model, so OpenCode compacts the conversation before it overflows.

FAQ

Can OpenCode use Claude through a custom provider?

Yes. With MegaBrain as the provider, use megabrain/anthropic/claude-sonnet-5.5 or megabrain/anthropic/claude-opus-5.5 as the model. No Anthropic account is needed.

Should I use the V1 or V2 config shape?

Either. V2 accepts the V1 shape shown here and converts it, and V1 is also what OpenWork and Kilo read, so it is the portable choice.

How much does it cost?

You pay per token at each model’s price, from a prepaid balance in US dollars topped up by card. There is no subscription and no seat fee, so an agent that sits idle costs nothing. Prices per million tokens are in the model catalog; mb-auto/free costs nothing at all.

Do I need separate Anthropic, OpenAI or Google accounts?

No. One MegaBrain key reaches Claude, GPT, Gemini, Grok, Kimi, GLM, DeepSeek, Qwen and MiniMax. You switch models by changing the model ID, not the provider or the key.

Where do I keep the API key?

In the MEGABRAIN_API_KEY environment variable, referenced as {env:MEGABRAIN_API_KEY} — never commit the key into a project’s opencode.json.

MegaBrain · one API for every model

Claude, GPT, Gemini, Kimi, GLM and DeepSeek in any agent — one key, one balance.

Sign up, add funds by card and pay only for what your agents use. No subscription. Free models work without a key.

Sources

More MegaBrain University guides