Semantic-Layer Query Guardrail
Route natural-language data questions through a curated semantic layer so the model selects and parameterises vetted metrics and dimensions instead of free-authoring raw SQL against production data.
Problem
Natural language is ambiguous and business terminology is domain-specific, so the model does not know what a particular organisation means by revenue or active user, and a text-to-SQL system that lets the model author raw SQL will silently pick the wrong column, the wrong join, or the wrong filter and return a confidently incorrect number. Letting the model write arbitrary SQL against production also widens the action surface to every table the credential can reach. The system needs the model's language understanding to map the question onto a metric, without granting it the authority to define what that metric is or to author the query that computes it.
Solution
Model the metrics, dimensions, and business definitions once in a semantic layer and expose querying it as the agent's only data tool. The agent's job shrinks from authoring SQL to selecting: it maps the natural-language question onto one or more pre-defined metrics, picks the dimensions to group by, and fills in time grain and filter parameters, emitting a structured metric request rather than a SQL string. The semantic layer validates that request against its schema and deterministically compiles it to SQL — resolving the canonical column, join path, and definition behind each metric — then executes it and returns the result. Because the definitions live in the layer, every consumer computes revenue the same way; because the agent cannot author raw SQL, it cannot reach a table outside the modelled catalogue or invent a definition. A question whose metric is not yet modelled is refused or escalated to extend the layer rather than answered by improvised SQL.
When to use
- An agent answers natural-language questions over a production warehouse where business terms such as revenue or active user have organisation-specific definitions the model cannot infer.
- A semantic or metric layer (dbt Semantic Layer, Cube, LookML) already encodes vetted metric and dimension definitions, or one is worth building.
- Wrong answers from a mis-authored query are costly and consistency of definitions across consumers matters more than answering every possible ad-hoc question.
Open the full interactive page →
Diagram, neighbourhood map, code examples, related patterns and full provenance.