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

# OpenCode

> Configure OpenCode to use TokenLab API

## Overview

<Note>
  **Type**: Coding Tool

  **Primary Path**: OpenAI-compatible

  **Support Confidence**: Supported path
</Note>

OpenCode is an open-source AI coding assistant that runs in your terminal. It supports multiple LLM providers and can be configured to use TokenLab's API for access to hundreds of models.

## Installation

<Tabs>
  <Tab title="Install Script">
    ```bash theme={null}
    curl -fsSL https://opencode.ai/install | bash
    ```
  </Tab>

  <Tab title="Homebrew">
    ```bash theme={null}
    brew install opencode-ai/tap/opencode
    ```

    Or use the community tap (more frequent updates):

    ```bash theme={null}
    brew install anomalyco/tap/opencode
    ```
  </Tab>

  <Tab title="Go">
    ```bash theme={null}
    go install github.com/opencode-ai/opencode@latest
    ```
  </Tab>
</Tabs>

Verify installation:

```bash theme={null}
opencode --version
```

## Configuration

### Step 1: Set Environment Variables

```bash theme={null}
export OPENAI_API_KEY="sk-your-tokenlab-key"
```

For permanent configuration, add to `~/.bashrc` or `~/.zshrc`:

```bash theme={null}
echo 'export OPENAI_API_KEY="sk-your-tokenlab-key"' >> ~/.zshrc
source ~/.zshrc
```

### Step 2: Configure OpenCode

OpenCode's current docs recommend configuring providers through `opencode.json` or `opencode.jsonc`. For TokenLab, use a custom provider instead of overloading the built-in `openai` provider:

```jsonc theme={null}
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "tokenlab": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "TokenLab",
      "options": {
        "baseURL": "https://api.tokenlab.sh/v1",
        "apiKey": "{env:OPENAI_API_KEY}"
      },
      "models": {
        "gpt-5.4": {
          "name": "GPT-5.4"
        },
        "gpt-5-mini": {
          "name": "GPT-5 Mini"
        }
      }
    }
  },
  "model": "tokenlab/gpt-5.4"
}
```

<Note>
  Use `@ai-sdk/openai-compatible` for chat-completions style compatibility. If you specifically need a provider path built on `/v1/responses`, OpenCode's provider docs recommend switching the package to `@ai-sdk/openai`.
</Note>

## Basic Usage

**Start interactive mode:**

```bash theme={null}
opencode
```

**Run with a prompt:**

```bash theme={null}
opencode "Explain this codebase"
```

**Specify model:**

```bash theme={null}
opencode --model tokenlab/gpt-5.4 "Fix the bugs in main.py"
```

## Available Models

| Model               | Best For                         |
| ------------------- | -------------------------------- |
| `gpt-5.4`           | Complex tasks, code architecture |
| `gpt-5-mini`        | Quick fixes, simple queries      |
| `claude-sonnet-4-6` | Code review, documentation       |
| `claude-opus-4-6`   | Complex reasoning                |
| `gemini-3.5-flash`  | Fast responses                   |
| `deepseek-r1`       | Algorithm design                 |

## Common Commands

**Analyze code:**

```bash theme={null}
opencode "What does this function do?" < src/utils.ts
```

**Generate code:**

```bash theme={null}
opencode "Create a REST API with Express"
```

**Review changes:**

```bash theme={null}
git diff | opencode "Review these changes"
```

**Fix errors:**

```bash theme={null}
opencode "Fix the TypeScript errors in this project"
```

## Interactive Commands

| Command         | Description                 |
| --------------- | --------------------------- |
| `/help`         | Show available commands     |
| `/model <name>` | Switch to a different model |
| `/clear`        | Clear conversation history  |
| `/exit`         | Exit OpenCode               |

## Troubleshooting

<AccordionGroup>
  <Accordion title="Connection Error">
    * Verify `options.baseURL` is set to `https://api.tokenlab.sh/v1`
    * Check network connectivity
    * Try `curl https://api.tokenlab.sh/v1/models` to test
  </Accordion>

  <Accordion title="Authentication Failed">
    * Verify `OPENAI_API_KEY` environment variable is set
    * Verify `options.apiKey` references `{env:OPENAI_API_KEY}` or another valid secret source
    * Check that the key starts with `sk-`
    * Ensure the key is active in TokenLab dashboard
  </Accordion>

  <Accordion title="Model Not Found">
    * Check model name in config matches exactly
    * Verify model availability at [tokenlab.sh/en/models](https://tokenlab.sh/en/models)
  </Accordion>
</AccordionGroup>

## Best Practices

<AccordionGroup>
  <Accordion title="Use project context">
    Run OpenCode from your project root for better understanding of your codebase.
  </Accordion>

  <Accordion title="Choose appropriate models">
    Use faster models (`gpt-5-mini`) for simple tasks and stronger models (`gpt-5.4`, `claude-sonnet-4-6`) for complex ones.
  </Accordion>

  <Accordion title="Review generated code">
    Always review AI-generated code before applying changes to your project.
  </Accordion>
</AccordionGroup>
