OpenCode is free and open source; GLM is one of the cheapest capable model families, with a flat coding plan from $18/month. Together they make a low-cost, fully terminal coding agent that reads your repo, edits files, and runs commands without OpenAI or Anthropic pricing.
This is the OpenCode + GLM setup: the provider block, model IDs, and the billing choice. For OpenCode’s general provider model, see OpenCode custom providers.
Step 1: Install OpenCode and get a Z.AI key
npm install -g opencode-ai
Sign up at Z.AI for a key — pay-per-token or the GLM Coding Plan. On Windows, WSL gives the cleaner shell — see install OpenCode on Windows with WSL.
Step 2: Add the GLM provider
OpenCode connects to OpenAI-compatible APIs through @ai-sdk/openai-compatible. Add Z.AI in opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"zai": {
"npm": "@ai-sdk/openai-compatible",
"name": "Z.AI GLM",
"options": {
"baseURL": "https://api.z.ai/api/paas/v4",
"apiKey": "{env:ZAI_API_KEY}"
},
"models": {
"glm-5-turbo": { "name": "GLM-5 Turbo" },
"glm-5": { "name": "GLM-5" }
}
}
},
"model": "zai/glm-5-turbo"
}
Export the key:
export ZAI_API_KEY="your-zai-key"
Step 3: Run it
opencode
The default is zai/glm-5-turbo. Switch in the TUI or change the model key.
GLM models in OpenCode (by plan)
| zai/glm-5-turbo | Everyday default; all plans |
|---|---|
| zai/glm-5 | Hard tasks; Pro plan |
| zai/glm-4.7 | Solid alternative; all plans |
Pricing
OpenCode is free; GLM bills via pay-per-token or the GLM Coding Plan (from $18/month, with rolling-window caps). Heavy daily users save with the plan. See GLM Coding Plan setup and GLM-4.6 vs GLM-5 vs GLM-5-Turbo.
Troubleshooting
- Provider not found — the
providerkey (zai) must match themodelprefix. - Auth errors —
ZAI_API_KEYnot set in the launching shell. - Format errors — you used
/anthropic; switch to the OpenAI (paas/v4) URL. - Access denied on glm-5 — plan tier doesn’t include it; use
glm-5-turbo.
GLM + OpenCode checklist
- OpenCode installed
- Z.AI key or GLM Coding Plan active
- Provider block with the OpenAI (paas/v4) base URL
- GLM model IDs listed; default set
- Launched with opencode
Wrapping up
GLM with OpenCode is a cheap, open agent: add a Z.AI provider in opencode.json using @ai-sdk/openai-compatible on the OpenAI endpoint, list glm-5-turbo and glm-5, and set a default. Pay per token or use the GLM Coding Plan, and default to Turbo to stretch your usage.
For other GLM setups, see run GLM-5 with Claude Code and GLM + Cline in VS Code.