OpenAI-compatibleTerminal UI

OpenCode + MegaBrain Gateway

OpenCode is a terminal-based AI coding assistant built by the SST team. Connect it to MegaBrain Gateway to unlock 500+ models — without changing your existing terminal workflow.

Why MegaBrain?

500+ models

Claude, GPT, Gemini, Llama, Mistral and more — all through one API key.

No markup

Pay provider prices. No per-seat fees on top of what the model costs.

Works with your existing shell

No new CLI to learn. Drop MegaBrain into the provider config OpenCode already reads.

Prerequisites

Setup

1

Install OpenCode

Install via npm (requires Node 18+):

npm install -g opencode-ai

Or via Homebrew on macOS:

brew install opencode

Verify the install: opencode --version

2

Open the config

Run the built-in config command to open the file in your editor:

opencode config

Or edit the file directly at ~/.config/opencode/config.json.

3

Set MegaBrain as your provider

Replace the file contents with the following, substituting your API key:

{
  "provider": {
    "openai": {
      "name": "MegaBrain Gateway",
      "apiKey": "mb-your-api-key-here",
      "baseURL": "https://getmegabrain.com/api/gateway/v1"
    }
  },
  "model": "anthropic/claude-sonnet-4-6",
  "autoshare": false
}

The baseURL points to the MegaBrain OpenAI-compatible gateway. The model field accepts any model ID from our models list.

4

Start OpenCode

Navigate to your project directory and run:

opencode

OpenCode will open its terminal UI and route all inference through MegaBrain Gateway. You can switch models at runtime using the model picker inside the UI (press m by default).

How OpenCode works

Terminal UI

A rich TUI rendered directly in your terminal — no browser or Electron required.

File context

Automatically indexes your project files and sends relevant context with each request.

Tool use (read / write / bash)

The model can read files, write changes, and run shell commands — all with your approval.

Inline diffs

Proposed edits are shown as diffs in the terminal. Accept or reject each change individually.

Session history

Conversations are persisted locally so you can resume past sessions or review what changed.

OpenAI-compatible

Uses the standard OpenAI chat completions API — any compatible provider works, including MegaBrain.

Recommended models

# Best for complex refactors and architecture
anthropic/claude-sonnet-4-6

# Fast iteration and smaller tasks
google/gemini-2.5-flash

# Strong reasoning, great at debugging
openai/o3-mini

# Balanced speed + quality
openai/gpt-4o

# Auto-select the best available frontier model
auto-frontier

Browse all available models on the Models page. Any model that supports tool use works best with OpenCode.

Troubleshooting

OpenCode says "invalid API key"

Double-check that apiKey in your config starts with mb- and matches the key shown in your profile. Keys are not shown again after creation — generate a new one if you lost it.

Model not found error

Make sure the model ID uses the provider-prefixed format (e.g. anthropic/claude-sonnet-4-6). Plain names like claude-3-5-sonnet will not resolve through the gateway. See the models list for exact IDs.

Config changes are not picked up

Quit OpenCode and relaunch — it reads config.json on startup only. Validate your JSON first with jq . ~/.config/opencode/config.json to rule out syntax errors.

Tool calls are not working

Tool use (file read/write, bash) requires a model that supports function calling. All Claude, GPT-4o, and Gemini 1.5+ models on MegaBrain support it. Avoid base completion models (...-instruct variants) for agentic tasks.