MixtrainDocs

The Router system enables intelligent model selection and traffic management. Route requests based on conditions, run A/B tests, and implement fallback strategies.

Quick Start

from mixtrain import Router

router = Router("image-gen-router")
result = router.route({"user": {"tier": "premium"}, "prompt": "A sunset"})

print(f"Matched rule: {result.matched_rule.name}")
print(f"Target: {result.selected_targets[0].model_name}")

Routing Strategies

StrategyDescription
SINGLERoute to a single target
SPLITA/B testing with weighted distribution
SHADOWPrimary + test targets for comparison
FALLBACKSequential fallback if primary fails

Key Concepts

Routing Rules

Rules define conditions and targets:

router = Router("image-gen-router")

# View current rules
for rule in router.rules:
    print(f"{rule['name']}: priority {rule['priority']}")
    for target in rule.get('targets', []):
        print(f"  -> {target['model_name']}")

Condition Operators

OperatorExample
equals{"field": "tier", "operator": "equals", "value": "premium"}
not_equals{"field": "status", "operator": "not_equals", "value": "blocked"}
greater_than{"field": "credits", "operator": "greater_than", "value": 100}
less_than{"field": "tokens", "operator": "less_than", "value": 1000}
is_in{"field": "region", "operator": "is_in", "value": ["us-east", "us-west"]}
contains{"field": "query", "operator": "contains", "value": "urgent"}
regex_match{"field": "id", "operator": "regex_match", "value": "^prod_"}

Version Management

Routers support versioning for safe deployments:

router = Router("video-gen-router")

# View version history
for v in router.versions:
    print(f"v{v['version']}: {v['change_message']}")

# Check active version
print(f"Active: v{router.active_version}")

# Deploy specific version
router.deploy(version=3)

Request History

Track routing decisions:

router = Router("image-gen-router")

requests = router.list_requests(limit=50)
for req in requests:
    print(f"{req['matched_rule']}: {req['selected_target']}")

Use Cases

A/B Testing Models

Split traffic between models to compare performance:

# 70% to flux-pro, 30% to stable-diffusion-xl
# Configure in web UI or via update()

Premium Tier Routing

Route premium users to higher-quality models:

result = router.route({
    "user": {"tier": "premium"},
    "prompt": "Generate an image"
})
# Routes to premium model based on rules

Fallback Strategy

Automatically fallback if primary model fails:

# Configure FALLBACK strategy
# Primary: hunyuan-video
# Fallback: veo-2

Next Steps

On this page