SwitchRouterDocumentation
Dashboard

Production & troubleshooting

How to run SwitchRouter reliably, handle failures, and where to look when something goes wrong.

Keep keys secret

API keys grant access to your workspace wallet, so treat them like credentials: store them in server-side environment variables, never in source control, browsers or mobile clients, and rotate by revoking and creating a new key in the Console.

env
SWITCHROUTER_API_KEY=sk_sr_…   # server-side environment only

Timeouts and retries

Set a client timeout that covers your max_tokens and streaming duration. Retry only transient failures (429, 502, 503) with exponential backoff. There is no idempotency key: a retried request may be billed again, so retry only when the previous attempt failed.

javascript
async function completeWithRetry(client, body, attempts = 3) {
  let lastError;
  for (let attempt = 0; attempt < attempts; attempt += 1) {
    try {
      return await client.chat.completions.create(body);
    } catch (error) {
      lastError = error;
      const status = error?.status;
      // Retry transient provider/rate-limit errors only.
      if (status !== 429 && status !== 502 && status !== 503) throw error;
      const delay = Math.min(2 ** attempt * 500, 8000);
      await new Promise((resolve) => setTimeout(resolve, delay));
    }
  }
  throw lastError;
}

Rate limits

Requests are rate-limited per credential and per IP. On 429, read the retry-after-* response header and back off rather than retrying immediately.

Error handling

Branches are driven by the HTTP status and the stable error.code. Log the x-switchrouter-request-id header — it identifies the request in support conversations.

error · 402
{
  "error": {
    "message": "Insufficient balance for this request.",
    "type": "insufficient_quota",
    "code": "insufficient_balance",
    "request_id": "req_..."
  }
}

Streaming in production

  • Disable intermediary response buffering — the API sends cache-control: no-cache, no-transform.
  • Set a longer read timeout than for non-streaming calls and handle the terminating data: [DONE] event.
  • Enable stream_options.include_usage when you need token counts per response.

Routing, protocols, and failover

SwitchRouter exposes three inference wire protocols on the same gateway: OpenAI Chat Completions (POST /v1/chat/completions), Anthropic Messages (POST /v1/messages), and OpenAI Responses (POST /v1/responses).

  • Deployment selection: Requests are validated against the requested model's capabilities (streaming, tool calling, structured output, image inputs) and routed to a configured upstream deployment.
  • Automatic failover: When a model (or a Connect virtual routing pool such as switchrouter/auto) has multiple configured healthy deployments, retryable upstream failures automatically fail over to the next candidate deployment before returning an error.
  • Explicit failure when no route is healthy: Failover depends on alternate deployments being configured and healthy. If all candidate upstreams fail, the gateway returns a structured 502 or 503 response and releases any reserved credit hold so your balance is not charged.

Billing and settlement

SwitchRouter uses a closed-loop prepaid credit ledger in PostgreSQL (integer kobo accounting). Deposited funds convert into non-transferable, non-withdrawable SwitchRouter credits consumable strictly on the platform. Spend is attributed to the API key that made the request, held during in-flight execution, and settled only for verified completed turns. For Connect workspaces, settled usage debits in strict priority order: current-period credits, then banked rollover credits, then never-expiring top-up credits.

Not implemented

Documented honestly, not hidden
If a feature is not listed here, assume it is unavailable rather than implied.
  • No embeddings, audio, image-generation or realtime endpoints.
  • No MCP server.
  • Usage aggregation is a Console (session) surface — there is no API-key usage endpoint yet.

Troubleshooting checklist

  1. Confirm the base URL ends in /v1.
  2. Confirm the key is active (not revoked or expired) and belongs to the workspace you expect.
  3. Confirm the model ID exists in GET /v1/models.
  4. Confirm the model supports the feature you used (streaming, tools, structured output).
  5. For 402, fund the workspace wallet under Billing.
  6. Capture the x-switchrouter-request-id when escalating.

Start from the Quickstart if you are still setting things up.

Start building with SwitchRouter.

Connect your app or coding agent in seconds. Route across frontier models through one endpoint — then launch your app and start building what's next.

Start building