> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://respan.ai/docs/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://respan.ai/docs/_mcp/server.

# OpenCode (gateway)

> 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.

## Setup

#### Install OpenCode

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

#### Set environment variables

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

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

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

#### Run OpenCode

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

All LLM calls now route through Respan.

## 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.

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://api.respan.ai/api",
        "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"
```