🎬 New — watch the 2-minute guide videos →
Documentation

Run an open coding agent on GLM

← Cookbook

Drive an open-source coding agent (OpenCode) with an open GLM model as the brain — metered, budgeted and residency-checked through BharatRouter, with no provider lock-in.

GLM (BYOK) over the OpenAI-compatible API Agents Intermediate 8 min

Run an open-source coding agent — OpenCode — with an open GLM model (MIT-licensed) as the brain, all governed through BharatRouter. The agent speaks OpenAI Chat Completions and BharatRouter is a drop-in /v1/chat/completions endpoint, so wiring it up is just config — and every tool-calling turn is metered, budgeted and logged for the org.

You'll use: the GLM family (glm-4.6, glm-4.5-air, glm-4.7-flash, glm-5.2, vision glm-4.6v) over the OpenAI-compatible API, with BYOK. Browse them on Models.

Why route GLM through BharatRouter

A coding agent loops fast and burns tokens — so put it behind a governed endpoint. You get per-key ₹ budgets and daily limits on the whole loop, usage metering on every turn (streamed usage included), and failover between GLM hosts. Best of all, you swap the agent's brain — glm-4.6glm-4.5-airglm-4.7-flash — by changing one string, with the same BharatRouter key.

Bring a GLM key

GLM is BYOK: save a key once and plain requests for glm-4.6 route on it. Two hosts work — Zhipu (z.ai, the source) or OpenRouter (often cheaper). A Zhipu key is <id>.<secret> and works as a raw bearer token — no JWT step:

curl -X PUT https://api.bharatrouter.com/me/byok/zhipu \
  -H "Authorization: Bearer br-..." \
  -d '{"key": "<id>.<secret>", "label": "glm"}'

Point OpenCode at the gateway

Install OpenCode (npm install -g opencode-ai), then drop an opencode.json in your project that registers a bharatrouter provider and defaults to GLM. Carry your gateway key in BHARATROUTER_API_KEY:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "bharatrouter": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "BharatRouter",
      "options": {
        "baseURL": "https://api.bharatrouter.com/v1",
        "apiKey": "{env:BHARATROUTER_API_KEY}"
      },
      "models": { "glm-4.6": { "name": "GLM-4.6" } }
    }
  },
  "model": "bharatrouter/glm-4.6"
}

Run it

export BHARATROUTER_API_KEY="br-..."
opencode run --model bharatrouter/glm-4.6 \
  "Create fib.py with fib(n) and run it to print fib(10)."

Swap the brain by changing one string: bharatrouter/glm-4.5-air (cheap), bharatrouter/glm-4.7-flash (cheapest), bharatrouter/glm-5.2 (frontier, ~1M context) or bharatrouter/glm-4.6v (vision). Want to wire Claude Code or another agent to the gateway over MCP instead? See Wire an agent to the gateway.

More recipes in the Cookbook, or see the full API reference.