Skip to content

Create Routing Rule

Create a new routing rule for the authenticated user.

POST /v1/routing-rules
ParameterTypeRequiredDescription
namestringYesHuman-readable name for the rule (max 128 characters)
is_enabledbooleanYesWhether the rule is active
priorityintegerYesPriority for rule evaluation (-1000 to 1000, higher = first)
match_jsonobject|stringYesConditions that trigger the rule
action_jsonobject|stringYesActions to apply when rule matches
ConditionTypeDescriptionExample
containsstring or arrayText in input (case-insensitive)"code" or ["code", "bug"]
metadata_equalsobjectMetadata key-value pairs{"task": "analysis"}
modestringRouting mode equals"balanced", "cost", "quality", "latency"
taskstringShorthand for metadata_equals.task"coding"

Empty conditions ({}) match all requests.

ActionTypeDescriptionValid Values
set_providerstringForce specific provideropenai, anthropic, gemini, mistral, deepseek
set_modelstringForce specific modelAny valid model name
set_modestringOverride routing modebalance, cost, quality, latency
add_warningobjectAdd warning message{"message": "text"}
const rule = await client.routingRules.create({
name: 'Coding Tasks → OpenAI',
priority: 100,
is_enabled: true,
match_json: {
task: 'coding'
},
action_json: {
set_provider: 'openai',
set_model: 'gpt-4o'
}
});
console.log(rule.data.id); // Created rule ID
const rule = await client.routingRules.create({
name: 'Documentation → Cost Mode',
priority: 50,
is_enabled: true,
match_json: {
contains: ['documentation', 'docs', 'readme']
},
action_json: {
set_mode: 'cost'
}
});
const rule = await client.routingRules.create({
name: 'Always Warn About Beta',
priority: -100,
is_enabled: true,
match_json: {}, // Match all requests
action_json: {
add_warning: {
message: 'Beta routing engine in use'
}
}
});
{
"data": {
"id": 123,
"user_id": 456,
"name": "Coding Tasks → OpenAI",
"is_enabled": true,
"priority": 100,
"match_json": {
"task": "coding"
},
"action_json": {
"set_provider": "openai",
"set_model": "gpt-4o"
},
"created_at": "2025-11-10T12:00:00Z",
"updated_at": "2025-11-10T12:00:00Z"
}
}
{
"error": {
"message": "Routing rules are not available on the free plan",
"type": "authorization_error",
"http_status": 403
}
}
{
"error": {
"message": "Validation failed",
"type": "validation_error",
"http_status": 422,
"errors": {
"priority": ["The priority must be between -1000 and 1000"]
}
}
}
  1. Use descriptive names - Make rule purposes clear
  2. Set appropriate priorities - Higher values for more specific rules
  3. Test with is_enabled: false - Test rules before activating
  4. Use specific conditions - Avoid overly broad match conditions
  5. Combine actions carefully - Only first provider/model/mode applies