guide
guide8 min read

Text-to-SQL vs a Semantic Layer for AI Agents: Which Is More Reliable, and When Do You Need Both?

Direct SQL generation is flexible, governed metrics are reliable, and production agents need a route to each

AI agents should answer questions about governed business metrics through a semantic layer, and keep direct text-to-SQL for exploration where a person can read the query. The evidence for why is in the Spider 2.0 paper, which reports that an o1-preview-based code agent solved only 21.3% of enterprise-style text-to-SQL tasks, compared with 91.2% on the older Spider 1.0 benchmark and 73.0% on BIRD. Real warehouses are harder than benchmark schemas. The two approaches are not rivals: the best pattern routes the agent's SQL generation through the semantic layer.

Key Takeaways

  • •Direct text-to-SQL needs no modeling and handles ad hoc questions, but it is non-deterministic and can return a plausible number that silently disagrees with the official metric.
  • •A semantic layer such as dbt MetricFlow defines each metric once in YAML and generates the joins itself, so every agent and every dashboard computes revenue the same way.
  • •A semantic layer only answers what someone modeled, so unmodeled questions need a governed fallback path rather than a silent failure.
  • •The Spider 2.0 paper shows that even strong code agents solve only 21.3% of enterprise-style text-to-SQL tasks, so an agent should not be trusted with unconstrained SQL on a production schema.
  • •The reliable architecture is both: the agent asks the semantic layer first, falls back to read-only SQL with its assumptions stated, and logs which path it took.

Why this question matters now

Teams are wiring agents into warehouses through MCP, and the first question is what the agent is allowed to produce: a free-form SQL string, or a request for a named metric. The MCP specification describes tools as typed operations a client can call, which makes both designs possible. One tool might be run_sql. Another might be query_metric. The choice between them decides how often a wrong answer reaches a human who cannot tell it is wrong.

The risk is not a syntax error. Syntax errors fail loudly and the agent retries. The dangerous case is a query that runs, returns a number, and uses the wrong join, the wrong grain, or a filter the finance team never agreed to. When an agent answers "Why did row counts drop 40% overnight?" or "How much are we spending on Snowflake this month?", the reader assumes the number means what the dashboard's number means.

We hold a position on this because we build an Insights Agent that does text-to-SQL grounded by a semantic layer. Our view is that reliability comes from constraining what the model can invent, not from picking a smarter model. The rest of this page lays out why, where each approach fails, and how to combine them.

Where direct text-to-SQL breaks

Direct text-to-SQL hands the model a schema and lets it write the query. It works well on clean, small schemas and for exploration. It degrades as the warehouse looks like a real one: hundreds of tables, overlapping columns such as created_at and order_date, soft-deleted rows, and business definitions that live in someone's head. The Spider 2.0 result above is the clearest public signal of that gap between tidy benchmarks and enterprise schemas.

The failure mode is metric drift. Ask the same question on Monday and Thursday and the model may join through a different path, because generation is non-deterministic. Atlan's comparison makes the same point and reports a dbt Labs benchmark in which raw text-to-SQL scored 84 to 90% accuracy while semantic-layer-grounded queries scored 98 to 100%. That benchmark is dbt's own test of its own product, so we read the direction as credible and the exact figures as indicative.

Governance is the second problem. A business user cannot read a generated query and spot a fan-out join. An engineer can. That is why direct text-to-SQL suits technical users who review the query, and why it is a poor default for an agent answering executives.

What a semantic layer gives an agent

A semantic layer moves the hard part, the business logic, out of the prompt and into versioned definitions. According to the dbt MetricFlow documentation, metrics are defined in YAML on a graph of semantic models, entities and dimensions, and MetricFlow supports five metric types: simple, ratio, derived, cumulative and conversion. The engine then builds the joins from entity relationships, which the docs say avoids fan-out and chasm joins.

For an agent this changes the task. Instead of inventing a join, it picks a metric and some dimensions, and deterministic code generates the SQL. The model can still be wrong about which metric the user meant, but it can no longer be wrong about how revenue is computed. That is a much smaller error surface, and it is one you can test and review.

The cost is coverage. A semantic layer answers only what someone modeled. If nobody defined a metric for refund rate by acquisition channel, the agent has nothing to call. Left alone, that produces either a refusal or, worse, a model that quietly drops back to improvised SQL and presents the result with the same confidence.

When you need both

Use the semantic layer for any number that appears in a board deck, a contract, an SLA or a finance close. Use direct text-to-SQL for investigation: schema questions, one-off cuts, and debugging. Atlan's write-up reaches a similar conclusion, that routing text-to-SQL through a semantic layer is increasingly the recommended architecture, and it also notes a dissenting view that well-modeled schemas may need little separate semantic layer. We think the dissent is right for small, clean warehouses and wrong for the messy ones most teams run.

The deciding factor is who verifies the answer. If the person asking can read SQL and will check it, free-form generation is fine and faster. If the person cannot, the agent needs guardrails that do the verifying for them. Most self-serve analytics falls in the second group, which is why governed metrics should be the default path.

The deciding factor is also stakes. A wrong number in a scratch analysis costs a minute. A wrong number in a revenue report costs trust. Match the path to the cost of being wrong, not to what is easiest to build first.

A five-step way to wire up both

  • •Step 1: Inventory the metrics people actually ask for and define the top ones in your semantic layer first, starting with revenue, active users and freshness scores.
  • •Step 2: Expose two tools to the agent, a metric query tool and a read-only SQL tool, and make the metric tool the one the agent must try first.
  • •Step 3: Give the SQL tool a read-only role, row limits and a timeout so a bad query cannot write data or run for an hour.
  • •Step 4: Require the agent to state its path and assumptions in every answer, for example "computed from the governed revenue metric" versus "ad hoc SQL, not a certified metric".
  • •Step 5: Log every fallback to SQL, then promote the most frequent ones into the semantic layer so coverage grows from real demand.

Step 5 is the one teams skip. The fallback log is a free backlog of the questions your semantic layer is missing. Reviewing it weekly turns an agent from a risk into a source of modeling work.

In practice we chain this with the Catalog Agent for discovery, so the agent can find which table or metric matches a question before it generates anything. Context about owners, definitions and lineage is what lets the model pick the right target instead of guessing from column names.

What neither approach fixes

A semantic layer does not protect you from stale data, broken pipelines or schema changes. If a column is renamed upstream and the model definition is not updated, the governed metric is confidently wrong. The agent needs freshness and quality signals beside the metric, so it can say "this number is correct per the definition, but the source table is 14 hours late".

Neither approach removes the need for access control. An agent that can run SQL can read PII columns unless the role forbids it. A metric tool narrows that exposure, but the SQL fallback must be locked down with the same least-privilege thinking you would apply to a human analyst, and its calls should land in an audit trail.

Finally, neither approach resolves ambiguity in the question. "Revenue" can mean gross, net, booked or recognized. A good agent asks, or states which definition it chose. That behavior comes from the prompt and the tool design, not from the choice of SQL generation method.

Frequently Asked Questions

Is text-to-SQL or a semantic layer more reliable for AI agents?

For governed metrics, a semantic layer is more reliable because the logic is defined once and the SQL is generated deterministically. Text-to-SQL is more flexible but non-deterministic. Reported benchmarks, including the dbt Labs figures summarized by Atlan, point the same direction, though vendor benchmarks should be reproduced on your own schema before you rely on them.

Do I need a semantic layer if my warehouse is well modeled?

Possibly not for a small, clean schema with few metrics. Once several teams define the same metric differently, or the agent serves non-technical users, a semantic layer pays for itself. The test is simple: run the same twenty questions five times each and compare the answers.

Can an agent use dbt MetricFlow through MCP?

Can an agent use dbt MetricFlow through MCP?

Yes in principle. MetricFlow exposes metric queries through its commands and the dbt Semantic Layer, and an MCP server can wrap that as a typed query_metric tool. The agent then requests a metric and dimensions rather than writing joins. Check your dbt version first: the docs list MetricFlow as compatible with dbt 1.6 and higher.