This feature is available on all Portkey plans.
- If this user is on the
paid plan
, route their request to acustom fine-tuned model
- If the model parameter is set to
fastest
, route togpt-4o-mini
, ifsmartest
, route toopenai o1
- If this user is an
EU resident
, call anEU hosted model
- If the
temperature
parameter is above0.7
, route to a more creative model - If the request is coming from
testing environment
with allm-pass-through
flag, route it to thecheapest model
- ..and more!
- Metadata - Custom key-value pairs you pass with your requests
- Request Parameters - Any parameter that you send in your original LLM request (like model, temperature, max_tokens)
- Request URL Path - Match against
url.pathname
(for example, route/embeddings
requests)
Enabling Conditional Routing
Conditional routing is one of the strategies in Portkey’s Gateway Configs. (others beingfallback
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 or request parameters 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 or request parameters
then
: Define which target to call if the query PASSES
Supported Query Paths and Limits
metadata.<key>
: Looks upcontext.metadata[<key>]
. Keys must be flat (no additional dots) and values should be primitive types.params.<key>
: Looks upcontext.params[<key>]
. Only primitive values are supported for comparisons.url.pathname
: Matches against the full request path (for example,/admin/users
).
metadata.user_plan
, params.model
). Nested paths like metadata.features.new_model_enabled
are not supported.
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 |
$regex
uses JavaScript regular expressions with no flags. Matching is case-sensitive by default, and invalid patterns simply cause the condition to evaluate tofalse
.
Logical Query Operators
$and
: All conditions must be true$or
: At least one condition must be true
- You can write nested queries (with
$and
,$or
operators) - When a condition evaluates to
false
or is malformed, Portkey moves on to the next condition until it finds a successful one. - 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
- If a referenced key is missing (in
metadata
,params
, orurl
), the condition evaluates tofalse
; it does not throw an error.
2. Getting Config ID
Based on theconditions
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.
3. Implementing Conditional Routing
Conditional routing is enabled through Portkey’s Gateway Configs by following these steps:- Create a conditional config in Portkey UI (app.portkey.ai/configs)
- Save the config to get a Config ID
- Use this Config ID in your requests
Parameter-Based Routing
Route requests based on the values of parameters like
model
, temperature
, or max_tokens
Metadata-Based Routing
Route requests based on custom metadata fields you include with your request
Routing Based on Request Parameters
With conditional routing, you can route based on any parameter in your LLM request (model
, temperature
, max_tokens
, etc.).
Portkey only supports params routing for primitive types (string, number, boolean). Complex types like arrays and objects are not supported.
model
parameter, allowing you to use aliases instead of specific model names:
When using parameter-based routing, make sure to include the parameter specified in your routing condition in your request. In the example above, you need to include
model
in your request for routing to work properly.Routing Based on Metadata
Portkey support metadata-based routing, which requires sending custom metadata with your request. Applying Conditional Routing Based on MetadataIn this example we are routing our request based on
user_plan
metadata sent along request. If the user is on a paid
plan, we route to a finetuned-gpt4
model, otherwise we route to a base-gpt4
model.You can combine both metadata and request parameters in your conditions. For example, you can route based on a combination of
metadata.user_plan
and params.model
.Combined Routing with Multiple Conditions
You can combine metadata and parameter conditions for more sophisticated routing:More Examples Using Conditional Routing
Here are practical examples of how you can leverage conditional routing to handle real-world challenges in your LLM applications. These examples showcase common patterns that help optimize cost, performance, compliance, and feature deployment without changing your application code.User-Based Routing
User-Based Routing
Route premium users to advanced models and free users to cost-effective ones.
Model Selection & Parameter Routing
Model Selection & Parameter Routing
Use intuitive aliases instead of remembering specific model names.
Application Features & Testing
Application Features & Testing
Route different tasks to specialized models for optimal results.
URL Path Routing
URL Path Routing
Match requests based on the incoming request path.
Using Conditional Router with Guardrails
Using Conditional Router with Guardrails
You can apply input guardrails to specific targets in your conditional routing configuration. This allows you to validate or transform the input before it’s sent to the LLM:In this example, when a user with the “paid” plan sends a request, it’s routed to the premium model but first runs through two input guardrails: one for PII detection and another for toxic content filtering.
Guardrails are referenced by their IDs in the configuration. You can create guardrails in the Portkey UI and then reference them in your conditional routing config.Learn more about Portkey Guardrails to understand how to create and manage different types of guardrails for your LLM applications.
Important Note
- Parameter Presence: When routing based on request parameters, make sure the parameters specified in your conditions are included in your requests.
- Primitive Types Only: Parameter-based routing works with primitive data types (strings, numbers, booleans) but not with arrays or nested objects.
- Order Matters: Conditions are evaluated in the order they’re defined, so place more specific conditions before more general ones.
-
Missing Keys Do Not Error: If your condition references a parameter or metadata key that doesn’t exist in the request, the condition evaluates to
false
. The router tries the next condition, and if none pass, uses thedefault
target. -
Multiple Parameter Types: Portkey supports routing based on any parameter that can be sent in LLM requests including
model
,temperature
,top_p
,frequency_penalty
,presence_penalty
,max_tokens
, and many others. -
Supported Query Paths:
metadata.<key>
,params.<key>
, andurl.pathname
. Nested paths beyond these (for example,metadata.a.b
) are not supported.
Join us on Discord to share your thoughts on this feature or to get help with setting up advanced routing scenarios.