Conditional Routing
This feature is available on all Portkey plans.
Using Portkey Gateway, you can route your requests to different provider targets based on custom conditions you define. These can be conditions like:
- If this user is on the
paid plan
, route their request to acustom fine-tuned model
- If this user is an
EU resident
, call anEU hosted model
- If this user is a
beta tester
, send their request to thepreview model
- If the request is coming from
testing environment
with allm-pass-through
flag, route it to thecheapest model
- ..and more!
Using this strategy, you can set up various conditional checks on the metadata
keys you’re passing with your requests and route requests to the appropriate target — all happening very fast on the gateway, on edge.
Enabling Conditional Routing
Conditional routing is one of the strategies in Portkey’s Gateway Configs. (others being fallback
and loadbalance
). To use it in your app,
- You need to create a
conditional
config in Portkey UI. - Save the Config and get an associated Config ID.
- And just pass the Config ID along with your requests using the
config
param.
1. Creating the conditional
Config
Here’s how a sample conditional
config looks (along with its simpler, tree view).
strategy.mode
: Set toconditional
strategy.conditions
: Query conditions with rules applied on metadata values along with which target to call when the condition passesstrategy.default
: The default target name to call when none of the conditions passtargets
: Array of target objects with uniquenames
and provider details. These target names are referenced in theconditions
objects above.
conditions
and default
are required params for the conditional
strategy.
Structure of conditions
Object
conditions
are where you will actually write the routing rules. Here’s a sample condition
object:
query
: Write the exact rule for checking metadata values
then
: Define which target to call if the query PASSES
List of Condition Query Operators
Operator | Description |
---|---|
$eq | Equals |
$ne | Not equals |
$in | In array |
$nin | Not in array |
$regex | Match the regex |
$gt | Greater than |
$gte | Greater than or equal to |
$lt | Less than |
$lte | Less than or equal to |
Logical Query Operators
$and
: All conditions must be true$or
: At least one condition must be true
Example Condition objects with Logical Operators
- You can write nested queries (with
$and
,$or
operators) - When a condition is incorrect or it fails, Portkey moves on to the next condition until it finds a successful condition.
- If no conditions pass, then the
default
target name is called - Since Portkey iterates through the queries sequentially, the order of your conditions is important
2. Getting Config ID
Based on the conditions
and the Config structure described above, you can create your Config in Portkey UI, and save it to get Config ID. The UI also helps you autofill and autoformat your Config.
Adding the above sample condition to our final Config:
3. Using the Config ID in Requests
Now, while instantiating your Portkey client or while sending headers, you just need to pass the Config ID and all your requests will start getting routed according to your conditions.
Conditional routing happens on Portkey’s on-the-edge stateless AI Gateway. We scan for the given query field in your request body, apply the query condition, and route to the specified target based on it.
Currently, we support Metadata based routing — i.e. routing your requests based on the metadata values you’re sending along with your request.
Applying Conditional Routing Based on Metadata
Here, we’re using the following Config that we defined above.
More Examples Using Conditional Routing
Here are some examples on how you can leverage conditional routing to handle real-world scenarios like:
- Data sensititvity or data residency requirements
- Calling a model based on user’s input lanuage
- Handling feature flags for your app
- Managing traffic better at peak usage times
- ..and many more
Route your requests to different models based on the data sensitivity level
of the user.
Soon, Portkey will also support routing based on other critical parameters like input character count
, input token count
, prompt type
, tool support
, and more.
Similarly, we will also add support for smart routing to wider targets, like fastest
, cheapest
, highest uptime
, lowest error rate
, etc.
Please join us on Discord to share your thoughts on this feature and get early access to more routing capabilities.
Was this page helpful?