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

# OpenCode

> Use the LeanMCP AI Gateway with OpenCode CLI

# OpenCode Integration

[OpenCode](https://opencode.ai) is a powerful CLI tool for AI-assisted coding that supports 75+ LLM providers. You can configure it to use the LeanMCP AI Gateway as a custom provider.

<Info>
  OpenCode uses the AI SDK and supports any OpenAI-compatible API, making it perfect for the LeanMCP AI Gateway.
</Info>

## Prerequisites

<Steps>
  <Step title="Get Credits">
    Purchase credits at [leanmcp.com](https://leanmcp.com)
  </Step>

  <Step title="Create API Key">
    Create an API key at [leanmcp.com/api-keys](https://leanmcp.com/api-keys) with **SDK** permissions
  </Step>

  <Step title="Install OpenCode">
    ```bash theme={null}
    npm install -g opencode
    # or
    brew install opencode
    ```
  </Step>
</Steps>

## Configuration

### Configuration File

Create or update `opencode.json` in your project directory (or `~/.config/opencode/opencode.json` for global use):

```json theme={null}
{
    "$schema": "https://opencode.ai/config.json",
    "provider": {
        "leanmcp": {
            "npm": "@ai-sdk/openai-compatible",
            "name": "LeanMCP Custom Provider",
            "options": {
                "baseURL": "https://aigateway.leanmcp.com/v1/anthropic/",
                "apiKey": "leanmcp_your_api_key_here"
            },
            "models": {
                "claude-sonnet-4-5": {
                    "name": "claude-sonnet-4-5"
                }
            }
        }
    },
    "model": "leanmcp/claude-sonnet-4-5"
}
```

<Note>
  Replace `leanmcp_your_api_key_here` with your actual API key.
</Note>

## Using LeanMCP in OpenCode

Once configured, you can simply run:

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

Since the `model` is defined in `opencode.json` as `leanmcp/claude-sonnet-4-5`, Opencode will automatically use it.

To view available models:

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

Select `leanmcp/claude-sonnet-4-5` if it's not already selected.

## Configuration Options

| Option            | Description                                              |
| ----------------- | -------------------------------------------------------- |
| `npm`             | AI SDK package (`@ai-sdk/openai-compatible` for gateway) |
| `name`            | Display name in OpenCode UI                              |
| `options.baseURL` | LeanMCP AI Gateway endpoint                              |
| `options.apiKey`  | Can be set here or via `/connect`                        |
| `options.headers` | Custom headers for requests                              |
| `models`          | Map of available models                                  |

### Setting Token Limits

For proper context management, specify model limits:

```json theme={null}
{
  "provider": {
    "leanmcp": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "LeanMCP AI Gateway",
      "options": {
        "baseURL": "https://aigateway.leanmcp.com/v1/openai"
      },
      "models": {
        "gpt-5.2": {
          "name": "GPT-5.2",
          "limit": {
            "context": 128000,
            "output": 4096
          }
        }
      }
    }
  }
}
```

## Environment Variables

Alternatively, set credentials via environment variables:

```bash theme={null}
# Add to your shell profile (~/.bashrc, ~/.zshrc)
export LEANMCP_API_KEY="leanmcp_your_api_key_here"
```

Then reference in config:

```json theme={null}
{
  "provider": {
    "leanmcp": {
      "options": {
        "baseURL": "https://aigateway.leanmcp.com/v1/openai",
        "apiKey": "{env:LEANMCP_API_KEY}"
      }
    }
  }
}
```

## Multiple Providers via LeanMCP

Route all your AI providers through LeanMCP:

```json theme={null}
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "leanmcp-openai": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "OpenAI (via LeanMCP)",
      "options": {
        "baseURL": "https://aigateway.leanmcp.com/v1/openai"
      },
      "models": {
        "gpt-5.2": {},
        "gpt-5.2": {},
        "gpt-5.2": {}
      }
    },
    "leanmcp-anthropic": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Anthropic (via LeanMCP)",
      "options": {
        "baseURL": "https://aigateway.leanmcp.com/v1/anthropic"
      },
      "models": {
        "claude-sonnet-4-5-20250929": {},
        "claude-opus-4-5-20251101": {}
      }
    },
    "leanmcp-xai": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "xAI Grok (via LeanMCP)",
      "options": {
        "baseURL": "https://aigateway.leanmcp.com/v1/xai"
      },
      "models": {
        "grok-beta": {}
      }
    }
  }
}
```

## Verifying Setup

1. Run OpenCode: `opencode`
2. Select a LeanMCP model: `/models`
3. Ask a question
4. Check your [LeanMCP Dashboard](https://app.leanmcp.com) to see the logged request

## Benefits

<CardGroup cols={2}>
  <Card title="Unified Logging" icon="list">
    All your OpenCode sessions logged in one place
  </Card>

  <Card title="Multi-Provider" icon="layer-group">
    Use OpenAI, Anthropic, xAI through one gateway
  </Card>

  <Card title="Cost Tracking" icon="dollar-sign">
    Track spending across all your coding sessions
  </Card>

  <Card title="Security" icon="shield">
    Detect if sensitive code is being sent
  </Card>
</CardGroup>

## Troubleshooting

<AccordionGroup>
  <Accordion title="Provider not appearing in /models">
    * Verify `opencode.json` is in your project root or `~/.config/opencode/`
    * Check JSON syntax is valid
    * Restart OpenCode after config changes
  </Accordion>

  <Accordion title="Authentication errors">
    * Run `/connect` and re-enter your API key
    * Verify the API key has SDK permissions
    * Check you have credits in your account
  </Accordion>

  <Accordion title="Model not working">
    * Ensure the model name matches what LeanMCP supports
    * Check the baseURL matches the provider (openai, anthropic, etc.)
    * Try a different model to isolate the issue
  </Accordion>
</AccordionGroup>

## Resources

* [OpenCode Documentation](https://opencode.ai/docs)
* [OpenCode Providers Guide](https://opencode.ai/docs/models/)
* [OpenCode GitHub](https://github.com/anomalyco/opencode)

## Next Steps

<CardGroup cols={2}>
  <Card title="View Logs" icon="eye" href="/ai-gateway/observability">
    See all your OpenCode requests
  </Card>

  <Card title="SDK Integration" icon="code" href="/ai-gateway/sdk-integration">
    Use in your own applications
  </Card>
</CardGroup>
