# OpenCode

OpenCode takes any provider that speaks OpenAI Chat Completions, so the Model API is one entry in `opencode.json`. Nothing to install besides OpenCode itself. The model is `auto`, which picks a model of the catalogue for the conversation.

Requirements: OpenCode (`opencode --version` shows the version), a Model API key and money on the [organization balance](https://console.samreshuuu.ru).

## 1. Create a key

In the [developer console](https://console.samreshuuu.ru), open API keys, keep **Model API key** in the **Access** field and create the key. The secret is shown once. Put it into your shell profile (`~/.zshrc`, `~/.bashrc`) so OpenCode can read it:

```bash
export SAMRESHUUU_API_KEY="sk-org-your_api_key"
```

## 2. Add the provider

OpenCode's global config is `~/.config/opencode/opencode.json`; an `opencode.json` at the root of a project applies to that project only. Create the file if there is none, or add the `provider` entry to the one you have.

```json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "samreshuuu": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Samreshuuu",
      "options": {
        "baseURL": "https://api.samreshuuu.ru/v1",
        "apiKey": "{env:SAMRESHUUU_API_KEY}"
      },
      "models": {
        "auto": {
          "name": "auto",
          "variants": {
            "low": {
              "reasoningEffort": "low"
            },
            "medium": {
              "reasoningEffort": "medium"
            },
            "high": {
              "reasoningEffort": "high"
            }
          }
        }
      }
    }
  },
  "model": "samreshuuu/auto"
}
```

| Setting | What it does |
| --- | --- |
| `npm` | `@ai-sdk/openai-compatible` makes OpenCode call `POST /v1/chat/completions`. OpenCode fetches the package itself the first time. |
| `options.baseURL` | The Model API's address. Keep `/v1` at the end. |
| `options.apiKey` | `{env:SAMRESHUUU_API_KEY}` reads the key from the environment, so it never sits in the file. |
| `models` | The models of OpenCode's picker. `auto` is one; any id from the [Models](/models) page can be another. |
| `variants` | The reasoning levels OpenCode offers for the model, each sent as `reasoning_effort`. |
| `model` | The model new sessions start on, as `samreshuuu/<id>`. |

OpenCode does not know the window of `auto` and plans its compaction for a default one. If you add a model of the catalogue with a smaller window, give it `"limit": {"context": …, "output": …}` from `context_length` and `max_output_tokens` in `GET /v1/models`.

## 3. Start OpenCode

Run `opencode` and pick the model with `/models`: the Model API's models read `samreshuuu/<id>`. To check the setup without a session, `opencode models` lists the models OpenCode knows, `samreshuuu/auto` among them.

Send a prompt. The call shows up in the call log of the [console](https://console.samreshuuu.ru) with the model `auto` picked and its cost. Tools work as OpenAI functions: OpenCode runs them, the model calls them.

## Undo the setup

Remove the `samreshuuu` entry from `provider`, and the `model` line if it points at it. OpenCode then goes back to your other providers.

## Troubleshooting

| What you see | What to check |
| --- | --- |
| The Model API's models are not in `/models` | The config is not valid JSON, or `SAMRESHUUU_API_KEY` is not set in the shell that started OpenCode. Run `opencode models`. |
| `401` | The key is wrong or revoked. Check it under API keys in the console. |
| `404` on every request | `baseURL` has no `/v1` at the end. |
| `model_not_found` | The model id under `models` is not in the catalogue: take one from `GET /v1/models`. |
| `402` | The balance does not cover the call's hold, or the key's spend limit is reached. |

| HTTP | type | Code | message | Retry |
| --- | --- | --- | --- | --- |
| 401 | `authentication_error` | `invalid_api_key` | The API key is unknown, disabled, revoked or expired. Switch a disabled key back on in the developer console; create a new one in place of a revoked or expired key. | no |
| 404 | `not_found_error` | `model_not_found` | The model does not exist. GET /v1/models lists the models this API sells. | no |
| 400 | `invalid_request_error` | `no_auto_model` | No model meets the request for auto. | no |
| 402 | `billing_error` | `insufficient_balance` | The organization balance does not cover this request (max_tokens is held in full until the answer is settled). Top up the balance or lower max_tokens. | no |
| 402 | `billing_error` | `key_limit_exceeded` | This API key has reached its spending limit. Raise the limit or use another key. | no |

If an error persists, write to support with the `x-request-id` of the answer.
