Curated models for your best work

Join the waitlist

Router docs

OpenCode with TokenSpend

Add TokenSpend as a provider in your OpenCode config, V1 or V2.

Paste it into your coding agent. It carries no key and states how far this setup has been tested.

See what gets copied
# Connect OpenCode to TokenSpend

API format: Chat Completions
API key: a TokenSpend key, read from the environment variable TOKENSPEND_API_KEY. Never write it into a project file or print it.
Back up any existing config before you change it, and tell the user what you changed.
Models: exact IDs are listed at https://tokenspend.dev/models (for example anthropic/claude-sonnet-4-6).

Merge into the project's OpenCode config without overwriting existing settings. Check `opencode --version` first: V1 uses opencode.json, V2 uses opencode.jsonc.

V1:
```json
{
  "$schema": "https://opencode.ai/config.json",
  "model": "tokenspend/zai/glm-5.2",
  "provider": {
    "tokenspend": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "TokenSpend",
      "options": {
        "baseURL": "https://api.tokenspend.dev/v1",
        "apiKey": "{env:TOKENSPEND_API_KEY}"
      },
      "models": {
        "zai/glm-5.2": {
          "name": "zai/glm-5.2"
        }
      }
    }
  }
}
```

V2:
```json
{
  "$schema": "https://opencode.ai/config.json",
  "model": "tokenspend/coder",
  "providers": {
    "tokenspend": {
      "env": [
        "TOKENSPEND_API_KEY"
      ],
      "package": "@opencode/ai/providers/openai-compatible",
      "settings": {
        "baseURL": "https://api.tokenspend.dev/v1"
      },
      "models": {
        "coder": {
          "modelID": "zai/glm-5.2"
        }
      }
    }
  }
}
```

Check:
1. Send one small request with synthetic content. It is billable. Note the request ID, not the key.
2. Open https://tokenspend.dev/dashboard/router and match the request ID, model, status, token counts and cost.
If something fails, check the error code at https://tokenspend.dev/docs/router/reliability#errors and https://tokenspend.dev/status. Do not send keys or private prompts to support.

1. Set your key

Create a rail key in Router, then load it into this terminal. It stays in your environment, not in a project file.

Terminal
printf 'TokenSpend rail key: '
read -r -s TOKENSPEND_API_KEY
printf '\n'
export TOKENSPEND_API_KEY

2. Connect OpenCode

OpenCode

Run opencode --version, then merge the matching config into your project's OpenCode config without overwriting your settings. Start OpenCode from a terminal with your key set.

V1 · opencode.json
OpenCode V1
{
  "$schema": "https://opencode.ai/config.json",
  "model": "tokenspend/zai/glm-5.2",
  "provider": {
    "tokenspend": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "TokenSpend",
      "options": {
        "baseURL": "https://api.tokenspend.dev/v1",
        "apiKey": "{env:TOKENSPEND_API_KEY}"
      },
      "models": {
        "zai/glm-5.2": {
          "name": "zai/glm-5.2"
        }
      }
    }
  }
}
V2 · opencode.jsonc
OpenCode V2
{
  "$schema": "https://opencode.ai/config.json",
  "model": "tokenspend/coder",
  "providers": {
    "tokenspend": {
      "env": [
        "TOKENSPEND_API_KEY"
      ],
      "package": "@opencode/ai/providers/openai-compatible",
      "settings": {
        "baseURL": "https://api.tokenspend.dev/v1"
      },
      "models": {
        "coder": {
          "modelID": "zai/glm-5.2"
        }
      }
    }
  }
}

Pick the TokenSpend entry in /models. It uses the Chat Completions API. Tool results must reference the matching call IDs, and unsupported fields return a clear error.

Sources: V1 custom provider · V2 provider configuration.

3. Check the receipt

A response is only half the check. Send one small request, then match its request ID, model and cost in Router activity. The full steps are in Verify the receipt.

Other agents

All coding agents