# opencode (/docs/code-cli/opencode)

[opencode](https://opencode.ai) takes OhMyGPT as a provider through its standard OpenAI-compatible package. One block in `opencode.json` gives opencode Claude, GPT, GLM and the rest of the catalog on your OhMyGPT key, and you can test the setup for free with `ohmygpt/mock`.

## Configure [#configure]

Add the provider to `~/.config/opencode/opencode.json` for all projects, or to `opencode.json` in a project root for that project only:

```json title="~/.config/opencode/opencode.json"
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "ohmygpt": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "OhMyGPT",
      "options": {
        "baseURL": "https://api.ohmygpt.com/v1",
        "apiKey": "{env:OHMYGPT_API_KEY}"
      },
      "models": {
        "anthropic/claude-sonnet-5-5": {
          "limit": { "context": 1000000, "output": 128000 }
        },
        "anthropic/claude-haiku-5-5": {
          "limit": { "context": 1000000, "output": 128000 }
        }
      }
    }
  },
  "model": "ohmygpt/anthropic/claude-sonnet-5-5",
  "small_model": "ohmygpt/anthropic/claude-haiku-5-5"
}
```

- `baseURL` ends in `/v1`. The package appends `/chat/completions`.
- `{env:OHMYGPT_API_KEY}` reads your key from the environment, so the file holds no secret.
- Each key under `models` is an OhMyGPT model ID. `limit` tells opencode when to compact the conversation; [Choose a model](#choose-a-model) shows where the numbers come from.
- `model` and `small_model` take the form `<provider key>/<model ID>`. opencode uses `small_model` for light tasks such as session titles.

Export the key and start opencode:

```bash
export OHMYGPT_API_KEY="sk-..."
opencode
```

Run `/models` to switch between the models you listed.

## Check the connection [#check-the-connection]

opencode sends Chat Completions requests. Send one to the free test model `ohmygpt/mock`:

```bash
curl https://api.ohmygpt.com/v1/chat/completions \
  -H "Authorization: Bearer $OHMYGPT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "ohmygpt/mock", "messages": [{"role": "user", "content": "ping"}]}'
```

```json
{
  "id": "chatcmpl-ohmygpt-mock",
  "object": "chat.completion",
  "model": "mock",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "This is a mock reply from OhMyGPT. Your integration works; switch to a real model when ready."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": { "prompt_tokens": 15, "completion_tokens": 17, "total_tokens": 32 }
}
```

To try opencode itself at no cost, add `"ohmygpt/mock": {}` under `models` and select it with `/models`. The mock calls the first tool opencode declares, then answers with text once it gets the result. The mock is available once your account has been topped up.

## Choose a model [#choose-a-model]

Every chat model works with opencode. List them with the context and output sizes to put in `limit`:

```bash
curl -s https://api.ohmygpt.com/v1/models \
  | jq -r '.data[] | select(.available and any(.protocols[]?; .id == "openai-completions"))
           | "\(.id)\tcontext=\(.context_length)\toutput=\(.limits.max_completion_tokens)"'
```

```text
anthropic/claude-sonnet-5-5	context=1000000	output=128000
openai/gpt-5.5	context=1050000	output=128000
zhipu/glm-5.3	context=1048576	output=131072
...
```

Prices for each are on the [Models](https://www.ohmygpt.com/models) page.

## Troubleshooting [#troubleshooting]

| Symptom | Cause | Fix |
|---|---|---|
| `401` with `Invalid API key` | `OHMYGPT_API_KEY` is not set in the shell that runs opencode, so `{env:...}` is empty | Export the variable and restart opencode |
| `404` on every request | `baseURL` is missing `/v1` | Set it to `https://api.ohmygpt.com/v1` |
| `404` saying the model does not exist | The key under `models` is not an OhMyGPT ID | Copy the ID from `GET /v1/models` |
| The provider is missing from `/models` | opencode did not load the file, or the JSON is invalid | Check the path and validate the JSON |
| `402` | The balance or the key's spending limit is used up | Top up at [Billing](https://www.ohmygpt.com/billing) |

Every response carries an `X-Request-ID` header, and [Usage](https://www.ohmygpt.com/usage) shows what each request cost.
