OpenCode
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-aiorbrew install anomalyco/tap/opencode -
Grab an API key from gateway.merge.dev/api-keys (an organization key, or a project API key to scope OpenCode usage to a project) and export it:
-
OpenCode downloads the Merge Gateway provider package from the npm registry the first time you send a message, so the machine needs access to
registry.npmjs.org. If your network uses a proxy or a private npm registry, see the troubleshooting entry under Caveats. -
Decide which models fit your workflow. Gateway model names take the form
provider/model, for exampleanthropic/claude-sonnet-4-6for heavy coding orgoogle/gemini-2.5-flashfor fast, low-cost edits. Browse the full catalog withGET /v1/modelsor 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.
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.
Pick a model
Run /models and select a Merge Gateway model. The full Gateway catalog comes from the registry, so the list populates automatically.
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
Pin or rename models with opencode.json
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):
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.
Let Gateway choose the model
Instead of hard-picking a model, add a default_routing entry to the models block in opencode.json (for example "default_routing": { "name": "Merge routing" }) and select it with /models. Gateway hands those requests to your routing policy, which picks the vendor and model per request based on your rules (cost, performance, or prompt complexity). The policy comes from the key’s project (project API key) or the org default.
Persisting the configuration
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.
Fix "Failed to initialize provider: merge-gateway"
OpenCode downloads the Merge Gateway provider package (merge-gateway-ai-sdk-provider) from the npm registry the first time you send a message, then caches it in ~/.cache/opencode/packages. This error means that download failed on your machine, before any request reached Gateway, which is why reconnecting or generating a new API key doesn’t help. Common causes are a corporate proxy or VPN, a private npm registry that hasn’t approved the package, expired registry credentials, or a firewall blocking registry.npmjs.org.
The fastest fix is to seed OpenCode’s package cache from your terminal. OpenCode reuses an existing cache entry and skips its own installer, so this resolves the error whenever your shell has proxy or registry settings that OpenCode doesn’t inherit (common when OpenCode is launched outside the terminal):
If that install succeeds, send a message again and you’re done. If it fails, its error output names the real blocker, because it exercises the same download path OpenCode uses. Check npm config list for registry, proxy, or https-proxy overrides you don’t expect. If your organization runs a private registry, it needs to allow both merge-gateway-ai-sdk-provider and its dependencies. After fixing the npm configuration, delete ~/.cache/opencode/packages and send a message again.
If you can’t change the npm setup, use this provider block instead. It runs on OpenCode’s built-in OpenAI-compatible provider against Gateway’s /v1 endpoint, so nothing is downloaded. Chat, tool calls, and image input all work; the trade-off is that Gateway-specific response detail (for example cache token breakdowns) isn’t surfaced, and you list models yourself instead of getting the full catalog:
Tool calling
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.
Which models can I use?
Any model Gateway supports. Use GET /v1/models to list them, or open the Gateway dashboard and copy the model identifier.