Provider Routing & Fallback
Advanced model and runtime routing with strategy-based selection and automatic failover.
3 min read
This is a migrated reference document from the original Anvio source repo. Content is preserved as-is; canonical documentation lives in the sections above in the sidebar.
Original path: docs/36-provider-routing.md
Advanced model and runtime routing with strategy-based selection and automatic failover.
Routing Strategies
| Strategy | Optimizes For | Selection Criteria |
|---|---|---|
cheapest | Cost | Lowest $/token |
fastest | Latency | Historical p95 latency |
highest_quality | Quality | Capability tier + benchmark scores |
coding_optimized | Code tasks | Models tuned for code |
research_optimized | Research tasks | Long context + reasoning |
Configuration
# workspace/providers/routing.yaml
apiVersion: anvio.io/v1
kind: ProviderRouting
spec:
defaultStrategy: highest_quality
routes:
coding:
strategy: coding_optimized
primary:
provider: anthropic
model: claude-sonnet-4-20250514
pool: anthropic
fallback:
- provider: openai
model: gpt-4o
pool: openai
- provider: gemini
model: gemini-2.0-pro
pool: gemini
review:
strategy: highest_quality
primary:
provider: cursor
runtime: cursor
fallback:
- provider: anthropic
model: claude-sonnet-4-20250514
chat:
strategy: cheapest
primary:
provider: openrouter
model: qwen/qwen-2.5-72b-instruct
pool: openrouter
research:
strategy: research_optimized
primary:
provider: anthropic
model: claude-opus-4-20250514
fallback:
- provider: openai
model: o3Architecture
Sequence: Failover Chain
Task Classification
Routes selected by:
- Explicit agent config:
spec.model.routing: coding - Skill context: skills tagged
routing: research - Runtime inference: keyword/heuristic classifier (optional)
- Default route:
defaultStrategy
Agent Override
# workspace/agents/architect.yaml
spec:
model:
routing: coding # Uses routes.coding
override:
provider: anthropic # Force provider (skip routing)
model: claude-opus-4-20250514Fallback Rules
| Condition | Action |
|---|---|
| Rate limit (429) | Next in fallback chain |
| Auth error (401/403) | Rotate credential, retry once, then fallback |
| Timeout | Next in fallback chain |
| All exhausted | Return error with audit trail |
Metrics & Observability
Router emits events:
PROVIDER_SELECTED— which provider chosen and whyPROVIDER_FAILOVER— fallback triggeredPROVIDER_EXHAUSTED— all options failed
Logged to workspace/audit/routing.jsonl.
CLI
anvio routing show
anvio routing test coding --input "Implement auth middleware"
anvio routing stats --last 24hExtension Guide
Status (2026-10-25): items below that mention a plugin or plugin manifest describe design intent. Anvio has no plugin system (ROADMAP T-058); extending this subsystem today means changing the package code.
- Add custom strategies via plugin
- Implement
TaskClassifierfor domain-specific routing - Integrate cost tracking from provider responses
Relationship to Existing Code
Extends packages/models and docs/09-model-router.md. Current ModelProvider port remains; router sits above it.
Package Boundaries
- Schema:
packages/core/src/schemas/routing.schema.ts - Router:
packages/models/src/model-router.ts - Classifier:
packages/models/src/task-classifier.ts
Last updated on
Workspace Architecture
Everything in Anvio lives in a **portable workspace** — backup, git-version, move, and run on another machine without database migration.
Skills Catalog & Marketplace Architecture
Bundled skills ship with Anvio; optional skills install without runtime code changes; marketplace architecture supports future community, team, and private catalogs.