Skip to content

Routing best practices#

Practical patterns for Routing Rules after providers are connected. Use these examples with Add a routing rule and verify in Traces.

Request types for Chat and API#

The gateway distinguishes two chat request types in Rule Builder and CEL Expression Preview:

Table 1: Chat and API request types
Request type Typical traffic
Chat Completion Non-streaming chat completions (many API clients)
Chat Completion Stream Streaming chat completions (used by OptScale Chat)

Choose the condition that matches your clients:

  • Chat onlyrequest_type == "chat_completion_stream". Step-by-step: Add a single rule.

  • Chat and non-streaming API clientsrequest_type == "chat_completion" || request_type == "chat_completion_stream". Step-by-step: Combine conditions with OR.

A condition of only Request type = Chat Completion matches non-streaming API requests only. It does not match OptScale Chat.

Configure a target provider with a fallback#

Use this pattern when one provider should handle matching requests and another provider should take over if the target provider cannot process the request. The gateway first routes requests to the target provider and then tries the configured fallback provider if necessary.

Before you start

  • Two Active providers with the required models enabled.
  • Both providers (and their models) assigned to the test user in AI Access — Allowed providers. Otherwise, requests cannot be routed to the fallback provider during failover.

Navigate to ProvidersRouting Rules, then click + ADD. Configure the rule using the values shown in the following example.

Table 2: Example routing rule — target with fallback
Field Example value
Rule name org-chat-target-fallback
Description Route chat traffic to Provider A with fallback to Provider B
Enable Rule On
Scope Organization (leave Scope ID empty)
Priority 0 (evaluated before higher numbers in the same scope; use 100 only if you intentionally defer to rules in 0–99)
Rule Builder OR+ Add ruleRequest type = Chat Completion
+ Add ruleRequest type = Chat Completion Stream
CEL Expression Preview request_type == "chat_completion" || request_type == "chat_completion_stream"
Target Provider A / gpt-4o / Weight 1
Fallback Provider B / gpt-4o-mini

Create the rule by following Add a routing rule, then verify it in Traces. To apply the rule only to Chat requests, set Request type to Chat Completion Stream in Rule Builder.

Weighted split across providers#

Use this pattern to distribute matching traffic across two (or more) provider/model pairs by weight. The gateway picks one target per request using a weighted random choice—not a sticky session. Target weights in the create form must sum to 1.

Before you start

  • Two Active providers with the shared model enabled (for example gpt-4o on both).
  • Both providers (and their models) assigned to the test user in AI Access — Allowed providers. Otherwise, requests cannot be routed to the fallback provider during failover.
  • Optional fallback: configure a separate fallback destination for when the selected target fails. A fallback is not a third weight bucket.

Notes

  • In Rule Builder, Model = gpt-4o matches the model name part of the request.
  • Include both chat request types if you want Chat and non-streaming API clients—see Request types for Chat and API.
Table 3: Example routing rule — weighted split (70/30)
Field Example value
Rule name org-gpt4o-weighted-split
Description 70/30 split for gpt-4o across Provider A and Provider B
Enable Rule On
Scope Organization (leave Scope ID empty)
Priority 0 (or a higher number only if you intentionally defer to other matching rules)
Rule Builder AND+ Add rule group → select OR for the nested group (see Use a nested rule group):
+ Add ruleRequest type = Chat Completion
+ Add ruleRequest type = Chat Completion Stream
Then, in the outer AND group:
+ Add ruleModel = gpt-4o
CEL Expression Preview (request_type == "chat_completion" || request_type == "chat_completion_stream") && model == "gpt-4o"
Target 1 Provider A / gpt-4o / Weight 0.7
Target 2 Provider B / gpt-4o / Weight 0.3
Fallback Provider B / gpt-4o-mini

Create the rule by following Add a routing rule. Use + ADD TARGET to configure the second target. Then verify the rule in Traces. Over many requests, you should see traffic distributed across both targets; each individual request is routed to only one target.

For Chat-only traffic with a model filter, start with Add a single rule, then add + Add ruleModel = gpt-4o in the same AND group. The resulting expression is request_type == "chat_completion_stream" && model == "gpt-4o".

Prefer cheaper model, same provider#

Use when users select an expensive model and you want a cheaper default on the same provider connection.

Table 4: Example routing rule — downshift model
Field Example value
Rule name chat-downshift-gpt4o
Enable Rule On
Scope Organization
Priority 10
Rule Builder AND+ Add rule groupOR
+ Add ruleRequest type = Chat Completion
+ Add ruleRequest type = Chat Completion Stream
Back to the outer AND group → + Add ruleModel = gpt-4o
CEL Expression Preview (request_type == "chat_completion" || request_type == "chat_completion_stream") && model == "gpt-4o"
Target Provider A / gpt-4o-mini / Weight 1
Fallback Provider A / gpt-4o

Fall back to the original premium model if the cheaper path fails. Create with Add a routing rule, then verify.

Employee exception#

Use when one person must always hit a specific provider (VIP access or debugging).

Table 5: Example routing rule — employee exception
Field Example value
Rule name employee-staging-debug
Enable Rule On
Scope Employee
Scope ID Select a user from the list
Priority 0
Rule Builder OR+ Add ruleRequest type = Chat Completion
+ Add ruleRequest type = Chat Completion Stream
CEL Expression Preview request_type == "chat_completion" || request_type == "chat_completion_stream"
Target Staging provider / model / Weight 1
Fallback Empty, or a production backup

Keep employee rules narrow. Disable when finished instead of deleting if you may reuse them.

Multi-provider high availability#

Use when you want ordered recovery across vendors. Fallbacks are failure recovery, not a traffic split—use weights on Targets only when you need a 70/30-style distribution (see Weighted split).

Table 6: Example routing rule — primary with ordered backups
Field Example value
Rule name org-chat-ha
Enable Rule On
Scope Organization
Priority 0
Rule Builder OR+ Add ruleRequest type = Chat Completion
+ Add ruleRequest type = Chat Completion Stream
CEL Expression Preview request_type == "chat_completion" || request_type == "chat_completion_stream"
Target Provider A / gpt-4o / Weight 1
Fallbacks Provider B / gpt-4o, then Provider C / claude-… (try order = list order)

Same pattern as Configure a target provider with a fallback, with additional fallback rows via + ADD FALLBACK.

Migrate traffic by incoming provider#

Use this pattern when clients send requests to Provider A, but you want the gateway to route matching traffic to Provider B without requiring clients to change their configuration.

Table 7: Example routing rule — migrate traffic from Provider A
Field Example value
Rule name migrate-from-provider-a
Enable Rule On
Scope Organization
Priority 0
Rule Builder AND+ Add rule groupOR
+ Add ruleRequest type = Chat Completion
+ Add ruleRequest type = Chat Completion Stream
Back to the outer AND group → + Add ruleProvider = Provider A
CEL Expression Preview (request_type == "chat_completion" || request_type == "chat_completion_stream") && provider == "Provider A"
Target Provider B / same model / Weight 1
Fallback Provider A / same model

Create the rule by following Add a routing rule, then verify it in Traces. Requests that match the conditions are routed to Provider B. If Provider B cannot process a request, the gateway retries it using Provider A.

Match a model family with regex#

Use this pattern when multiple model IDs share a common prefix and an exact model match is too restrictive. In Rule Builder, use the regex operator for the Model field.

Table 8: Example routing rule — model family regex
Field Example value
Rule name chat-gpt4o-family
Enable Rule On
Scope Organization
Priority 10
Rule Builder AND+ Add rule groupOR
+ Add ruleRequest type = Chat Completion
+ Add ruleRequest type = Chat Completion Stream
Back to the outer AND group → + Add ruleModel → operation regex ^gpt-4o
CEL Expression Preview (request_type == "chat_completion" || request_type == "chat_completion_stream") && model.matches("^gpt-4o")
Target Preferred provider / pinned model / Weight 1

Create the rule by following Add a routing rule, then verify it in Traces.

Use an exact model match whenever possible. Use a regular expression only when you need to match multiple related model IDs.

Canary rollout on a team#

Use this pattern to test a new provider with a limited blast radius. Requests are distributed by weight; the selected target is determined independently for each request (not a sticky session).

Table 9: Example routing rule — team canary
Field Example value
Rule name team-gpt4o-canary
Enable Rule On
Scope Team
Scope ID Specify the pilot team
Priority 0
Rule Builder AND+ Add rule groupOR
+ Add ruleRequest type = Chat Completion
+ Add ruleRequest type = Chat Completion Stream
Back to the outer AND group → + Add ruleModel = gpt-4o
CEL Expression Preview (request_type == "chat_completion" || request_type == "chat_completion_stream") && model == "gpt-4o"
Target 1 Old provider / gpt-4o / Weight 0.9
Target 2 New provider / gpt-4o / Weight 0.1
Fallback Old provider / gpt-4o

Create the rule by following Add a routing rule. Use + ADD TARGET to configure the second target, then verify the rule in Traces.

Start with a pilot team. Expand the rule to the Organization scope only after validating the rollout in Traces and confirming that the required providers, models, AI Access settings, and budgets are configured correctly. See also Weighted split across providers.