- Segment by user tier — route premium users to flagship models and free users to cost-effective ones.
- Route by geography — direct requests to different models based on geography.
- Optimize by complexity — send simple prompts to fast, cheap models and complex ones to high-capability models.
Basic Structure
Routes are defined directly on the Router. Each route has aroute object and a condition object.
Example router set up
Example router set up
condition field accepts a CEL expression that can be evaluated against the following from each Chat Completions request:
- Request metadata — key-value pairs sent in
extra_body.metadata(e.g. user tier, region, complexity score). - Messages — the full
messagesarray from the request body
| Operation | Example | Description |
|---|---|---|
| Equality | tier == "premium" | Matches a specific value. |
| Comparison | request_count > 100 | Numeric comparisons (>, <, >=, <=). |
| Logical AND | tier == "premium" && region == "us-east" | Both conditions must be true. |
| Logical OR | tier == "premium" || tier == "enterprise" | Either condition must be true. |
| Range check | score >= 80 && score <= 100 | Matches values within a range. |
startsWith() | user_id.startsWith("enterprise-") | Checks if a string starts with a prefix. |
endsWith() | user_id.endsWith("-admin") | Checks if a string ends with a suffix. |
contains() | user_id.contains("test") | Checks if a string contains a substring. |
matches() | messages.last().content.matches("(?i).*\\bcode\\b.*") | RE2 regex match on any string field. |
size() | user_id.size() > 10 | Gets the length of a string. |
messages.has() | messages.has() | true if the message array exists and is non-empty. |
messages.first() | messages.first().content.contains("system") | First message in the array. |
messages.last() | messages.last().content.matches("(?i).*help.*") | Last message in the array. |
messages.get(i) | messages.get(0).role == "system" | Message at index i (0-based). |
messages.len() | messages.len() > 3 | Number of messages in the array. |
Expressions are typed — ensure your metadata types match. Use
count == 100 when the value is a number and tier == "premium" when it’s a string. Mismatched types will not match.Making a Request
When calling your router, pass metadata in your requests usingextra_body.metadata to provide the values referenced by your conditions, enabling dynamic route selection.
tier, user_id, and region) will be available for the router’s CEL expressions, enabling conditional routes to match and select the appropriate model configuration based on those values.
Example Use Cases
User Subscription Tiers
Route users to different models based on their subscription tier:Geographic Routing
Route requests based on user location:Prompt-Based Routing
You can combine fast prompt-based regex routing with metadata checks in a single condition, enabling routing by both what the user is asking and who the user is. Route coding questions from premium users to a reasoning model:Conditions are evaluated before route variants are selected, so messages defined inside variants are not part of condition evaluation.