# Step-by-Step Guide: Install OpenCode and Configure `z.ai` as a Custom Model
## Step 1: Install OpenCode
**Using the install script (recommended):**
```bash
curl -fsSL https://opencode.ai/install | bash
```
**Using package managers:**
- **Node.js (npm):**
```bash
npm install -g opencode-ai
```
- **Homebrew (macOS and Linux):**
```bash
brew install sst/tap/opencode
```
- **Scoop (Windows):**
```bash
scoop bucket add extras
scoop install extras/opencode
```
Alternatively, download the binary directly from the [GitHub Releases page](https://github.com/sst/opencode/releases).
## Step 2: Initialize OpenCode
After installation, run OpenCode to start the interactive terminal UI:
```bash
opencode
```
On first run, OpenCode will:
- Create the `.opencode` data directory in your current working directory
- Initialize a SQLite database for session storage
- Auto-detect available LLM provider API keys from environment variables
- Launch the terminal UI
## Step 3: Configure `z.ai` API Access
`z.ai` provides an OpenAI-compatible API, which allows integration with tools like OpenCode that support the OpenAI protocol.
1. Sign up or log in at [z.ai](https://z.ai/subscribe?ic=WRPEOE7H5H)
2. Navigate to your [account settings](https://z.ai/manage-apikey/apikey-list)
3. Generate your `z.ai` API key
4. Keep this key secure for configuration
## Step 4: Configure OpenCode to Use `z.ai` as a Custom Model
OpenCode supports custom LLM providers through configuration. You'll need to set up `z.ai` as a provider using its compatible endpoint.
1. Run the authentication command:
```bash
opencode auth login
```
2. When prompted, select "custom" or "OpenAI-compatible" provider
3. Enter the following configuration:
- **API Provider:** OpenAI Compatible
- **API Base URL:** `https://api.z.ai/v1` (or the endpoint specified in `z.ai` documentation)
- **API Key:** Your `z.ai` API key
- **Model Name:** The specific model from `z.ai` you want to use (e.g., from the coding plan)
4. Save the configuration. OpenCode will store this in your configuration file.
Alternatively, you can manually edit the configuration file at `~/.opencode/config` to include:
```yaml
providers:
custom:
name: z.ai
api_key: YOUR_ZAI_API_KEY
base_url: https://api.z.ai/v1
model: glm-4.6 # or the model from your coding plan
```
## Step 5: Test the Integration
1. Start OpenCode:
```bash
opencode
```
2. Create a new session and attempt a coding task
3. Verify that requests are being routed through z.ai by checking for response patterns or latency characteristics
4. Check your `z.ai` dashboard for API usage to confirm the connection is active
## Key Integration Notes
- z\.ai's OpenAI-compatible API is a hard dependency for using custom models with tools like OpenCode
- The integration allows OpenCode to communicate with z.ai models without code modifications
- Configuration details can be adjusted based on your specific task requirements
This setup enables you to leverage the z.ai coding plan within OpenCode's terminal-based AI coding agent workflow, combining the strengths of both platforms for efficient development.