OpenCode

Add Merge Gateway as a provider in OpenCode to use any model with a single API key

OpenCode is an open-source terminal coding agent. Add Merge Gateway as a provider and OpenCode can use every model Gateway supports through a single API key, with routing policies, cost governance, and full request observability in the Gateway dashboard.

Before you start

  • Install OpenCode: npm i -g opencode-ai or brew install anomalyco/tap/opencode

  • Grab an API key from gateway.merge.dev/settings/api-keys and export it:

    $export MERGE_GATEWAY_API_KEY="mg_your_key"
  • Decide which models fit your workflow. Gateway model names take the form provider/model, for example anthropic/claude-sonnet-4-6 for heavy coding or google/gemini-2.5-flash for fast, low-cost edits. Browse the full catalog with GET /v1/models or in the dashboard.

Configure OpenCode

Merge Gateway is a registered provider in OpenCode’s model registry, so there’s no opencode.json to write to get started.

1

Connect Merge Gateway

Run opencode in your project, then /connect and select Merge Gateway. Paste your API key when prompted, or skip the prompt if you already exported MERGE_GATEWAY_API_KEY.

2

Pick a model

Run /models and select a Merge Gateway model. The full Gateway catalog comes from the registry, so the list populates automatically.

3

Send a test message

Ask OpenCode to make a small code change. Streaming, tool calls, and file edits all work, and the request shows up in your Gateway dashboard within a few seconds.

Caveats

The registry gives you the full catalog. To expose a curated subset, rename models in the picker, or override the base URL, add a merge-gateway block to opencode.json in your project root (or ~/.config/opencode/opencode.json):

1{
2 "$schema": "https://opencode.ai/config.json",
3 "provider": {
4 "merge-gateway": {
5 "models": {
6 "anthropic/claude-sonnet-4-6": { "name": "Claude Sonnet 4.6" },
7 "google/gemini-2.5-flash": { "name": "Gemini 2.5 Flash" }
8 }
9 }
10 }
11}

These two cover a common pair: a high-capability model for hard problems, and a fast, low-cost model for quick edits. Each key under models is a Gateway provider/model name from GET /v1/models. Because the provider comes from the registry, you don’t need the npm or name fields, and your exported MERGE_GATEWAY_API_KEY is picked up automatically. To route through a different host, add an options.baseURL.

Instead of hard-picking a model, point at a routing policy and let Gateway select the vendor and model per request based on your rules (cost, performance, or prompt complexity). Useful when you want cheap models for simple edits and stronger models for hard problems without switching by hand.

The export MERGE_GATEWAY_API_KEY=... above lives in your shell, so it applies only to that terminal. To persist it, add the same line to your shell profile (~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish). The key you paste through /connect is saved by OpenCode, so it persists on its own. If you add an opencode.json for model overrides, commit it to your project so it persists across sessions too.

OpenCode is agentic and relies on tool calls to read and edit files. Tool calling works through Gateway for any model whose capabilities.supports_tool_calling is true on the chosen vendor route. See Tool calling.

Any model Gateway supports. Use GET /v1/models to list them, or open the Gateway dashboard and copy the model identifier.

Next steps