Skip to main content
Use this when you want explicit, deterministic routing — “try GPT-4o first, then Claude, then DeepSeek if both fail”. For semantic auto-routing (the model picks itself based on cluster + error profile), use load_router instead.

Constructor parameters

model_list (required)

A list of deployment configs. Each entry maps a logical alias (model_name) to a concrete provider/model:
Optional per-entry fields:

fallbacks

A list of {alias: [fallback_models]} maps:
Fallbacks are tried after all model_list deployments for the alias fail. They’re fully-qualified "provider/model" strings (not aliases).

strategy

How to order the deployments within one alias on each call: Strategy only changes which deployment is tried first; on failure the router falls through to the others.

num_retries

Retries per deployment before moving to the next. Default 2.

timeout

Per-request timeout in seconds. Default 120.

Methods

.completion(model, messages, **kwargs) → ModelResponse

Sync completion, same shape as ot.completion. The model argument is the alias (e.g. "smart"), not a provider/model string. **kwargs is passed through to the underlying ot.completion call.

.acompletion(model, messages, **kwargs) → ModelResponse

Async version of .completion. Same API, returns a coroutine.

Full example

How failure handling works

For a call to alias "smart" with num_retries=2:
  1. Order the "smart" deployments per strategy[D1, D2].
  2. Try D1 up to 1 + num_retries = 3 times; 300ms backoff between attempts.
  3. If all attempts on D1 fail, move to D2, try 3 times.
  4. If both deployments are exhausted, try each entry in fallbacks["smart"] exactly once.
  5. If everything fails, raise the last captured exception.
Stats per deployment are updated on every attempt (dep.requests, dep.errors, dep.total_latency_ms), which is how lowest-latency and least-cost strategies get their data.

When to use Router vs load_router

They compose: Router aliases can point at models that in turn go through the semantic router via "auto", so you can layer rule-based policy on top of learned routing.