> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openference.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenCode CLI

> Configure the open-source OpenCode terminal AI coding agent with Openference.

# OpenCode CLI

OpenCode is an open-source terminal AI coding agent. It works excellently with Openference.

## Add the provider

Create or edit `~/.config/opencode/opencode.json`:

```json theme={null}
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "openference": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Openference",
      "options": {
        "baseURL": "https://api.openference.com/v1",
        "apiKey": "{env:OPENFERENCE_API_KEY}"
      },
      "models": {
        "GLM-5.2": {
          "name": "GLM-5.2 (via Openference)",
          "limit": {
            "context": 850000,
            "output": 128000
          }
        }
      }
    }
  }
}
```

> **Important:** set `limit.context` and `limit.output` for every model you add.
> Without them, OpenCode assumes a small default context window and starts
> compacting the session around \~100k tokens — far below what models like
> GLM-5.2 (1M context) support. See [Context window values](#context-window-values) below.

## Authenticate

Export your key, then store credentials for the `openference` provider ID (must match the config key above):

```bash theme={null}
export OPENFERENCE_API_KEY="YOUR_API_KEY"
opencode auth login -p openference   # choose Other if prompted, provider ID: openference
```

Alternatively, run `/connect` in the TUI, scroll to **Other**, enter provider ID `openference`, and paste your key.

If you use `{env:OPENFERENCE_API_KEY}` in config (shown above), exporting the variable is enough — you do not need a separate auth step.

## Select a model

Start OpenCode and run `/models` to pick `openference/GLM-5.2` (or another model you listed).

```bash theme={null}
opencode
# inside the TUI: /models
```

## Verify flow

OpenCode connects by calling `GET /v1/models` and `POST /v1/chat/completions`. Openference returns an OpenAI-compatible model list filtered by key restrictions.

## Model configuration

Add only the models you use. The model keys **must match exactly** the IDs shown on the [Models page](https://openference.com/models) or returned by the API. `GLM-5.2` is a good default to start with.

## Context window values

Use these values in each model's `limit` block. The `context` value is the
safe **input** budget — the model's total context wall minus `output`, with a
small \~2% safety buffer. Models with a smaller output cap get a larger input
budget.

| Model             | `limit.context` | `limit.output` |
| ----------------- | --------------- | -------------- |
| GLM-5.2           | 850000          | 128000         |
| DeepSeek-V4-Pro   | 850000          | 131072         |
| DeepSeek-V4-Flash | 850000          | 131072         |
| Qwen3 Code        | 915000          | 65536          |
| Qwen3.7 Plus      | 915000          | 65536          |

`GET /v1/models` also advertises each model's `context_length` and
`max_output_tokens`, so you can always look up the current values:

```bash theme={null}
curl https://api.openference.com/v1/models \
  -H "Authorization: Bearer $OPENFERENCE_API_KEY"
```

## Notes

* Both `build` and `plan` agents work through Openference.
* You can switch agents with `Tab` and use `/undo` and `/redo` freely.
* See the [Compatibility Matrix](/integrations/compatibility-matrix) for an overview of all clients.
