---
title: "OpenCode (gateway)"
description: "Set up OpenCode with the Respan gateway."
---

Route OpenCode's LLM calls through the Respan gateway for automatic logging, caching, and model switching. Only your `RESPAN_API_KEY` is needed.

Paste this into your coding agent:

```text
Help me set up the Respan gateway with the OpenCode integration.
Follow https://www.respan.ai/docs/integrations/gateway/open-code.md
```

## Setup

<Steps>
<Step title="Install OpenCode">

```bash
npm install -g opencode-ai
```

</Step>

<Step title="Set environment variables">

Add to your shell profile (`.bashrc`, `.zshrc`, or PowerShell `$PROFILE`):

```bash
export OPENAI_BASE_URL="https://gateway.respan.ai"
export OPENAI_API_KEY="YOUR_RESPAN_API_KEY"
```

No separate OpenAI API key needed. The Respan gateway handles provider authentication.

</Step>

<Step title="Run OpenCode">

```bash
opencode run -m "openai/gpt-5.5" "your prompt"
```

All LLM calls now route through Respan.

</Step>
</Steps>

## Switch models

Change the `-m` flag to use 1000+ models from different providers through the same gateway.

```bash
opencode run -m "openai/gpt-5.5" "your prompt"
opencode run -m "openai/gpt-5-mini" "your prompt"
```

<Note>
OpenCode validates model IDs against its own catalog for the built-in `openai` provider, so only OpenAI model names work behind the `openai/` prefix. For Claude, Gemini, and other providers, add Respan as a custom provider as shown below.
</Note>

See the [full model list](https://platform.respan.ai/platform/models).

## Use any gateway model with a custom provider

Add a `respan` provider to `~/.config/opencode/opencode.json` (or `opencode.json` in your project). Any model ID the gateway exposes can be listed here:

```json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "respan": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Respan Gateway",
      "options": {
        "baseURL": "https://gateway.respan.ai",
        "apiKey": "{env:RESPAN_API_KEY}"
      },
      "models": {
        "gpt-4o": { "name": "GPT-4o" },
        "claude-sonnet-4-5-20250929": { "name": "Claude Sonnet 4.5" },
        "gemini/gemini-3.5-flash": { "name": "Gemini 3.5 Flash" }
      }
    }
  }
}
```

Export the key and run with the `respan/` prefix:

```bash
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
opencode run -m "respan/claude-sonnet-4-5-20250929" "your prompt"
opencode run -m "respan/gemini/gemini-3.5-flash" "your prompt"
```
