cancel
Showing results forĀ 
Search instead forĀ 
Did you mean:Ā 
Community Articles
Dive into a collaborative space where members like YOU can exchange knowledge, tips, and best practices. Join the conversation today and unlock a wealth of collective wisdom to enhance your experience and drive success.
cancel
Showing results forĀ 
Search instead forĀ 
Did you mean:Ā 

I Stopped Sending Every Data Engineering Task to an LLM - A Cost Aware Routing Pattern on Databricks

AmitDECopilot
Contributor II

AI is becoming part of almost every data engineering workflow.

We are asking AI to generate SQL, PySpark pipelines, data-quality rules, unit tests, technical specifications and even complete project structures. The results can be impressive, but there is another side that we do not discuss enough: cost and unnecessary complexity.

Recently, I asked an AI coding tool to generate validation scripts for a large data pipeline. It produced thousands of lines of code and consumed a significant number of tokens. But when I reviewed the output carefully, only a small portion of the required columns and business rules were actually covered.

The code volume looked impressive. The actual coverage was not.

That experience made me question a common assumption:

Does every data engineering task really need the most powerful LLM?

In most cases, the answer is no.

Not Every Task Requires AI

Consider some typical data engineering activities:

  • Generate null checks for 100 columns.

  • Create default-value checks for dimension keys.

  • Compare source and target row counts.

  • Generate a standard Delta table definition.

  • Validate naming conventions.

  • Create repetitive unit-test queries.

These are predictable and rule-based tasks. A Python or SQL generator can often perform them faster, more consistently and at almost zero AI cost.

Now consider a different set of problems:

  • Diagnose why YTD values reset incorrectly.

  • Explain why a join is unexpectedly multiplying records.

  • Interpret unclear transformation rules from an STTM.

  • Recommend a design for handling late-arriving dimensions.

  • Analyze whether a pipeline meets a complex business requirement.

These tasks require reasoning and context. This is where an LLM provides real value.

The problem is that many implementations send both types of work to the same expensive model.

Cost Control Alone Is Not Enough

Databricks recently introduced AI spend controls in Unity AI Gateway. Organizations can define budgets and monitor AI expenditure across users, workspaces and use cases. It also provides request-level cost information through Unity Catalog system tables.

This is important because token usage alone does not tell us whether an AI task delivered value.

A request may consume 100,000 tokens and still generate incomplete or unusable code. Another request may consume 5,000 tokens and identify a production issue that saves several days of investigation.

The real metric should not be:

How many tokens did we use?

It should be:

What useful and validated outcome did we receive for the cost?

Databricks has also introduced Smart Routing in Unity AI Gateway, which selects models based on task complexity. According to Databricks, its internal coding workloads achieved meaningful cost reduction by routing simpler tasks to less expensive models and escalating complex tasks when necessary.

This is a strong direction. But for data engineering, I believe routing should go one step further.

We should not only select the appropriate model. We should first determine whether an LLM is required at all.

A Data Engineering-Specific Routing Pattern

I call this pattern the AI Pipeline Engineering Governor.

The governor sits between the user and the execution engine. It analyzes the requested task and chooses one of three execution paths.

Path 1: Deterministic Engine

The task is handled using Python, SQL templates or metadata-driven rules.

Examples include:

  • DDL generation

  • Standard data-quality checks

  • Null and default-value validation

  • Naming-standard validation

  • Schema comparison

  • Reconciliation query generation

  • Repetitive test generation

This path is inexpensive, predictable and easy to validate.

Path 2: Low-Cost Model

A smaller model handles tasks requiring limited interpretation.

Examples include:

  • Explaining a generated SQL statement

  • Classifying a transformation rule

  • Summarizing a pipeline failure

  • Converting a clearly defined rule into SQL

  • Generating documentation from validated metadata

Path 3: Advanced Reasoning Model

A more capable model is used only when the problem requires deeper reasoning.

Examples include:

  • Investigating reconciliation mismatches

  • Diagnosing record-count explosions

  • Understanding complex temporal logic

  • Resolving conflicting business rules

  • Recommending architectural trade-offs

  • Analyzing unfamiliar pipelines with incomplete documentation

The router should default to the deterministic or lower-cost path. It should escalate only when the task complexity, ambiguity or validation result justifies the additional expense.

Proposed Databricks Architecture

The solution can be implemented using native Databricks capabilities.

1. Databricks App

A simple Databricks App provides the user interface.

The engineer submits:

  • Task description

  • Source and target schemas

  • STTM or transformation rules

  • Existing SQL or PySpark code

  • Data-quality expectations

The interface displays the selected execution path before running the request.

2. Task Classifier

The classifier evaluates factors such as:

  • Task type

  • Number of tables and columns

  • Availability of metadata

  • Rule complexity

  • Requirement ambiguity

  • Need for business interpretation

  • Expected output type

  • Validation requirements

The first version does not need another expensive AI model for classification. A rule-based scoring mechanism is enough.

For example:

if task_type in ["null_check", "schema_compare", "ddl_generation"]:
    route = "DETERMINISTIC"

elif ambiguity_score <= 2 and transformation_count < 10:
    route = "LOW_COST_MODEL"

else:
    route = "ADVANCED_MODEL"

Over time, the router can learn from execution results and human feedback.

3. Execution Layer

The execution layer invokes the selected option:

  • Python generator

  • SQL template engine

  • Databricks Foundation Model API

  • External model governed through Unity AI Gateway

The execution decision and its reason should be stored with every request.

4. Validation Layer

Generating code is not the final step.

Every output should pass relevant validations before it is presented for approval.

The validation layer can check:

  • Syntax correctness

  • Schema alignment

  • Referenced columns

  • Transformation coverage

  • Naming standards

  • Data-quality coverage

  • Unit-test execution

  • Source-to-target reconciliation

  • Security or governance violations

If a lower-cost route fails validation, the task can be escalated automatically with the failure details included as context.

This is better than immediately sending the original request to the most expensive model.

5. MLflow Tracing and Evaluation

MLflow 3 can capture the complete execution trace, including inputs, outputs, intermediate steps and evaluation results.

For every task, I would record:

  • Initial route

  • Final route

  • Model used

  • Input and output tokens

  • Estimated cost

  • Execution time

  • Validation score

  • Test coverage

  • Number of escalation attempts

  • Human approval or rejection

  • Final production outcome

MLflow scorers can evaluate whether the generated output is complete, grounded in the supplied metadata and aligned with engineering standards.

6. Unity Catalog Governance

The generated artifacts should remain traceable to their original inputs.

Unity Catalog can help govern:

  • Source and target metadata

  • Model endpoints

  • Prompt and configuration versions

  • Execution traces

  • Generated code

  • Validation results

  • Approval status

  • Data ownership

  • Access policies

Sensitive STTM documents, schemas and production code should not become uncontrolled prompt content.

Governance must apply to both the data and the AI interaction.

7. Human Approval

The governor should never promote AI-generated code directly into production.

The final workflow should be:

Request
   ↓
Task Classification
   ↓
Deterministic Engine or AI Model
   ↓
Automated Validation
   ↓
Cost and Quality Evaluation
   ↓
Human Approval
   ↓
CI/CD Promotion

AI can accelerate engineering work, but the engineer should remain accountable for the production outcome.

Measuring Whether the Router Is Working

A cost-aware router should not be evaluated only by total savings.

The following metrics provide a more complete picture:

  • Cost per accepted artifact

  • Percentage of tasks completed without an LLM

  • Percentage completed by a low-cost model

  • Escalation rate

  • First-pass validation rate

  • Transformation-rule coverage

  • Unit-test coverage

  • Human rejection rate

  • Average delivery time

  • Production defect rate

Suppose a cheaper model reduces the cost by 60% but doubles the engineer’s review time. That may not be a real saving.

Similarly, an advanced model may be more expensive but justified when it correctly diagnoses a complex reconciliation issue.

The objective is not simply to minimize AI cost.

The objective is to maximize validated engineering output per dollar.

How This Differs from Generic Model Routing

Generic model routing asks:

Which model should handle this task?

A data engineering governor asks several questions:

  1. Can this task be completed deterministically?

  2. If AI is required, what is the least expensive capable model?

  3. Is the output aligned with the supplied metadata and business rules?

  4. Did the generated artifact pass technical validation?

  5. Is escalation justified?

  6. Has a human approved the final result?

This distinction matters because many data engineering tasks are metadata-driven and repeatable. Using an LLM for every step may introduce inconsistency where deterministic logic would be more reliable.

Start Small

The first version does not need a complex multi-agent architecture.

A useful MVP could support three tasks:

  1. Generate data-quality checks from table metadata.

  2. Translate transformation rules into SQL or PySpark.

  3. Diagnose source-to-target reconciliation mismatches.

The first task can primarily use deterministic generation. The second can use a lower-cost model with validation. The third can use an advanced reasoning model.

This is enough to demonstrate routing, tracing, validation, cost measurement and human approval end to end.

Final Thought

LLMs are powerful, but using the most capable model for every task is neither an architecture nor a governance strategy.

Some work requires reasoning. Some work requires deterministic automation. The important part is knowing the difference.

Databricks now provides many of the building blocks through Unity AI Gateway, Unity Catalog and MLflow 3. The next step is to apply them to measurable engineering workflows where cost, quality and accountability can be evaluated together.

The future of agentic data engineering is not about generating the maximum amount of code.

It is about producing the right engineering outcome, using the right execution path, at a cost we can explain.

References

#Databricks #DataEngineering #AgenticAI #GenerativeAI #MLflow #UnityCatalog #UnityAIGateway #AIGovernance #FinOps #LLMOps #PySpark #Lakehouse #DataQuality #AIAgents #CostOptimization

Amit Kumar Singh
Lead Data Engineer | AI-Assisted Data Engineering
1 REPLY 1

suryaprayaga
Contributor

Yes that's true. There are hundreds (if not 100s) of tasks that can be done without depending on the agents. Databricks indeed has DQX for governance and data quality. Instead of relying on smaller activities (especially when they are repeating in nature) it would not make sense to rely on an LLM to do it. 

suryaprayaga