When to use selection strategies
Selection strategies are useful when:- Clients use
ngrok/autoor omit the model field - You want to prefer certain models over others
- You need performance-based routing (lowest latency, lowest error rate)
- You want cost optimization (cheapest models first)
- You need feature-based filtering (only models with tool calling)
Basic configuration
Define strategies in your Traffic Policy:How strategies execute
Strategies execute in order until one returns at least one model:ai.models) to ensure requests don’t fail.
Client model priority
When clients specify models in their request, selection strategies act as filters only:- Strategies can filter OUT models but cannot ADD models the client didn’t request
- The client’s preferred order is preserved (
modelfield first, thenmodelsarray entries) - If strategies filter out all client-specified models, the request fails with an error
gpt-4o, they won’t unexpectedly get claude-3-5-sonnet even if your strategy prefers Anthropic.
Common patterns
Provider priority
Prefer a specific provider, with fallbacks:Cost optimization
Prefer cheaper models:Performance-based
Prefer low-latency, reliable models:Feature-based
Only models with specific capabilities:Geographic filtering
Only models in specific regions:Metadata-based
Use custom metadata for filtering:Known models only
Reject unknown pass-through models (only allow models in the catalog):Available functions
Filtering
| Function | Description | Example |
|---|---|---|
filter(predicate) | Filter by condition | ai.models.filter(m, m.provider_id == 'openai') |
only(ids) | Include only specific models | ai.models.only(['gpt-4o', 'claude-3-5-sonnet']) |
ignore(ids) | Exclude specific models | ai.models.ignore(['gpt-3.5-turbo']) |
onlyProviders(ids) | Include only specific providers | ai.models.onlyProviders(['openai', 'anthropic']) |
ignoreProviders(ids) | Exclude specific providers | ai.models.ignoreProviders(['google']) |
onlyAuthors(ids) | Include only specific authors | ai.models.onlyAuthors(['openai']) |
ignoreAuthors(ids) | Exclude specific authors | ai.models.ignoreAuthors(['meta']) |
Geographic
| Function | Description | Example |
|---|---|---|
inRegion(code) | Models available in region | ai.models.inRegion('us-east-1') |
inCountryCode(code) | Models available in country | ai.models.inCountryCode('US') |
Cost
| Function | Description | Example |
|---|---|---|
underCost(type, max) | Models under price threshold | ai.models.underCost('text.input', 1.0) |
sortBy('price') | Sort by price (cheapest first) | ai.models.sortBy('price') |
Selection
| Function | Description | Example |
|---|---|---|
random() | Select one random model | ai.models.random() |
randomize() | Shuffle model order | ai.models.randomize() |
[index] | Select by index | ai.models.filter(...)[0] |
Lookup
| Function | Description | Example |
|---|---|---|
get(providerId, modelId) | Get specific model | ai.models.get('openai', 'gpt-4o') |
getMetadata(key) | Get metadata value | m.getMetadata('tier') |
Available model variables
When usingfilter(), these variables are available on each model m:
| Variable | Type | Description |
|---|---|---|
m.id | string | Model identifier |
m.provider_id | string | Provider identifier |
m.author_id | string | Model author identifier |
m.display_name | string | Human-readable name |
m.known | boolean | Whether this model is in the catalog (false for unknown pass-through models) |
m.custom | boolean | Whether this is a custom-configured model |
m.metadata | object | Custom metadata from config |
m.input_modalities | list | Input types (“text”, “image”, etc.) |
m.output_modalities | list | Output types |
m.supported_features | list | Features (“tool-calling”, “coding”, etc.) |
m.max_context_window | number | Maximum context window size |
m.max_output_tokens | number | Maximum output tokens |
Metrics variables
Access performance metrics viam.metrics:
CEL operators
| Operator | Description | Example |
|---|---|---|
==, != | Equality | m.provider_id == 'openai' |
<, >, <=, >= | Comparison | m.metrics.global.latency.upstream_ms_avg < 1000 |
&& | Logical AND | m.provider_id == 'openai' && m.id.contains('gpt-4') |
|| | Logical OR | m.id.contains('mini') || m.id.contains('turbo') |
! | Logical NOT | !m.custom |
in | List membership | 'image' in m.input_modalities |
String functions
| Function | Description | Example |
|---|---|---|
contains() | Check substring | m.id.contains('gpt') |
startsWith() | Check prefix | m.id.startsWith('gpt-4') |
endsWith() | Check suffix | m.id.endsWith('-turbo') |
Complete example
Next steps
- CEL Functions Reference - Complete function reference
- Model Filtering Example - More examples
- Metrics Reference - Understanding metrics