strategy.conditions: Query conditions with rules applied on metadata values or request parameters along with which target to call when the condition passes
strategy.default: The default target name to call when none of the conditions pass
targets: Array of target objects with unique names and provider details. These target names are referenced in the conditions objects above.
conditions and default are required params for the conditional strategy.
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.
from portkey_ai import Portkeyclient = Portkey( api_key="PORTKEY_API_KEY", config="model-routing-config-id")# This will use the "smartest" model (claude-3.5-sonnet)completion = client.chat.completions.create( model="smartest", # This value matches our conditional routing condition messages=[ {"role": "developer", "content": "You are a helpful assistant."}, {"role": "user", "content": "Explain quantum computing to a 5-year-old"} ])# This will use the "fastest" model (gpt-4o-mini)completion = client.chat.completions.create( model="fastest", # This value matches our conditional routing condition messages=[ {"role": "developer", "content": "You are a helpful assistant."}, {"role": "user", "content": "What's 2+2?"} ])
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.
Portkey support metadata-based routing, which requires sending custom metadata with your request.Applying Conditional Routing Based on Metadata
Simple Conditional Config
Nested Conditional Config
In 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.
You can combine metadata and parameter conditions for more sophisticated routing:
Copy
Ask AI
from portkey_ai import Portkeyclient = Portkey( api_key="PORTKEY_API_KEY", config="combined-routing-config-id")# Route based on both metadata and temperature parametercompletion = client.with_options( metadata={"user_tier": "enterprise"}).chat.completions.create( temperature=0.9, # High temperature will route to creative model messages=[{"role": "user", "content": "Write a story about dragons"}])
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 Subscription Tier
Regional Compliance
Data Sensitivity
Route premium users to advanced models and free users to cost-effective ones.
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.
You can also apply output guardrails to validate or transform the LLM’s response:
This configuration applies output guardrails to the “creative-model” target, which is used when the temperature parameter is high. The response from this model will be checked for factual accuracy and moderated for inappropriate content.
For comprehensive protection, you can apply both input and output guardrails to your targets:
This configuration provides end-to-end protection by:
First checking inputs with guardrails before sending to the LLM
Then validating outputs with guardrails before returning to the user
Each target can have its own set of guardrails, allowing you to apply different levels of protection based on your routing conditions.
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.
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.
Error Handling: If your condition references a parameter that doesn’t exist in the request, the request will fail with an error.
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.
Join us on Discord to share your thoughts on this feature or to get help with setting up advanced routing scenarios.