Never hit a Z.AI rate limit again — multi-account key rotation and automatic failover for OpenCode's zai-coding-plan provider.
Spread requests across multiple Z.AI / ZhipuAI Coding Plan API keys. When a request fails with 429, 401, or 5xx, the plugin switches to the next key and retries once. You keep the normal OpenCode GLM model list (glm-5, glm-5.1, glm-4.7, etc.) while the plugin handles key management behind the scenes.
Tested with OpenCode 1.4.3 and Node.js 20+.
- targets
zai-coding-planonly - reuses OpenCode's built-in GLM model registry
- injects
Authorization: Bearer <key>at request time - loads keys from
~/.config/opencode/zai-keys.jsonorZAI_API_KEYS - rotates to the next key on
429,401, or5xx - sets model cost to zero in memory so Coding Plan usage does not show token pricing
- it does not add support for the general
zaiprovider - it does not fetch models dynamically from Z.AI
- it does not define custom models in the plugin
- it does not persist runtime cooldown/disable state across process restarts
OpenCode already knows the zai-coding-plan provider and its GLM models. This plugin does not replace that provider. Instead, it hooks into OpenCode's auth.loader for zai-coding-plan and swaps in a custom fetch implementation that chooses the active key, injects the Bearer token, and retries once with the next key when needed.
At runtime, the key source priority is:
ZAI_API_KEYS~/.config/opencode/zai-keys.json- the single API key stored in OpenCode auth for
zai-coding-plan
- Node.js 20+
- OpenCode with plugin support
- access to Z.AI Coding Plan keys
Add the plugin to your opencode.json:
Clone the repository, build it, then reference the compiled file:
npm install
npm run build{
"$schema": "https://opencode.ai/config.json",
"plugin": ["/absolute/path/to/oc-zai-multi-account/dist/index.js"]
}For local-path usage,
dist/must exist. Build first.
This plugin depends on the built-in zai-coding-plan provider, so keep a minimal provider stub in opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["oc-zai-multi-account@latest"],
"provider": {
"zai-coding-plan": {
"options": {
"apiKey": "oc-zai-multi-account-managed"
}
}
}
}OpenCode only exposes models for providers that are active in config/auth state. Without a zai-coding-plan provider entry, the GLM models may not appear in the model list even though the plugin is installed.
The apiKey value above is only a bootstrap placeholder. The plugin replaces it at request time with a real key from your configured key sources.
{
"keys": [
{
"id": "main",
"label": "Main",
"apiKey": "sk-xxx",
"enabled": true
},
{
"id": "backup",
"label": "Backup",
"apiKey": "sk-yyy",
"enabled": true
}
],
"rotation": {
"strategy": "round-robin",
"cooldownMs": 60000
}
}export ZAI_API_KEYS="sk-xxx,sk-yyy,sk-zzz"
export ZAI_ROTATION_STRATEGY="round-robin"
export ZAI_COOLDOWN_MS="60000"If neither the file nor environment variables are set, the plugin falls back to the single API key stored in OpenCode auth.
After installing the plugin and adding the provider stub, create an auth record for zai-coding-plan once:
opencode providers login \
--provider zai-coding-plan \
--method "Z.AI Coding Plan API Key"When prompted, enter:
oc-zai-multi-account-managed
This placeholder is intentional. It activates the provider/auth path inside OpenCode so the plugin's auth.loader can take over and inject the real keys from zai-keys.json or ZAI_API_KEYS.
If you want to use OpenCode auth as the only key source instead of zai-keys.json / ZAI_API_KEYS, enter your real Z.AI Coding Plan key here instead of the placeholder.
If you skip this step, you may see one of these symptoms:
zai-coding-planmodels do not appear- requests fail before the plugin's rotating fetch runs
token expired or incorrecteven though your key file is valid
If you previously configured multiple custom Z.AI providers in opencode.json, remove them once this plugin is enabled.
Delete blocks like:
"provider": {
"zai1": { ... },
"zai2": { ... }
}and keep only the single zai-coding-plan provider stub shown above.
For requests to zai-coding-plan:
- the plugin selects the next available key
- it sends
Authorization: Bearer <key> - if the response is:
2xx: success, keep going429: put the current key into cooldown, try the next key once401: disable the current key for the rest of the process, try the next key once5xx: try the next key once
- if all enabled keys are cooling down, wait for the shortest cooldown to expire
- if the retry also fails, return the original error to OpenCode
Current implementation details:
- retry count: 1
- rotation strategy: round-robin
- default cooldown: 60000ms
- state is in memory only
This plugin does not define any models. It relies on OpenCode's built-in zai-coding-plan provider and its built-in GLM model list.
That means you keep the standard model IDs exposed by OpenCode, such as:
glm-5glm-5.1glm-5-turboglm-4.7glm-4.7-flashglm-4.7-flashxglm-4.6glm-4.5
and the rest of the zai-coding-plan models bundled by your OpenCode version.
After setup, these commands are useful:
opencode providers list
opencode models zai-coding-planYou should see:
- a
Z.AI Coding Plancredential entry - the normal GLM model list for
zai-coding-plan
Check all three conditions:
- the plugin is installed and loaded
provider.zai-coding-planexists inopencode.json- you completed the one-time auth bootstrap with
opencode providers login
Usually this means one of these:
- the auth bootstrap step was skipped, so OpenCode never entered the plugin auth path
- the keys in
zai-keys.jsonorZAI_API_KEYSare invalid - the plugin is not loading from the path/package you expect
Recommended checks:
opencode providers list
opencode models zai-coding-planThen verify that:
~/.config/opencode/zai-keys.jsonexists and contains real keysopencode.jsonstill contains thezai-coding-planprovider stub- your plugin entry points to the built
dist/index.js
Make sure you built the plugin:
npm install
npm run buildand confirm the path points to dist/index.js, not src/index.ts.
That works too. Configure one key in zai-keys.json or rely on the OpenCode auth fallback. You just will not get any failover benefits.
- never commit real API keys
- keep
~/.config/opencode/zai-keys.jsonoutside your repository - do not publish
zai-keys.json,.zai_api_key_*, or screenshots containing provider credentials - if you are migrating from old
zai1/zai2config, remove stale key files you no longer use
npm install
npm run check
npm run buildMIT
{ "$schema": "https://opencode.ai/config.json", "plugin": ["oc-zai-multi-account@latest"] }