Cookbooks
Cookbook: Dynamic model routing
The Model Filtering policy covers static allow and block lists, but model selection can also be code. A custom chain policy can pick the model per request based on price, capability, the calling app, or anything else in the request. This recipe routes every request to the cheapest active completions model.
Two public primitives from @zuplo/runtime do the work:
AIGatewayModels.load(context)returns the cached provider catalog, including each model's capability, status, and per-token pricing. Credentials are never returned.AIGatewayModelRouting.set(context, routing)validates the routing, resolves provider credentials internally, and stores the selection the AI Gateway handler uses.AIGatewayModelRouting.get(context)reads the current selection.
The policy
Code
Declare and use it
Declare the module in config/policies.json and push:
Code
Then add cheapest-model to an app's chain on its Policies tab, in place of
Model Filtering.
Routing precedence
Policy order determines which selection wins:
- Routing selected before Model Filtering stays authoritative—Model Filtering leaves an existing selection unchanged.
- Model Filtering creates routing when no earlier policy selected it.
- A custom policy placed after Model Filtering may deliberately replace that selection.
- If no policy selects routing, the handler derives it from the request's
providerName/model.
Prefer one policy as the primary selector so the route's intent is easy to understand.
Next steps
- Custom Policies: the full custom-policy quickstart
- Cookbook: Custom fallback logic: enrich a selection instead of creating one
- Policy Chains: how chain order works
Last modified on