Skip to content

Routing and usage

For every request, build the candidate list from upstream keys dedicated to the authenticated product. A route or policy can narrow that list. It cannot grant access to another product’s key.

  1. Read product and policy from the authenticated product key. Match the requested model and endpoint type. Consider an exact model mapping before a wildcard default of the same type. Deduplicate credentials reachable through both.
  2. Filter by owner, enabled route, supported model and operation, and key status. Skip disabled keys, unexpired cooldowns, and credentials with a known full quota window. For media, match the actual operation, such as speech or transcription.
  3. Apply the product key’s policy to the remaining eligible IDs. With an empty policy use global route priority within the eligible set. An explicit policy with zero usable candidates is exhausted.
  4. Try an eligible candidate and record its outcome. On a configured failure, recheck eligibility, cooldown, remaining attempts, timeout, and whether the request can be replayed. Discard failed response resources before moving on.
  5. Once the downstream response is committed, finish or terminate that response. A failure before commitment can trigger fallback. Stop on exhaustion and return an OpenAI-shaped error. A streamed upload whose body can no longer be replayed also stops the loop.

Example. Product A owns keys A1 and A2. Product B owns B1, which happens to rank first for the same model. When A1 is exhausted, a request from A may try A2. When A2 is also exhausted, A receives an error. B1 receives zero requests.

The example IDs both belong to the same product. The retry count limits attempts after the initial call. Use a request deadline to bound total latency. The values below illustrate a configuration.

{
"strategy": "weighted",
"candidates": [
{"provider_key_id": "product-a-primary", "weight": 70},
{"provider_key_id": "product-a-secondary", "weight": 30}
],
"fallback_on": ["429", "402", "401", "5xx", "timeout", "quota_window_full"],
"retry": {"max_retries": 2, "timeout_ms": 30000},
"sticky": false
}
Strategy Selection rule
priority Try eligible keys in configured order.
round_robin Rotate among eligible keys. An atomic D1 counter holds the position per product key.
weighted Select among eligible keys in proportion to their positive weights. Recompute when a key drops out.
least_latency Prefer the eligible key with the lowest recent observed latency. Keep enough measurement context to avoid promoting a key solely because it lacks samples.

A sticky preference, if enabled, still goes through ownership and health checks. A provider 429 or 402 can trigger cooldown and fallback. A provider 401 signals a credential problem requiring inspection. Client authentication and allowlist failures terminate at the gateway. An upstream validation error usually calls for a corrected request.

For streams, give each attempt a deadline for its first usable chunk. The baseline suggests 15 seconds as a starting point. Keep this within the overall request deadline. For timeouts and transient server failures, retry only where the operation is safe to repeat. A timeout on a billable operation can leave the upstream outcome unknown.

Adapter Supported work in the design
OpenAI / OpenRouter Chat and supported compatible APIs, including catalog passthrough for OpenRouter. Optional OpenRouter attribution headers are HTTP-Referer and X-Title.
Mistral Reuse the compatible adapter with the configured base URL and advertised model capabilities.
Anthropic Native /v1/messages with Anthropic request and response bodies. Chat completions use Anthropic’s compatibility endpoint and inherit its feature limits. Use Messages for native prompt caching. Anthropic compatibility
Voyage Embeddings and rerank. Map the chosen operation explicitly to the provider’s format.
Gemini Optional native format conversion in phase 8 when needed.
Media Preserve binary bodies, MIME types, and streaming. The initial transcription contract uses the OpenAI-compatible format.
Self-hosted sub2api, CLIProxyAPI, and SubLane use a configured base URL and dedicated key. Probe actual supported endpoints and configure quota sources per service.

Self-hosted capability and quota discovery

Section titled “Self-hosted capability and quota discovery”

On registration, probe the model catalog and use small, bounded fixtures for chat, Messages, embeddings, rerank, and transcription. Persist the observed capabilities in provider_keys.usage_config. Where discovery is inconclusive, allow only explicitly configured passthrough operations on the registered base URL.

Every five minutes the scheduler probes up to 20 credentials, oldest observation first. Set SELF_HOSTED_ORIGINS to a comma-separated list of approved HTTPS origins before registering a self-hosted base URL. Put the optional management token in the separate usage_auth field. Each service can return the normalized windows object or use explicit JSONPath field mappings for its installed version. The poller extracts used units, capacity, and the next reset for five_hour, weekly, and monthly windows. Keep the management credential encrypted. On schema changes, mark the observation stale, alert the operator, and update the mapping. Never replace an unreadable observation with a fabricated zero.

A known full window makes the key ineligible until its reset. Refresh state before returning a cooled key to normal use. Concurrent calls can overrun an observed allowance between polls. Surface its age and reset countdown in the console. Keep experimental subscription proxies low in priority, respect service terms, and give critical routes an official credential owned by the same product.

See the gateway API for client requests and the console API for route setup.