# OpenCode Go/Zen Dual-Auth Setup

## The Problem

When using OpenCode's Go API (`https://opencode.ai/zen/go/v1`) or Zen API (`https://opencode.ai/zen/v1`) as a custom `@ai-sdk/openai-compatible` provider, the API key must be in **two places**:

1. `~/.local/share/opencode/auth.json` under the provider name
2. `~/.config/opencode/opencode.json` under `provider.<name>.options.apiKey`

Omitting either gives:
- **Missing API key** — auth.json entry missing
- **Invalid API key** — config's `options.apiKey` missing or wrong

## Why?

OpenCode's bundled `@ai-sdk/openai-compatible` package expects the credential in `auth.json` (indexed by provider name). However, the SDK also reads `options.apiKey` from the config to set the `Authorization` header. Both must be populated for the SDK to send a Bearer token the Go/Zen APIs will accept.

## Setup Steps

### 1. Populate `~/.local/share/opencode/auth.json`

```json
{
  "providers": {
    "opencode": { "apiKey": "sk-tx1..." },
    "opencode-go": { "apiKey": "sk-tx1..." },
    "opencode-zen": { "apiKey": "sk-tx1..." }
  }
}
```

All three entries use the **same API key** — the OpenCode Go key from `https://opencode.ai/auth`.

The `opencode` entry (built-in CLI provider) is created by `opencode providers login`. The `opencode-go` and `opencode-zen` entries must be added manually or via the TUI's `/connect` command.

### 2. Configure `~/.config/opencode/opencode.json`

Use the template at `templates/opencode-go-zen.json`. Key excerpt:

```json
{
  "opencode-go": {
    "npm": "@ai-sdk/openai-compatible",
    "options": {
      "baseURL": "https://opencode.ai/zen/go/v1",
      "apiKey": "sk-tx1..."
    },
    "models": { ... }
  }
}
```

### 3. Verify

```bash
# Both providers should show models
opencode models | grep "opencode-go/"
opencode models | grep "opencode-zen/"

# Smoke test
opencode run "say OK" --model opencode-go/qwen3.6-plus
opencode run "say OK" --model opencode-zen/deepseek-v4-flash-free
```

## Pitfalls

- **The key is masked in terminal output (`***`).** Use `read_file` or direct `python3 -c "import json; print(json.load(...))"` to verify the actual stored value.
- **The `opencode` built-in provider only exposes `big-pickle` for Go.** To access the other 13 Go models, use the `opencode-go` custom provider.
- **qwen3.6-plus-free** works through the built-in `opencode` provider but NOT through the Zen API directly.
