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

# OpenCode

> Connect OpenCode to RunInfra, use its built-in listing, or configure a provider manually.

Use RunInfra models in OpenCode through the OpenAI-compatible Chat Completions API.

## Connect OpenCode

Use Node 20 or newer, an installed OpenCode with first-time setup complete, a workspace API key, and available RunInfra credits.

```bash theme={"dark"}
npx @runinfra/connect opencode
```

In an interactive terminal, Connect starts login if needed. Review and approve the proposed changes. Connect saves a snapshot, writes the provider settings and model selection, and makes a real Chat Completions request that uses credits. Follow the printed restart hint before using OpenCode.

To choose a model during setup:

```bash theme={"dark"}
npx @runinfra/connect opencode on --model deepseek-v4-flash
```

See [Connect](/docs/tools-sdks/connect) for key placement, status, diagnostics, and restore behavior. Run `on` again to refresh the saved model list.

## Configure manually

### Use the built-in listing

RunInfra is listed on models.dev, which supplies OpenCode's provider and model catalog. You do not need a `provider` block to use that listing.

Load your workspace key into `RUNINFRA_GATEWAY_KEY` through your secret-management workflow, then launch OpenCode from that environment:

```bash theme={"dark"}
opencode --model runinfra/deepseek-v4-flash
```

Alternatively, start OpenCode, run `/connect`, select **RunInfra**, and enter your workspace key. Use `/models` to choose a RunInfra model. OpenCode saves that credential in `~/.local/share/opencode/auth.json`.

<Note>
  The built-in listing reads `RUNINFRA_GATEWAY_KEY`. Connect's login and helper mode use `RUNINFRA_API_KEY`. These names are not interchangeable unless you set both to the same workspace key.
</Note>

### Define an explicit provider

Use this form when you need to override the listing or declare a model yourself. Back up your configuration, then merge the block into `~/.config/opencode/opencode.json`. OpenCode also accepts `opencode.jsonc`. A project-level `opencode.json` can override global values.

```json theme={"dark"}
{
  "$schema": "https://opencode.ai/config.json",
  "model": "runinfra/deepseek-v4-flash",
  "provider": {
    "runinfra": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "RunInfra",
      "options": {
        "baseURL": "https://api.runinfra.ai/v1",
        "apiKey": "{env:RUNINFRA_GATEWAY_KEY}"
      },
      "models": {
        "deepseek-v4-flash": {
          "name": "DeepSeek V4 Flash",
          "reasoning": true,
          "tool_call": true,
          "limit": {
            "context": 1048576,
            "output": 32768
          }
        }
      }
    }
  }
}
```

`@ai-sdk/openai-compatible` sends Chat Completions requests. The provider id is `runinfra`; model selections use `runinfra/<slug>`, such as `runinfra/deepseek-v4-flash`. The key under `models` is the bare model id sent to the API.

Keep `limit.context` and `limit.output` explicit when declaring models. They let OpenCode track the available context and output budget. Standard listing entries already supply these values. An explicit provider block can shadow listing values, so update it when you change models.

The `{env:RUNINFRA_GATEWAY_KEY}` reference resolves to an empty string if the variable is missing. Load the key before you restart OpenCode. Keep literal keys out of shared project configuration.

## Verify

Load the same workspace key into `RUNINFRA_GATEWAY_KEY` in your terminal, including when you used `/connect` to save it in OpenCode. This request uses credits and reads `usage.prompt_tokens_details.cached_tokens`. The macOS and Linux example uses `jq`.

<Tabs>
  <Tab title="macOS and Linux">
    ```bash theme={"dark"}
    curl --silent --show-error --fail-with-body https://api.runinfra.ai/v1/chat/completions \
      -H "Authorization: Bearer $RUNINFRA_GATEWAY_KEY" \
      -H "Content-Type: application/json" \
      -d '{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"Say hello."}],"max_tokens":16384}' \
      | jq '.usage.prompt_tokens_details.cached_tokens'
    ```
  </Tab>

  <Tab title="PowerShell">
    ```powershell theme={"dark"}
    $body = '{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"Say hello."}],"max_tokens":16384}'
    $response = $body | curl.exe --silent --show-error --fail-with-body https://api.runinfra.ai/v1/chat/completions `
      -H "Authorization: Bearer $env:RUNINFRA_GATEWAY_KEY" `
      -H "Content-Type: application/json" `
      --data-binary '@-' | ConvertFrom-Json
    $response.usage.prompt_tokens_details.cached_tokens
    ```
  </Tab>
</Tabs>

A count of `0` is valid; a successful request does not guarantee a cache hit. This tests the API, not whether OpenCode has reloaded its configuration. Restart OpenCode and select the model with `/models`.

## Revert

If you used Connect:

```bash theme={"dark"}
npx @runinfra/connect opencode off
```

This restores the snapshot from immediately before the most recent `on`, which may already contain RunInfra settings. If a managed file changed afterward, Connect refuses to overwrite it. Back up and review those edits before proceeding.

For manual setup, restore the previous `provider.runinfra` block and any `model` or `small_model` selection you changed. Remove them only if you added them for this setup. Check global and project configuration, plus any `OPENCODE_CONFIG` or `OPENCODE_CONFIG_CONTENT` override.

If you saved a credential with `/connect`, run `opencode auth logout` and select RunInfra. Remove `RUNINFRA_GATEWAY_KEY` from the launch environment and any project `.env` where you set it. Restart OpenCode and use `/models` to select another model, since OpenCode can remember the last selection.

Connect's `logout` removes only its saved credentials. It does not restore agent settings or revoke the workspace key. Revoke an unwanted key in the dashboard.

## Related

<Columns cols={3}>
  <Card title="Connect" icon="plug" href="/docs/tools-sdks/connect">
    Set up, inspect, and restore your coding agent.
  </Card>

  <Card title="Chat completions" icon="braces" href="/docs/api-reference/chat-completions">
    Read the request contract and cache usage fields.
  </Card>

  <Card title="Models" icon="list" href="/docs/api-reference/models">
    Find model ids and context windows.
  </Card>
</Columns>
