SwitchRouter Connect & CLI
SwitchRouter Connect pairs Naira subscription plans and rolling fair-use windows with an intelligent coding-agent router (switchrouter/auto) and a single-command CLI installer (npx switchrouter / sr).
Configure OpenCode (~/.config/opencode/opencode.jsonc) to add SwitchRouter as a provider using "@ai-sdk/openai-compatible":
- Base URL: https://api.switchrouter.dev/v1
- API Key: "{env:SWITCHROUTER_API_KEY}"
- Header: "X-Title": "OpenCode"
- Query GET https://api.switchrouter.dev/v1/models to register tool-calling models (such as "deepseek.v3.2" and "qwen.qwen3-coder-480b-a35b") with their exact context_window and max_output_tokens limits.1. One-command CLI setup (npx switchrouter)
Run the official SwitchRouter CLI to authenticate your API key against GET https://api.switchrouter.dev/v1/connect/me, save credentials locally with 0600 permissions, and non-destructively configure OpenCode, Claude Code, Codex CLI, and your shell profile (with automatic .bak backups):
# Interactive global setup (auto-detects OpenCode, Claude Code, Codex CLI & Shell)
npx switchrouter connect
# Non-interactive setup for autonomous AI agents & CI
npx switchrouter connect --key sk_sr_your_key_here --yes
# Check your Naira credit balance & 5h / 7d / monthly fair-use gauges
npx switchrouter status2. Virtual model aliases (switchrouter/auto)
Instead of hardcoding a single model slug that either burns frontier rates on file searches or struggles on architecture turns, point your coding harness at switchrouter/auto:
- switchrouter/auto — Smart Phase Router (Default): Inspects each turn for images, reasoning effort, tool-loop depth, and architecture keywords. Automatically uses vision models for the single turn a screenshot is attached, then compacts image bytes and hands tool-execution follow-ups back to the fast coding workhorse pool.
- switchrouter/code — Core Coding Workhorse Pool: Sticky session-cached pool optimized for multi-file edits, tool calls, and test writing.
- switchrouter/fast — Sub-Second Search & Light Pool: Ultra-low latency pool for repo exploration, sub-agent lookups, and title generation.
- switchrouter/deep — Frontier Architecture & Debug Pool: Pins frontier reasoning models when designing complex systems or debugging race conditions.
3. Project setup (sr init & --project)
Need to isolate SwitchRouter to a specific repository or scaffold an application client? The CLI supports three project workflows:
# 1. Configure coding agents for ONLY the current git repository
# Writes ./opencode.jsonc, ./.claude/settings.local.json, ./.env.local
# and automatically protects secret files in .gitignore
npx switchrouter connect --project
# 2. Or initialize an app codebase (Node.js/TypeScript/Vercel AI SDK/Python)
# Writes .env.local + scaffolds src/lib/switchrouter.ts or switchrouter_client.py
npx switchrouter init
# 3. Or launch an agent with ephemeral environment injection (zero global config)
npx switchrouter run claude
npx switchrouter run opencode4. Manual harness configuration (Claude Code, OpenCode, Codex)
If you prefer configuring your coding harnesses manually without the CLI, use the snippets below:
export ANTHROPIC_BASE_URL="https://api.switchrouter.dev"
export ANTHROPIC_AUTH_TOKEN="sk_sr_your_key_here"
export ANTHROPIC_MODEL="switchrouter/auto"
export ANTHROPIC_SMALL_FAST_MODEL="switchrouter/fast"
claude{
"$schema": "https://opencode.ai/config.json",
"model": "switchrouter-connect/switchrouter/auto",
"small_model": "switchrouter-connect/switchrouter/fast",
"provider": {
"switchrouter-connect": {
"npm": "@ai-sdk/openai-compatible",
"name": "SwitchRouter Connect",
"options": {
"baseURL": "https://api.switchrouter.dev/v1",
"apiKey": "{env:SWITCHROUTER_API_KEY}",
"headers": {
"X-Title": "OpenCode"
}
},
"models": {
"switchrouter/auto": {
"name": "SwitchRouter Auto (Smart Router)",
"tool_call": true,
"attachment": true,
"limit": { "context": 200000, "output": 64000 }
},
"switchrouter/code": {
"name": "SwitchRouter Code (Workhorse Pool)",
"tool_call": true,
"limit": { "context": 200000, "output": 64000 }
},
"switchrouter/fast": {
"name": "SwitchRouter Fast (Search & Light Edits)",
"tool_call": true,
"limit": { "context": 200000, "output": 32000 }
},
"switchrouter/deep": {
"name": "SwitchRouter Deep (Frontier Architecture)",
"tool_call": true,
"attachment": true,
"limit": { "context": 200000, "output": 64000 }
}
}
}
}
}model = "switchrouter/auto"
model_provider = "switchrouter"
[model_providers.switchrouter]
name = "SwitchRouter Connect"
base_url = "https://api.switchrouter.dev/v1"
env_key = "SWITCHROUTER_API_KEY"
wire_api = "responses"5. CLI command reference (sr status, sr run, sr doctor)
- sr connect [--project] [--key sk_sr_...] [--yes] — Verify API key and configure coding harnesses globally or per-project.
- sr init [--template openai-ts|vercel-ai|python|env-only] — Write .env.local, protect secrets in .gitignore, and scaffold a starter SDK client.
- sr run <claude|opencode|codex> [args...] — Spawn an agent process with SwitchRouter environment variables injected only for that session.
- sr status [--json] — Display available Naira credits, rollover/top-up buckets, and 20-segment gauges for your 5-hour, 7-day, and monthly fair-use windows.
- sr doctor — Audit local 0600 credentials, harness files, and run a live switchrouter/fast latency ping.
- sr dashboard / sr topup — Open your workspace's Console Overview or Plan & Billing page in your browser using the server-resolved links from GET /v1/connect/me.
6. Installing Globally via npm
You can run the CLI anytime without installation via npx switchrouter, or install it globally to unlock the short sr command:
# Install globally via npm (unlocks both 'switchrouter' and 'sr' binaries)
npm install -g switchrouter
# Verify installation & ping switchrouter/fast latency
sr doctorBuilding an autonomous agent? Fetch /llms-full.txt for the complete single-file machine-readable specification.