Portkey Docs
HomeAPIIntegrationsChangelog
  • Introduction
    • What is Portkey?
    • Make Your First Request
    • Feature Overview
  • Integrations
    • LLMs
      • OpenAI
        • Structured Outputs
        • Prompt Caching
      • Anthropic
        • Prompt Caching
      • Google Gemini
      • Groq
      • Azure OpenAI
      • AWS Bedrock
      • Google Vertex AI
      • Bring Your Own LLM
      • AI21
      • Anyscale
      • Cerebras
      • Cohere
      • Fireworks
      • Deepbricks
      • Deepgram
      • Deepinfra
      • Deepseek
      • Google Palm
      • Huggingface
      • Inference.net
      • Jina AI
      • Lingyi (01.ai)
      • LocalAI
      • Mistral AI
      • Monster API
      • Moonshot
      • Nomic
      • Novita AI
      • Ollama
      • OpenRouter
      • Perplexity AI
      • Predibase
      • Reka AI
      • SambaNova
      • Segmind
      • SiliconFlow
      • Stability AI
      • Together AI
      • Voyage AI
      • Workers AI
      • ZhipuAI / ChatGLM / BigModel
      • Suggest a new integration!
    • Agents
      • Autogen
      • Control Flow
      • CrewAI
      • Langchain Agents
      • LlamaIndex
      • Phidata
      • Bring Your own Agents
    • Libraries
      • Autogen
      • DSPy
      • Instructor
      • Langchain (Python)
      • Langchain (JS/TS)
      • LlamaIndex (Python)
      • LibreChat
      • Promptfoo
      • Vercel
        • Vercel [Depricated]
  • Product
    • Observability (OpenTelemetry)
      • Logs
      • Tracing
      • Analytics
      • Feedback
      • Metadata
      • Filters
      • Logs Export
      • Budget Limits
    • AI Gateway
      • Universal API
      • Configs
      • Multimodal Capabilities
        • Image Generation
        • Function Calling
        • Vision
        • Speech-to-Text
        • Text-to-Speech
      • Cache (Simple & Semantic)
      • Fallbacks
      • Automatic Retries
      • Load Balancing
      • Conditional Routing
      • Request Timeouts
      • Canary Testing
      • Virtual Keys
        • Budget Limits
    • Prompt Library
      • Prompt Templates
      • Prompt Partials
      • Retrieve Prompts
      • Advanced Prompting with JSON Mode
    • Guardrails
      • List of Guardrail Checks
        • Patronus AI
        • Aporia
        • Pillar
        • Bring Your Own Guardrails
      • Creating Raw Guardrails (in JSON)
    • Autonomous Fine-tuning
    • Enterprise Offering
      • Org Management
        • Organizations
        • Workspaces
        • User Roles & Permissions
        • API Keys (AuthN and AuthZ)
      • Access Control Management
      • Budget Limits
      • Security @ Portkey
      • Logs Export
      • Private Cloud Deployments
        • Architecture
        • AWS
        • GCP
        • Azure
        • Cloudflare Workers
        • F5 App Stack
      • Components
        • Log Store
          • MongoDB
    • Open Source
    • Portkey Pro & Enterprise Plans
  • API Reference
    • Introduction
    • Authentication
    • OpenAPI Specification
    • Headers
    • Response Schema
    • Gateway Config Object
    • SDK
  • Provider Endpoints
    • Supported Providers
    • Chat
    • Embeddings
    • Images
      • Create Image
      • Create Image Edit
      • Create Image Variation
    • Audio
      • Create Speech
      • Create Transcription
      • Create Translation
    • Fine-tuning
      • Create Fine-tuning Job
      • List Fine-tuning Jobs
      • Retrieve Fine-tuning Job
      • List Fine-tuning Events
      • List Fine-tuning Checkpoints
      • Cancel Fine-tuning
    • Batch
      • Create Batch
      • List Batch
      • Retrieve Batch
      • Cancel Batch
    • Files
      • Upload File
      • List Files
      • Retrieve File
      • Retrieve File Content
      • Delete File
    • Moderations
    • Assistants API
      • Assistants
        • Create Assistant
        • List Assistants
        • Retrieve Assistant
        • Modify Assistant
        • Delete Assistant
      • Threads
        • Create Thread
        • Retrieve Thread
        • Modify Thread
        • Delete Thread
      • Messages
        • Create Message
        • List Messages
        • Retrieve Message
        • Modify Message
        • Delete Message
      • Runs
        • Create Run
        • Create Thread and Run
        • List Runs
        • Retrieve Run
        • Modify Run
        • Submit Tool Outputs to Run
        • Cancel Run
      • Run Steps
        • List Run Steps
        • Retrieve Run Steps
    • Completions
    • Gateway for Other API Endpoints
  • Portkey Endpoints
    • Configs
      • Create Config
      • List Configs
      • Retrieve Config
      • Update Config
    • Feedback
      • Create Feedback
      • Update Feedback
    • Guardrails
    • Logs
      • Insert a Log
      • Log Exports [BETA]
        • Retrieve a Log Export
        • Update a Log Export
        • List Log Exports
        • Create a Log Export
        • Start a Log Export
        • Cancel a Log Export
        • Download a Log Export
    • Prompts
      • Prompt Completion
      • Render
    • Virtual Keys
      • Create Virtual Key
      • List Virtual Keys
      • Retrieve Virtual Key
      • Update Virtual Key
      • Delete Virtual Key
    • Analytics
      • Graphs - Time Series Data
        • Get Requests Data
        • Get Cost Data
        • Get Latency Data
        • Get Tokens Data
        • Get Users Data
        • Get Requests per User
        • Get Errors Data
        • Get Error Rate Data
        • Get Status Code Data
        • Get Unique Status Code Data
        • Get Rescued Requests Data
        • Get Cache Hit Rate Data
        • Get Cache Hit Latency Data
        • Get Feedback Data
        • Get Feedback Score Distribution Data
        • Get Weighted Feeback Data
        • Get Feedback Per AI Models
      • Summary
        • Get All Cache Data
      • Groups - Paginated Data
        • Get User Grouped Data
        • Get Model Grouped Data
        • Get Metadata Grouped Data
    • API Keys [BETA]
      • Update API Key
      • Create API Key
      • Delete an API Key
      • Retrieve an API Key
      • List API Keys
    • Admin
      • Users
        • Retrieve a User
        • Retrieve All Users
        • Update a User
        • Remove a User
      • User Invites
        • Invite a User
        • Retrieve an Invite
        • Retrieve All User Invites
        • Delete a User Invite
      • Workspaces
        • Create Workspace
        • Retrieve All Workspaces
        • Retrieve a Workspace
        • Update Workspace
        • Delete a Workspace
      • Workspace Members
        • Add a Workspace Member
        • Retrieve All Workspace Members
        • Retrieve a Workspace Member
        • Update Workspace Member
        • Remove Workspace Member
  • Guides
    • Getting Started
      • A/B Test Prompts and Models
      • Tackling Rate Limiting
      • Function Calling
      • Image Generation
      • Getting started with AI Gateway
      • Llama 3 on Groq
      • Return Repeat Requests from Cache
      • Trigger Automatic Retries on LLM Failures
      • 101 on Portkey's Gateway Configs
    • Integrations
      • Llama 3 on Portkey + Together AI
      • Introduction to GPT-4o
      • Anyscale
      • Mistral
      • Vercel AI
      • Deepinfra
      • Groq
      • Langchain
      • Mixtral 8x22b
      • Segmind
    • Use Cases
      • Few-Shot Prompting
      • Enforcing JSON Schema with Anyscale & Together
      • Detecting Emotions with GPT-4o
      • Build an article suggestion app with Supabase pgvector, and Portkey
      • Setting up resilient Load balancers with failure-mitigating Fallbacks
      • Run Portkey on Prompts from Langchain Hub
      • Smart Fallback with Model-Optimized Prompts
      • How to use OpenAI SDK with Portkey Prompt Templates
      • Setup OpenAI -> Azure OpenAI Fallback
      • Fallback from SDXL to Dall-e-3
      • Comparing Top10 LMSYS Models with Portkey
      • Build a chatbot using Portkey's Prompt Templates
  • Support
    • Contact Us
    • Developer Forum
    • Common Errors & Resolutions
    • December '23 Migration
    • Changelog
Powered by GitBook
On this page
  • Enabling Conditional Routing
  • 1. Creating the conditional Config
  • Structure of conditions Object
  • List of Condition Query Operators
  • Logical Query Operators
  • 2. Getting Config ID
  • 3. Using the Config ID in Requests
  • Applying Conditional Routing Based on Metadata
  • More Examples Using Conditional Routing

Was this helpful?

Edit on GitHub
  1. Product
  2. AI Gateway

Conditional Routing

PreviousLoad BalancingNextRequest Timeouts

Last updated 9 months ago

Was this helpful?

This feature is available on all Portkey .

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 a custom fine-tuned model

  • If this user is an EU resident, call an EU hosted model

  • If this user is a beta tester, send their request to the preview model

  • If the request is coming from testing environment with a llm-pass-through flag, route it to the cheapest 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 . (others being fallback and loadbalance). To use it in your app,

  1. You need to create a conditional config in Portkey UI.

  2. Save the Config and get an associated Config ID.

  3. 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": "conditional",
		"conditions": [
			...conditions
		],
		"default": "target_1"
	},
	"targets": [
		{
			"name": "target_1",
			"virtual_key":"xx"
		},
		{
			"name": "target_2",
			"virtual_key":"yy"
		}
	]
}
│─ config
│  │─ strategy mode:conditional
│  │  │─ conditions 
│  │  │  │─ array of conditions
│  │  │─ default 
│  │  │  │─ target name
│  │─ targets
│  │  │─ target 1
│  │  │  │─ name
│  │  │  │─ provider details
│  │  │─ target 2
│  │  │  │─ name
│  │  │  │─ provider details
  • strategy.mode: Set to conditional

  • strategy.conditions: Query conditions with rules applied on metadata values 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.

Structure of conditions Object

conditions are where you will actually write the routing rules. Here's a sample condition object:

{
  "query": { "metadata.user_plan": { "$eq": "paid" } },
  "then": "finetuned-gpt4"
}
  • 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

{
  "$and": [
    { "metadata.user_type": { "$eq": "pro" } },
    { "metadata.model": { "$eq": "gpt-4" } }
  ]
}
{
  "$or": [
    { "metadata.user_type": { "$eq": "pro" } },
    { "metadata.user_quota": { "$eq": "premium" } }
  ]
}
{
  "$or": [
    {
      "$and": [
        { "metadata.user_type": { "$eq": "pro" } },
        { "metadata.user_tier": { "$eq": "tier-1" } }
      ]
    },
    { "metadata.user_quota": { "$eq": "premium" } }
  ]
}
  1. You can write nested queries (with $and, $or operators)

  2. When a condition is incorrect or it fails, Portkey moves on to the next condition until it finds a successful condition.

  3. If no conditions pass, then the default target name is called

  4. Since Portkey iterates through the queries sequentially, the order of your conditions is important


2. Getting Config ID

Adding the above sample condition to our final Config:

{
	"strategy": {
		"mode": "conditional",
		"conditions": [
			{
			  "query": { "metadata.user_plan": { "$eq": "paid" } },
			  "then": "finetuned-gpt4"
			},
			{
			  "query": { "metadata.user_plan": { "$eq": "free" } },
			  "then": "base-gpt4"
			}			
		],
		"default": "base-gpt4"
	},
	"targets": [
		{
			"name": "finetuned-gpt4",
			"virtual_key":"xx"
		},
		{
			"name": "base-gpt4",
			"virtual_key":"yy"
		}
	]
}
{
  "strategy": {
    "mode": "conditional",
    "conditions": [
      {
        "query": {
          "$and": [
            { "metadata.user_type": { "$eq": "pro" } },
            { "metadata.user_tier": { "$eq": "tier-1" } }
          ]
        },
        "then": "gpt4_v2_target"
      },
      {
        "query": {
          "$or": [
            { "metadata.client": { "$eq": "UI" } },
            { "metadata.app_name": { "$regex": "my_app" } }
          ]
        },
        "then": "app_target"
      }
    ],
    "default": "default_target"
  },
  "targets": [
    { "name": "gpt4_v2_target", "virtual_key": "openai-xx"},
    { "name": "app_target", "virtual_key": "openai-yy" },
    { "name": "default_target", "virtual_key": "openai-zz" }
  ]
}

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

from portkey_ai import Portkey

portkey = Portkey(
    api_key="PORTKEY_API_KEY",
    config="my-conditional-router-config"
)

response = portkey.with_options(
    metadata = {
        "user_plan": "free",
        "environment": "production",
        "session_id": "1729"
}).chat.completions.create(
    messages = [{ "role": 'user', "content": 'What is 1729' }]
)

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.

{
	"strategy": {
		"mode": "conditional",
		"conditions": [
			{
				"query": {
					"metadata.data_sensitivity": "high"
				},
				"then": "on-premises-model"
			},
			{
				"query": {
					"metadata data_sensitivity": {
						"$in": [
							"medium",
							"low"
						]
					}
				},
				"then": "cloud-model"
			}
		],
		"default": "public-model"
	},
	"targets": [
		{
			"name": "public-model",
			"virtual_key": "..."
		},
		{
			"name": "on-premises-model",
			"virtual_key": "..."
		},
		{
			"name": "cloud-model",
			"virtual_key": "..."
		}
	]
}

Implement feature flags to gradually roll out new AI models.

{
	"strategy": {
		"mode": "conditional",
		"conditions": [
			{
				"query": {
					"metadata.user_id": {
						"$in": [
							"beta-tester-1",
							"beta-tester-2",
							"beta-tester-3"
						]
					}
				},
				"then": "new-experimental-model"
			},
			{
				"query": {
					"metadata.feature_flags.new_model_enabled": true
				},
				"then": "new-stable-model"
			}
		],
		"default": "current-production-model"
	},
	"targets": [
		{
			"name": "current-production-model",
			"virtual_key": "..."
		},
		{
			"name": "new-experimental-model",
			"virtual_key": "..."
		},
		{
			"name": "new-stable-model",
			"virtual_key": "..."
		}
	]
}

Route to different models based on time of day for optimal performance at peak usage times.

{
	"strategy": {
		"mode": "conditional",
		"conditions": [
			{
				"query": {
					"metadata.request_time": {
						"$gte": "09:00",
						"$lt": "17:00"
					}
				},
				"then": "high-capacity-model"
			}
		],
		"default": "standard-model"
	},
	"targets": [
		{
			"name": "high-capacity-model",
			"virtual_key": "...",
			"override_params": {"model":"gpt-4o-mini"}
			
		},
		{
			"name": "standard-model",
			"virtual_key": "...",
			"override_params": {"model":"gpt-4o"}
		}
	]
}

Route requests to language-specific models based on detected input language.

{
	"strategy": {
		"mode": "conditional",
		"conditions": [
			{
				"query": {
					"metadata.detected_language": {
						"$in": [
							"'en",
							"fr",
							"de"
						]
					}
				},
				"then": "multilingual-model"
			},
			{
				"query": {
					"metadata.detected_language": "zh"
				},
				"then": "chinese-specialized-model"
			}
		],
		"default": "general-purpose-model"
	},
	"targets": [
		{
			"name": "multilingual-model",
			"virtual_key": "..."
		},
		{
			"name": "chinese-specialized-model",
			"virtual_key": "..."
		},
		{
			"name": "general-purpose-model",
			"virtual_key": "..."
		}
	]
}

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.

Based on the conditions and the Config structure described above, you can create your , and save it to get Config ID. The UI also helps you autofill and autoformat your Config.

.

to share your thoughts on this feature and get early access to more routing capabilities.

plans
Gateway Configs
Config in Portkey UI
Please join us on Discord
Here, we're using the following Config that we defined above