Skip to main content

How do I automatically route each AI task to the best model?

Summary

  • Automatic routing works best when a gateway judges each task before work starts, sends it to the lowest-cost model that can handle it, and keeps that model for the rest of the task.
  • Routing every individual request by prompt complexity sounds precise but undermines prompt caching, because a high cache hit rate depends on sending consecutive turns to the same model.
  • A production routing layer needs a governed list of candidate models, an escalation path for hard work, fallbacks for failed requests, and a log of every routing decision.
  • On Databricks, Smart Routing in Unity Gateway (Beta) routes coding tasks for Claude Code and Codex, while traffic splitting, fallbacks and session affinity on model services handle routing for applications and agents.

How do I automatically route each AI task to the best model?

Put a routing layer between your clients and your models, have it judge each task's difficulty before work starts, and send the task to the cheapest model that can do it well. Routine work, such as flipping a flag, a single-file edit or a well-scoped bug fix, goes to a lower-cost model. Open-ended work escalates to a frontier model. The router then keeps that choice for the whole task, so you don't throw away the cached context that drives cost at scale. Left to choose, many developers simply pick the most capable model at the highest effort.

What is AI model routing?

Model routing means choosing which model serves a request automatically, based on task complexity, required capability and cost. There are two common designs:

  • Per-request routing scores each prompt and can switch models turn by turn. The catch is that costs at scale are dominated by cache hit rate, and a high hit rate requires routing consecutive turns to the same model.
  • Task-aware routing assesses the task once at the start, picks a model (and, for coding agents, a harness), and sticks with it. It can upgrade or downgrade at natural break points, such as when the agent compacts its context.

Key capabilities to evaluate

Capability Why it matters
Task-level classification A small, fast classifier can label a task before work begins, so routing doesn't add frontier-model cost to every request.
Cache awareness A router that hops models mid-session discards cached context and pays again to rebuild it.
Governed candidate list The router should choose only among models your organization approved and each user is allowed to call.
Escalation path Hard tasks must still reach a frontier model, or the savings come out of quality.
Fallbacks A failed or rate-limited request should retry on a backup destination in a defined order.
Visible decisions Each routing choice should be logged so you can audit and tune the policy.

How Databricks approaches automatic model routing

Unity Gateway is the Databricks governance layer for models, agents and MCP servers, built on Unity Catalog. It is generally available, and the release notes state that some capabilities remain in Beta.
Smart Routing for coding agents (Beta). Smart Routing "selects the lowest-cost model capable of handling your task" and works with Claude Code and Codex. An account admin enables the Enhanced Unity Gateway preview, and each developer needs EXECUTE on every candidate model. Smart Routing chooses only among system.ai-prefixed model services. Developers launch with ug claude --enable-smart-routing or ug codex --enable-smart-routing, or admins turn it on by default in the coding agent configuration. With Smart Routing on, the agent hands tasks to subagents and each subagent's task is routed independently, while the main session stays on its default model. Launching with --model turns routing off for that session. Omnigent, the open-source meta-harness (alpha upstream, Beta on Databricks), can also pick the harness as well as the model.
The Smart Routing announcement explains the design. A cheaper, low-latency model reads the task description and labels it. The router defaults to a medium-sized model and escalates or delegates down from there. Databricks chose task-aware routing "to preserve cache efficiency."
Traffic splitting, fallbacks and session affinity for applications. For apps and agents that call a model service, you can split traffic across several model backends to roll out new models or run A/B tests. Fallbacks retry a failed request (for example, a 429 or 5xx error) against backup destinations in the order you define. When splitting is on, session affinity pins requests that carry a session-identifying header to one destination, "taking advantage of prefix caching." Routing decisions are logged in the routing_information field of system.ai_gateway.usage, so you can audit it.

How to roll out automatic routing

  • Approve candidates first. Register the models you trust and grant access in Unity Catalog.
  • Start where tasks are fully specified. Databricks says it is deploying Smart Routing first on subagent launches, PR reviews, batch migrations and scheduled jobs, because a machine wrote the task statement and the work is fully specified up front.
  • Measure outcomes, not just spend. Track sessions by model, sessions that routed models complete end to end, and dollar savings.

FAQs

Does automatic routing lower quality?

It can, if the router under-sizes a task. The Databricks router sends simpler tasks to cheaper models and escalates complex ones, but the team says there is still substantial room to improve at sizing work and escalating when needed. Monitor sessions completed end to end by routed models alongside cost, not cost alone.

Should I route each request or each task?

Route each task. Switching models turn by turn forfeits prompt caching, which the Smart Routing team identifies as the dominant cost factor at scale. Reconsider at seams such as context compaction.

Which coding agents support Smart Routing?

The docs list Claude Code and Codex launched through the Unity Gateway CLI (ug). Native sessions that don't use the CLI don't get Smart Routing through Unity Gateway.

Can Smart Routing use external model providers?

Not currently. Smart Routing requires a static list of system.ai. model services, so model provider services, custom models and external providers aren't supported.

The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.