Routing
JoyToken routing happens inside api-gateway. The gateway validates the API key and policy, converts the OpenAI-compatible request into router-service Route input, then uses the returned selected_model for wallet freeze, provider invocation, usage calculation, and billing records.
Routing Input
Chat requests are converted into router input:
Model Selection
Tier Resolution
Final allowed tiers come from:
- API key policy snapshot.
- API key
tier. - Request body
tier. - Wallet balance filtering when wallet quota is enabled.
If the request body contains an invalid tier, the gateway returns 403 policy_rejected. If the requested tier is outside the policy, the request is also rejected.
Policy Sent to Router
The gateway assembles policy from policy_snapshot_json and API key fields:
Wallet-Aware Fallback
If the request uses model: "auto" and the selected tier fails wallet freeze due to insufficient balance, the gateway can retry routing to another allowed tier.
This fallback only happens for model: "auto". Fixed model requests fail with 402 insufficient_quota when freeze fails.
Provider Request Body
Before calling provider-adapter, the gateway normalizes the provider body:
Response Metadata
Non-streaming responses merge routing metadata into the JSON body. Streaming responses append a metadata event before [DONE].
Common fields: