Imagine your SQL assistant is a rocket, blasting through complex queries. Then one day you realize you’re using rocket fuel to fetch a grocery list.

It’s exciting, until the fuel bill arrives. Suddenly it becomes clear that simple errands don’t need a rocket. The same thing happens when every SQL request, from a basic lookup to a multi-schema analysis, is routed to the same powerhouse AI model.

The process of getting an AI SQL assistant is usually the same. At first, productivity goes up: queries are made faster, boilerplate code goes away, and developers spend less time writing routine SQL queries. As more teams use it, the number of queries rises. When the infrastructure bill comes, the economy changes.

The issue is with the building. It costs a lot to run Frontier AI models that can think about execution plans, schemas, and complex query logic. That price makes sense for hard tasks, since it costs about $0.03 per query. When used for simple SELECT statements and CRUD operations, it becomes waste at scale.

But the answer isn’t to lower the model. It’s sending queries to the right place. Smart query routing sorts each request by how hard it is and sends it to the right model tier. This method can cut inference costs by 40–70% in SQL workloads without lowering the quality of the output.

This article explains how that architecture works: defining SQL complexity tiers, building classification and routing pipelines, and measuring the real cost-quality trade-offs once the system is running. These patterns reflect lessons learned while developing schema-aware AI capabilities in dbForge AI Assistant.

Why one model doesn’t fit all SQL tasks

Not all SQL queries are the same in terms of complexity. A query that fetches a user by primary key and one that rebuilds session funnels across several schemas with window functions are both SQL, but the reasoning needed to generate them is very different.

If a system treats them the same, the result is predictable: wasted compute. In most enterprise workloads, roughly most queries are routine. Simple lookups, single-table reads, basic inserts, syntax fixes. Nothing complicated. Sending all of those to a frontier model is like using a freight elevator to carry a notebook.

One way to think about the problem is by splitting queries into complexity tiers:

Tier  Description  Examples  Model Needed 
Tier 1 — Routine  Simple, well-defined tasks  Simple SELECTs, lookups, basic CRUD, syntax fixes  Fast, low-cost model 
Tier 2 — Moderate  Multi-step reasoning required  Multi-table JOINs, subqueries, aggregations, optimization hints  Mid-tier model 
Tier 3 — Complex  Deep schema awareness and reasoning  Cross-database queries, window functions, execution plan tuning, schema-aware refactoring  Frontier model 

The cost gap between tiers is large. A Tier 1 query might cost around $0.001 on a lightweight model. The same query sent to a frontier model costs closer to $0.03. At 10,000 queries per day, that’s $10 vs $300 in daily spend. A 30× difference, just from routing decisions.

Schema awareness also matters here. Tier 3 queries don’t just need more compute. They need context: table relationships, foreign keys, indexes, database-specific syntax. That context has to be injected during inference.

Running a simple Tier 1 query through that same heavy path wastes tokens, adds latency, and doesn’t improve the result.

A practical architecture for model selection

A routing system usually has four stages: classify, route, execute, and validate. Each stage does a different job, and each can fail in different ways. It helps to think about them separately before putting the full pipeline together.

Classification is the most important step. The classifier receives either the raw SQL query or the natural language prompt that will generate one and assigns it to a complexity tier. There are three common ways to build this classifier.

Rule-based classification relies on regex patterns and AST (abstract syntax tree) parsing to detect structural signals: things like table count, nesting depth, window functions, subqueries, or aggregation operators. This approach is fast and predictable, with almost no overhead. It works well for obvious cases: simple SELECT statements and basic DML can usually be identified without involving a model at all. 

Lightweight classifier models use a small language model trained to estimate SQL complexity. This adds an extra step, but it’s one of the highest ROI decisions in the entire pipeline. A classifier call might cost about $0.0001, which easily justifies avoiding a $0.03 frontier model call.  

In many setups, these lightweight models can also run locally, effectively removing cost for simple user queries altogether. They can also classify natural language prompts before SQL is generated, which is useful in assistant workflows where the query does not exist yet. 

Hybrid classification combines both approaches. Rule-based logic handles clear cases at zero cost, while the classifier handles the ambiguous middle: queries that look moderate but may actually require schema-aware reasoning to generate correctly. 

Routing happens after classification. But the tier alone is not the only factor. A few other things influence where a query should go. These include:  

  1. Schema context requirements. Some queries need the model to understand foreign key relationships, indexes, or other structural details. These queries carry more context and usually need to be routed to a higher-capability model. 
  2. Latency tolerance. User-facing features like autocomplete or inline suggestions have strict latency budgets. Background tasks usually don’t. In those cases, a slower but more capable model may be acceptable. 
  3. Confidence thresholds. Sometimes the classifier isn’t sure about the tier. In those cases, routing up is usually the safer option. A wrong downgrade can produce a bad query and trigger retries, which often costs more than using the stronger model in the first place. 

The validation layer runs after the code has been run. The job of this is to catch routing mistakes before they get to the user. After the execution, checks are made to make sure that the syntax is correct, that the results are reasonable (did the query return the right row shapes?), and that the schema is consistent. When a result fails validation, the system moves up a level and runs the query again.

At Devart, the most important thing for getting the dbForge AI Assistant’s routing accuracy right was building schema-aware context into the classification decision. Without schema context, queries that used unclear table names or relied on implicit relationships were always misclassified and sent to cheaper models that couldn’t handle them. The solution was to give the classifier not only the query structure but also some schema metadata.

Measuring what matters: cost-quality trade-offs in practice

The business case for routing only holds if quality holds alongside it. Cost reduction that causes degraded output, increased retries, or developer distrust is not a saving, it’s a transfer of cost from the infrastructure bill to engineering time. Three metrics determine whether a routing system is actually working.

Cost per query by tier establishes the baseline. Track actual spend at each tier separately, not as a blended average. Blending obscures whether the routing is working, a system routing 50% of queries to the wrong tier will still show a lower average cost, while silently producing worse results. 

The quality score checks for correctness, completeness, and following SQL best practices. The escalation rate is the most direct signal. It tells you how often a Tier 1 or Tier 2 model makes output that doesn’t pass validation and needs to be sent to a different location. A system that is well-tuned should keep escalation below 5%. The classifier needs to be retrained above that level. It could be misreading structural signals or it might not have the schema context it needs to tell the difference between moderate and complex. 

Latency impact looks at how long it takes for a response to move from one tier to the next, including any extra time needed for classification. Users should only notice a 50 to 100 millisecond delay in interactions that go through the routing layer. If classification itself becomes an issue, the hybrid approach (rules for clear cases, classifier only for unclear ones) fixes it without losing accuracy. 

In real life, a well-tuned routing system can lower inference costs by 40–60%, keep escalation below 5%, and keep output quality high for complex queries. To save 70% or more, you usually have to do Tier 1 tasks on your own with smaller models. That could work, but it also makes things more complicated, which not every team wants to deal with.

The “escalation tax” is another thing that needs to be looked at. If routing is too hard on cheaper models, the system might have to do more work overall: classifier call, initial model call, failed validation, reroute, and a second model call. In some cases, that costs more than sending the question to the frontier model in the first place.

Looking only at cost per call misses this effect. Escalation rate has to be tracked alongside it.

Strategic takeaways for engineering teams

Smart routing is not just a nice-to-have for mature AI SQL deployments; it’s a must-have for long-term ones. Teams that skip it trade a budget problem that can’t be solved for an architecture problem that can be. The patterns are there; the only thing left is to decide which ones to follow first.

Start with the classifier, not the models. The routing layer decides if everything else works. A well-tuned hybrid classifier will give you most of the cost savings without making things too complicated.

Use the schema context of the feed to help you make classification decisions. For SQL workloads that involve relationships between multiple tables or reasoning that is specific to a schema, query structure alone is not enough. Partial schema metadata at classification time greatly boosts tier accuracy.

Use the escalation rate as the main quality signal. It finds misclassification faster than any other metric and shows exactly where the classifier needs to get better.

Before the classifier, plan the validation layer. Knowing what failure looks like and what causes an escalation makes the routing logic cleaner and the system better able to handle edge cases.

The value of the routing layer goes up, not down, as open-source models get better and the cost of local inference goes down. Cheaper Tier 1 models make the cost difference between tiers bigger, which makes correct classification more valuable. The routing architecture that is built today will be useful for a long time, not just as a quick fix.