# How the gateway routes

> How the sc gateway picks a profile, routes each Claude Code request by role, checks a profile before launch, and handles endpoint keys, key pools, retries and ports.
>
> The gateway sends each model request to the endpoint your profile names. Check a profile with sc profiles -v, then launch on it with sc -u.

1. List your profiles

   ```sh
   sc profiles
   ```

   This prints every profile and role with its model, effort and sampling.

2. See the URL and key behind each role

   ```sh
   sc profiles -v
   ```

   This adds each row's wire format (the API dialect it speaks) and auth, the reasoning fields it sends by default, and every endpoint with masked keys. Run it where nobody is reading your screen.

3. Launch on a profile

   ```sh
   sc -u <profile> <project> <agent>
   ```

   A profile that fails the [launch check](#the-launch-check) stops the launch before Claude Code starts.

The gateway is the part of the [daemon](/docs/concepts) that receives a session's model requests and forwards each one to an endpoint in `~/.superclaude/config.toml`. It reads the role from each request's `model` field, resolves it to an endpoint and model, translates the request if needed, adds that endpoint's own key, and streams the answer back in Anthropic format. Claude Code's own credential never reaches your endpoints.

| Profile | What `sc` does |
|---|---|
| `direct` | Launches plain Claude Code. No gateway in the request path. |
| Hybrid (some role on a `passthrough` endpoint) | Starts the daemon and checks the profile. Claude Code keeps its login and sends to `api.anthropic.com`; the daemon's local proxy passes passthrough and unmapped roles through unchanged and sends the rest to the gateway. |
| Full gateway (every role on your endpoints) | Starts the daemon, checks the profile, then points Claude Code at the daemon's loopback port. |

**Choose which profile a launch uses**

A launch uses the profile named by `-u`, or else the top-level `default_profile`:

```toml
default_profile = "claude-flash"
```

Unset, it is [`direct`](/docs/provider-anthropic#your-claude-subscription), plain Claude Code. `-p <name>` also selects a profile when `<name>` is one; any other value passes through to Claude Code's `--print`.

**Read the sc profiles listing**

The listing for the config `sc` writes on first run, without its closing `Launch:`, `Detail:` and `Filter:` lines:

```text
Profiles (4) — ~/.superclaude/config.toml
  default_profile: direct  (bare `sc` uses this; `-u <name>` overrides)

  PROFILE       ROLE    MODEL                         EFFORT  SAMPLING
  ────────────────────────────────────────────────────────────────────
  claude-flash  opus    anthropic/opus
                sonnet  opencode/deepseek-v4.1-flash  high
                haiku   opencode/deepseek-v4.1-flash  low
  coding        opus    opencode/glm-5.2              high
                sonnet  opencode/glm-5.2              high
                haiku   opencode/deepseek-v4-flash
  fast          opus    opencode/deepseek-v4-flash
                sonnet  opencode/deepseek-v4-flash
                haiku   opencode/deepseek-v4-flash
  research      opus    opencode/qwen3.7-max          high
                sonnet  opencode/deepseek-v4-pro              temp 0.7 repetition_penalty 1.05 top_k 40
                haiku   opencode/deepseek-v4-flash
```

`sc profiles <name>` and `sc profiles --role <tier>` narrow the listing. Reading it is on [Profiles](/docs/custom-profiles#write-your-own-profile).

`sc --list-models` (or `-lm`) prints the live model list of one endpoint: the `opus` endpoint of a non-`direct` `default_profile`, or the only endpoint when the file defines one.

**My launch fails before Claude Code starts**

### The launch check

Before Claude Code starts, the daemon resolves every mapped role except passthrough ones, requires a key or a signed-in OAuth identity for each endpoint, and probes key-based endpoints with `GET <url>/v1/models`. A missing key, an unreachable endpoint, or a `401` or `403` on both auth schemes fails the launch. Any other answer passes.

An endpoint whose model list is public, such as OpenCode or OpenRouter, accepts any key there, so a wrong key passes the check and fails on the first request. OAuth tokens are not probed; they are resolved and refreshed per request.

### Messages and what to do

| Symptom | Check |
|---|---|
| Launch fails before Claude Code starts | The launch check rejected the profile. The message names the endpoint and role. |
| `has no keys` | The `${VAR}` is empty in the daemon's environment, or the key file is empty. |
| A `401` on the first request, after a clean launch | A wrong key on a provider whose model list is public. |
| `run sc auth <endpoint> --label <label>` mid-session | That OAuth identity's token could not be used or refreshed. Sign it in again. |
| `defines no fable role` | A full-gateway profile got a Fable request; add a `fable` row. |
| `port already in use` on Linux | The default port sits in the ephemeral range; see [the port](#change-the-gateway-port). |

**My key from the environment is not picked up**

### Keys from the environment

`key_csv = "${VAR}"` is expanded in the daemon's own environment, inherited from the `sc` command that started it. A variable exported after the daemon started is invisible to it: export it, run `sc daemon stop`, and launch again. A `key_file` avoids this, because the gateway reads it on every request. Other `config.toml` edits apply on the next request. Never put a real key inline in `config.toml`.

**Write or change an endpoint**

### Endpoint keys

An endpoint is an `[endpoints.<name>]` table, and `<name>` is the prefix profile slugs use. The [provider pages](/docs/models#what-works) give a working block for each provider. Every table and key is listed on [config.toml](/docs/config-toml), and every flag on the [command reference](/docs/cli).

| Key | Values | Meaning |
|---|---|---|
| `url` | a base URL | Everything before the API path; a trailing `/` or `/v1` is dropped. |
| `format` | `anthropic-messages`, `openai-compat`, `openai-responses` | The endpoint's wire format. A profile row can override it per model. Default `openai-compat`. |
| `auth` | `bearer`, `x-api-key`, `oauth`, `passthrough` | How the credential is presented. Omit it and the format decides. |
| `key_csv` | a string, usually one `${VAR}` | Comma-separated keys. |
| `key_file` | a path, `~` allowed | A file of keys separated by commas or newlines, read on every request. |
| `type` | `single` (default), `pool` | A label `sc profiles -v` prints. The gateway rotates every key an endpoint resolves either way. |
| `retry` | `{ max, backoff_ms, jitter }` | The backoff shape for retries: `backoff_ms` (default 500) and `jitter` (`full`, `equal`, `decorrelated` or `none`). `max` is parsed, but the retry deadline bounds the attempts. |
| `retries` | an integer | Shorthand for `retry`; `retry` wins if both are set. |
| `headers` | a table of strings | Extra static headers on every request. |
| `rates."<model-id>"` | `{ cache_hit, cache_miss, output }` | USD per million tokens. Its presence turns on spend tracking for that endpoint, shown in the status line. |
| `on_unsupported_param` | `warn`, `error`, `drop` | Parsed but not enforced yet. |
| `[endpoints.<name>.oauth]` | `provider = "codex"` or `"grok"` | The subscription sign-in for `auth = "oauth"`; see [Codex](/docs/codex-setup) and [Grok](/docs/provider-grok). |

Keys from `key_csv` and `key_file` are trimmed, de-duplicated and kept in order; both may be set. A declared `key_file` that cannot be read fails the launch.

| `format` | Upstream route | Default `auth` |
|---|---|---|
| `anthropic-messages` | `POST <url>/v1/messages`, no translation | `x-api-key` |
| `openai-compat` | `POST <url>/v1/chat/completions` | `bearer` |
| `openai-responses` | `POST <url>/v1/responses`; `<url>/responses` for the `codex` provider | `bearer` |
| `gemini` | reserved; the gateway answers `501` | — |

**Rotate keys and tune retries**

### Key pools and retries

The gateway rotates through every key an endpoint resolves, moves past a rate-limited key, and retries transient failures inside a 60-second budget.

To pool keys, set one variable to a comma-separated list:

```sh
export OPENAI_KEYS="<key-1>,<key-2>,<key-3>"
```

```toml
[endpoints.openai]
type = "pool"
url = "https://api.openai.com"
format = "openai-compat"
auth = "bearer"
key_csv = "${OPENAI_KEYS}"
retry = { max = 3, backoff_ms = 500, jitter = "full" }
```

The gateway rotates through the live keys. On a `429` it reads the response:

- A `Retry-After` or `retry-after-ms` header is a short throttle. That key cools down for the time it names, and the request moves to the next key.
- No such header means a spent quota. The key leaves the rotation until the reset time the endpoint reports, or for 15 minutes, and that request is not retried on it.
- A pool of one key is never cooled. The gateway retries that key with backoff inside the retry budget, then returns the `429` with a `Retry-After`.
- While every key is cooling, requests get `429 all keys for this endpoint are rate-limited (cooling down)`.

For an OAuth endpoint the pool is its signed-in identities, one per `--label`.

### Retries

Every endpoint retries a `5xx`, `402`, `408`, `409`, a network error or a throttle on exponential backoff. One request's whole retry ladder is bounded at 60 seconds and 32 attempts; when the budget runs out, the error goes back to Claude Code. Other `4xx` answers are returned at once, because the same request would get the same answer.

```toml
[daemon]
retry_deadline_secs = 60
upstream_idle_timeout_secs = 1800
```

`retry_deadline_secs = 0` disables retries. `upstream_idle_timeout_secs` is the longest silence allowed between two bytes of an upstream response, not a limit on how long a response takes; `0` removes it.

**Which variables a full-gateway launch sets**

| Variable | Value |
|---|---|
| `ANTHROPIC_BASE_URL` | a loopback URL on the daemon's control port, 48770 by default |
| `ANTHROPIC_AUTH_TOKEN` | the placeholder `sc-go`; the daemon injects the real key upstream |
| `ANTHROPIC_CUSTOM_HEADERS` | `X-SC-Profile: <profile>`, which tells the daemon which profile to resolve |
| `ANTHROPIC_DEFAULT_OPUS_MODEL`, `_SONNET_`, `_HAIKU_`, `_FABLE_` | a label per role, `<profile> · <model-id> · <role>` |
| `ANTHROPIC_SMALL_FAST_MODEL` | the `haiku` label |
| `CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC` | `1` |

A hybrid launch sets only `ANTHROPIC_CUSTOM_HEADERS`. The gateway classifies the role from the model name by substring, which is why tier words in profile names misroute on a full gateway ([the warning](/docs/custom-profiles#write-your-own-profile)).

**Change the gateway port**

The gateway listens on the daemon's control port, loopback only, 48770 by default. Change it only if something else holds that port:

```toml
[daemon]
port = 48770
```

`$SUPERCLAUDE_DAEMON_PORT` overrides the key, and `sc --daemon-port <n>` sets that variable for one launch. A running daemon binds its port once, so a new port takes effect after it restarts. `sc daemon status` shows what is running. On Linux the default sits in the ephemeral port range; if you see `port already in use` there, move it below 32768.
