Curated models for your best work

Join the waitlist

Router docs

One API. Your language.

Start with a TokenSpend SDK or curl, then connect your coding agent.

SDK quickstart

Before sending, issue a rail key in Router and connect a provider key or add managed credits. Requests are billable. TokenSpend prompt logging is off by default; provider retention is separate.

SDK quickstart

1. Install

Terminal
python3 -m pip install tokenspend

Python 3.9+; use a virtual environment. TLS error on python.org macOS installs? Run the bundled Install Certificates.command, then retry. Python guide

2. Set your rail key

Paste a tsr_ key at the hidden prompt in Bash or zsh. Never use a provider or admin key.

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

3. Choose a model

Model

4. Save as quickstart.py

quickstart.py
from tokenspend import TokenSpend

client = TokenSpend()
response = client.chat.completions.create(
    model="zai/glm-5.2",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content)
print("Request ID:", response.request_id)

python3 quickstart.py

Your first successful request will appear in Router activity.

Key: $TOKENSPEND_API_KEY

API

api.tokenspend.dev

Authentication

One tsr_ rail key

Proof

Request ID + receipt

Safe retries

Retry without a duplicate debit.

Reuse a key only for the same request within 24 hours. Duplicates return 409 with the original request ID, not a replayed response. A failed request needs a new key for another attempt.

Python
response = client.chat.completions.create(
    model="zai/glm-5.2",
    messages=messages,
    idempotency_key="deploy-42-generation",
)
print(response.request_id)
TypeScript
import { getResponseMetadata } from "@tokenspend/sdk";

const response = await client.chat.completions.create(
  { model: "zai/glm-5.2", messages },
  { idempotencyKey: "deploy-42-generation" }
);
console.log(getResponseMetadata(response)?.requestId);

Use your coding agent.

Coding agents use the same TokenSpend key and receipts as the SDKs. Pick yours.

Claude Code

Install Claude Code from Claude's setup guide, set your key as above, then start it from that terminal. Keep keys out of project files.

This example uses Sonnet 4.6. It turns off adaptive thinking and experimental betas.

Claude Code
(
  export ANTHROPIC_BASE_URL=https://api.tokenspend.dev
  export ANTHROPIC_AUTH_TOKEN="$TOKENSPEND_API_KEY"
  unset ANTHROPIC_API_KEY
  export ANTHROPIC_MODEL="anthropic/claude-sonnet-4-6"
  export CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING=1
  export CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1
  claude
)
Models that work in Claude Code (23)
  • anthropic/claude-opus-5-5
  • anthropic/claude-sonnet-4-6
  • zai/glm-5.3
  • zai/glm-5.3-flash
  • qwen/qwen3.5-397b
  • qwen/qwen3.8-27b
  • qwen/qwen3.8-flash
  • qwen/qwen3.8-2.4t-a95b
  • qwen/qwen3.7-max
  • qwen/qwen3.7-plus
  • qwen/qwen3.6-plus
  • qwen/qwen3.5-9b
  • thinking-machines/inkling
  • amazon/nova-lite
  • google/gemini-3.8-flash
  • google/gemini-3.6-flash
  • google/gemini-3.5-flash-lite
  • google/gemini-3.1-flash-lite
  • google/gemini-3.1-pro
  • xai/grok-4.6
  • xai/grok-4.5
  • xai/grok-4.3
  • xai/grok-build-0.1

For a non-Claude model, also set MAX_THINKING_TOKENS=0. For a smaller output limit, such as Nova's 5,000, set CLAUDE_CODE_MAX_OUTPUT_TOKENS to it.

In /status, check the base URL and the credential source. A settings file can override both. Exit the subshell to go back to your normal settings.

Adaptive thinking, server-side tools and unlisted beta fields return a 400. Keep tool results in Messages format.

Sources: gateway authentication · feature compatibility.

Codex

Codex talks to custom providers over the Responses API. Add TokenSpend as a provider in your Codex config, with the key in TOKENSPEND_API_KEY.

Codex config.toml
model_provider = "tokenspend"

[model_providers.tokenspend]
name = "TokenSpend"
base_url = "https://api.tokenspend.dev/v1"
env_key = "TOKENSPEND_API_KEY"
wire_api = "responses"

Function tools, tool-result continuation and reasoning controls work on Astra and the GPT-5 models. Stored-response chaining and hosted tools are not supported.

See Codex provider configuration and our Responses support.

Cursor

  1. Open Cursor Settings → Models. If your version has Override OpenAI Base URL, set it to https://api.tokenspend.dev/v1. If it does not, this setup is not available in your version.
  2. Paste a TokenSpend key into the API-key field, never a Cursor admin key. Add an exact TokenSpend model ID where custom models are allowed.
  3. Send a small chat request and check its receipt before you use Agent mode. Restore the old URL and key when you switch back.

Tab completion does not use custom keys. Cursor sends API-key requests through its own servers, so TokenSpend logging settings do not cover that path.

Sources: Cursor API keys.

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.

Check the receipt

  1. Send one small request with synthetic content. It is billable. Note the request ID, not the key.
  2. Open Router activity in the same workspace and match the request ID, model, status, token counts and cost.
  3. With an agent, ask it to read a harmless README, summarize it, then answer a follow-up. You should see a tool call, a tool result and matching receipts.

Nothing showing up? Check the error code and API status. Contact us with the request ID, UTC time, model and client version. Do not send keys or private prompts.

Capture is separate

Router sends your requests to a model. Capture is metadata-only: usage counts and git refs; it does not configure an API endpoint or send code. Use Capture installation after signing in. Capture and rail keys are not interchangeable.

TokenSpend never stores Router prompts or responses. TokenSpend does not currently offer provider ZDR.