51 min read

Contracted Compute Is Not Energized Compute: Preventing AI SQL Hallucinations in Production

A design and acceptance framework for preventing AI SQL hallucinations: separate permission gates from correctness gates, split pre- and post-execution checks, attach evidence to every answer, and measure abstention.

Contracted Compute Is Not Energized Compute: Preventing AI SQL Hallucinations in Production

A compute commitment and a working AI SQL agent have almost nothing in common except the invoice. The commitment is a promise that capacity exists and is billable whether or not you use it. Energized compute is capacity that actually ran work you can inspect. Buyers conflate the two constantly, and the conflation gets worse when an LLM is placed in front of the warehouse: a model that emits syntactically valid SQL has demonstrated an entitlement to execute, not that any query returned a correct answer.

The reader's real question is narrower and harder than "does the agent work": how do we stop an agent from returning confident, valid-looking SQL that answers the wrong question? This article is a design and acceptance framework for that problem, not a benchmark of any vendor. It separates permission gates from correctness gates, splits what can be checked before execution from what can only be checked after, attaches evidence to every answer, and treats abstention as a designed outcome with a metric.

Why an Entitlement Is Not Evidence of Output

Start with the cloud billing model, because the distinction is documented there in plain language. Google's Compute Engine reservations documentation states that a reservation exists regardless of whether or not its resources are being used, and commitments are a separate construct from the VMs that consume them. The reservation guarantees that requested capacity is available in the specified zone; it does not tell you what ran inside it.

Warehouse contracts work the same way. A provisioned warehouse, a reserved slot count, or a prepaid credit balance tells you what you are entitled to run. It does not tell you what ran, what it cost, or whether the result was right. Snowflake's load monitoring documentation is useful here precisely because it reports on the second thing: query status by interval, including running, queued for provisioning, blocked on a transaction lock, and queued behind other queries. Those are signals of energized capacity. None of them is a signal of correctness.

The AI SQL agent inherits the same gap, and widens it. A generated query that parses has cleared a low bar. A study of in-context-learning text-to-SQL errors across four ICL-based techniques, two benchmarks and two LLM settings found that 37.3% of generated SQL queries contained errors, and summarized 29 error types in 7 categories. The authors are explicit about why the remainder is hard: "While the compiler is able to report syntax errors, semantic errors are likely to escape."

So the runtime signals that show capacity is genuinely energized rather than merely contracted are: executed statements with row counts, query latency, bytes or credits actually consumed, and — the signal most teams omit — a correctness verdict attached to the result. The warehouse ran something. That is not the same as the agent answered the question.

The Failure Mode: Valid SQL That Answers the Wrong Question

The failures that reach users are rarely syntax failures. They are queries that execute cleanly and return a defensible-looking number for the wrong question. The error taxonomy from the text-to-SQL study maps well onto the families a data team will actually encounter.

Grain. The query aggregates at the wrong level, or counts the wrong object. The study's "Wrong COUNT Object" (E6) is the counting case: asked how many molecules have a triple bond type, the generated query uses COUNT(*) instead of counting the unique molecule IDs from the bond table. Its separate redundant-entry error is the dropped DISTINCT, as in its top-3 molecules example. Counting joined-table rows rather than distinct entities is a separate grain error in the same family.

Join path. Wrong join keys, or a join that cannot work because a foreign key was pruned from the schema context. The study documents an example where cards.id and sets.id are wrongly selected in the ON condition, and separately reports a bug in one system that pruned foreign key columns during prompt construction, leaving the model without the information to join tables properly.

Date boundary. Inclusive versus exclusive endpoints, timezone handling, and partial-period comparisons. These are the errors most likely to survive review because the result is plausible in magnitude.

Metric definition. The projection does not answer the question's requested entity. The study's example: asked "whose post has the highest popularity," the generated query selects MAX(ViewCount) — the highest popularity — instead of the person's DisplayName.

Value specification. A filter that can never be satisfied. The study's example searches for 'legal' in a legalities.status column whose values are 'Legal', 'Banned', or 'Restricted', so the WHERE clause never matches. This returns an empty set, which reads to a user as "no data" rather than "wrong query."

The following snippet is an illustrative inference constructed for this article, not an observed production incident. It shows the metric-definition failure in miniature:

-- Question: "Which customer placed the largest single order last quarter?"
  -- Returns the largest order amount, not the customer.
  SELECT MAX(o.order_total) AS answer
  FROM orders o
  WHERE o.placed_at >= DATE '2026-07-01'
    AND o.placed_at <  DATE '2026-10-01';
  

The query is valid, authorized, fast, and wrong. The correct projection needs the customer identifier alongside the maximum, which requires a different shape entirely.

The study makes two further points that matter for control design. Some semantic errors have detectable symptoms — an empty result set, or a single row of NULL values — while others have none. And its "Not an error" category is a reminder that error is defined against the question: the study regards a query that omits DISTINCT and returns duplicate entries as correct unless the question explicitly specifies the number of entries. Execution success is not a correctness test.

Two Gates, Not One: Permission Controls and Correctness Controls

Authorization and correctness are independent gates, and passing one says nothing about the other. Teams that build an agent often collapse them into a single "is this query safe" check, which is where the design starts to fail.

Permission controls answer: may this principal see these rows and columns? Snowflake's row access policies are schema-level objects that determine whether a given row in a table or view can be viewed. Dynamic data masking is a column-level feature that masks plain-text data in table and view columns at query time, with users seeing masked data based on the execution-context conditions defined in the policy. Both are evaluated at query time, against the role and context of the session that issues the query.

Correctness controls answer a different question: does this query express the question? A query can be perfectly authorized and semantically wrong. A query can be semantically right and still return a different result set for the end user than it did in testing, because a row access policy filtered rows the developer could see.

The consequence for an AI agent is concrete. If the agent validates its SQL against a privileged development role, it will pass correctness checks that fail for the actual user. The fix is to run correctness evaluation under the same role and policy context as the requesting user, and to never grant the agent a bypass role "just to test." A bypass role does not test the system users will touch; it tests a system that does not exist in production.

One documented limitation belongs in the design record. Snowflake's row access policy documentation states that row access policies do not currently prevent rows from being inserted, or prevent visible rows from being updated or deleted. That is a visibility control, not a write control. It matters only if the agent is ever given write scope, which it should not be — the controls described in this article are built around read paths.

What You Can Check Before Execution — and What You Cannot

The useful split is not "static analysis versus runtime" but "what the engine can decide without touching data" versus "what requires the data and the question's intent." Getting this wrong produces either false confidence or expensive pre-flight checks that mutate state.

Pre-execution, decidable: syntax and parse errors, schema and column existence, type compatibility, function availability in the target dialect, read-only enforcement, and cost or scan estimation. Function availability deserves emphasis because it is a documented failure mode: the study reports function hallucination as a category, with YEAR(x) valid in MySQL but unsupported in SQLite, where STRFTIME('%Y', x) is required. The study also notes that more than 40% of syntax errors in one benchmark were function hallucinations. Validate generated SQL against the target engine's function set, not against SQL in general.

Cost estimation has engine-specific semantics that are easy to get wrong. A BigQuery dry run returns total_bytes_processed without running the query, which supports a pre-execution cost gate. PostgreSQL is different: EXPLAIN without ANALYZE plans the statement without executing it, but the ANALYZE option causes the statement to be actually executed, and the documentation is explicit that other side effects of the statement will happen as usual. EXPLAIN ANALYZE is therefore not a safe pre-flight check for arbitrary generated SQL.

Post-execution only: row counts, empty-result detection, NULL-only results, result-shape checks (does the projection match the entity the question requested?), and any semantic comparison against a reference answer. Grain, join-path, and metric-definition errors generally cannot be decided before execution because they depend on the data and on what the question meant — not on the syntax.

Because post-execution checking requires actually running the query, containment controls make that safe. PostgreSQL's read-only transaction mode disallows INSERT, UPDATE, DELETE, MERGE, and COPY FROM to non-temporary tables, along with CREATE, ALTER, DROP, COMMENT, GRANT, REVOKE, and TRUNCATE — though the documentation describes this as a high-level notion of read-only that does not prevent all writes to disk. Statement timeouts are set through statement_timeout in PostgreSQL and through STATEMENT_TIMEOUT_IN_SECONDS at the warehouse or session level in Snowflake. A timeout is a safety control, not a correctness control: it stops an expensive query, not a wrong one.

Evidence Returned With the Answer

An answer without evidence is an assertion. For an AI SQL agent, evidence means the artifacts a reviewer would need to challenge the answer without re-running the agent.

A workable evidence bundle contains: the executed SQL text; the engine and dialect; the role and policy context it ran under; the row count and whether the result was empty; the scan or cost actually incurred; the schema objects and columns referenced; and the question-to-clause mapping the agent used — which column answers "whose," which predicate implements "last quarter," which GROUP BY establishes the grain.

The practice has a documented analogue in general hallucination mitigation. Anthropic's guidance on reducing hallucinations recommends allowing the model to say "I don't know," grounding responses in direct quotes, and verifying claims with citations, and it cautions that these techniques significantly reduce hallucinations but do not eliminate them. For SQL, the analogue of a citation is the clause-to-intent mapping plus the executed result. The mapping is what lets a reviewer see that MAX(ViewCount) was chosen where DisplayName was asked for.

The cost is real and should be stated as an estimate rather than a measurement. A second model pass to produce the clause mapping, plus a verification pass, adds tokens and round-trips that scale with schema size and result width. This article does not measure that overhead on any system; the honest position is that it must be budgeted, not that it is negligible. Teams should instrument their own latency and token consumption before committing to a per-query evidence bundle on high-volume paths, and may choose to attach the full bundle only to answers above a materiality threshold.

Store the evidence with the answer for audit. Surface at minimum the SQL, the row count, and the policy context in the user interface, so a reviewer can challenge the answer in place.

When to Abstain — and How to Measure It

Abstention is the control most teams skip, because it looks like a failure metric. It is not. An error is a failed execution. An abstention is a deliberate "I cannot answer this with evidence," and it is the correct output in several identifiable conditions:

  • The question is ambiguous between multiple valid interpretations. The ROSE paper, summarizing prior work, reports that more than half of failures arise from ambiguity per Ambrosia (Saparina and Lapata, 2024); treat that as their report of prior findings, not a universal rate for your workload.

  • The schema lacks a column that could answer the question.

  • The generated query fails a pre-execution check and cannot be repaired without guessing.

  • The result is empty or NULL-only in a way that suggests an unsatisfiable filter rather than genuine absence of data.

  • The correctness check cannot produce evidence.

Measurement is what turns abstention from a behavior into a control. Track abstention rate alongside answer accuracy on a labelled set. A falling abstention rate with flat accuracy is a warning sign of overconfidence, not progress — the agent is answering more and getting no better at being right.

The framing here aligns with NIST AI 600-1, which defines confabulation as GAI systems generating and confidently presenting erroneous or false content, and which emphasizes pre-deployment testing and measurement as voluntary guidance rather than certification. NIST's suggested actions include defining acceptable use policies with criteria for the kinds of queries GAI applications should refuse to respond to. That is a recommendation an organization can adopt; it is not a regulatory requirement, and this article does not present it as one.

A Minimal Production Acceptance Checklist

The following checklist is the author's synthesis of the controls above. It is a starting point to adapt to your environment, not a compliance standard. Each item is pass/fail, with a stated test.

#

Gate

Pass criterion

How to test

1

Read-only access

The agent's role cannot write

Attempt an INSERT and a DDL statement through the agent's connection; confirm both are rejected

2

Timeouts

Statement and queue timeouts are set at warehouse and session level

Submit a deliberately expensive query; confirm it terminates at the configured limit

3

Policy-context correctness

Correctness checks run under the requesting user's role

Run the same question as a masked and an unmasked role; compare results and confirm the difference is expected

4

Evidence bundle

Every answer returns SQL, row count, policy context, and clause mapping

Sample real questions; confirm each answer carries the full bundle

5

Abstention

The agent abstains on ambiguous and unanswerable questions

Run a curated set; record the abstention rate and review each abstention for correctness

6

Labelled evaluation set

A set exists covering grain, join path, date boundary, and metric definition

Report accuracy on that set, not execution success rate

7

Dialect validation

Function and dialect checks run against the target engine

Include at least one function valid elsewhere but not in the target, such as YEAR(x) on SQLite

Item 6 is the one that changes how the team talks about the system. Execution success rate measures whether the warehouse ran something. Accuracy on a labelled set measures whether the agent answered the question. Only the second number belongs in a status report to a buyer or an executive.

FAQ

Does a compute commitment guarantee my AI queries will run fast? No. A commitment or reservation guarantees entitlement to capacity; latency depends on energized capacity at the moment of the query, query shape, and contention. Google's documentation is explicit that a reservation exists regardless of whether its resources are being used, which is a statement about billing, not performance.

Can't I just use EXPLAIN to validate generated SQL? EXPLAIN without ANALYZE plans without executing, so it is safe for checking plan shape and estimated cost. EXPLAIN ANALYZE actually executes the statement and its side effects occur. Neither decides semantic correctness, because neither knows what the question meant.

Isn't row-level security enough to make AI SQL safe? No. Row access policies and masking govern visibility, not whether the query answers the question. A query can be fully authorized and semantically wrong, and a policy can change the result set the agent sees relative to the developer who tested it.

How do I know the agent is hallucinating versus the data being empty? You often cannot tell from the result alone. Empty and NULL-only results are symptoms to investigate, not proof of either condition. Require the evidence bundle and abstain when the evidence cannot distinguish the two.

What does evidence cost? Real latency and token overhead from the additional model passes and round-trips. This article treats that cost as an estimate to be budgeted and measured in your own environment, not as a measured figure.

Should the agent ever write? Default to read-only. The controls described here — read-only transactions, row access policies, statement timeouts — are built around read paths, and Snowflake's documentation notes that row access policies do not prevent inserts or stop visible rows from being updated or deleted.

Closing: Energized Means Verified

Contracted compute is a promise of capacity. Energized compute is capacity that ran verified work. For an AI SQL agent, the equivalent of energized compute is an answer that carries its SQL, its policy context, its result shape, and its evidence — and that abstains when it cannot.

The question for a buyer or an architect is therefore not "how much compute did we contract." It is "what fraction of our AI SQL answers can we show were correct, and what happens to the ones we cannot." A system that answers everything and proves nothing scores zero on the second question, regardless of how much capacity sits behind it.

The next step is concrete: build the labelled evaluation set with grain, join-path, date-boundary, and metric-definition cases, then run the acceptance checklist against it. That set becomes the artifact you use to compare models, to argue with vendors, and to tell the difference between a warehouse that ran something and an agent that answered the question.

Previous posts

1. https://singdata.com/trending/natural-language-analytics-tools-permission-controls/

2. https://singdata.com/trending/vector-index-row-level-security-layout

3.https://singdata.com/trending/text-to-sql-hallucination/