Ankit Khare

Notes · LLM evaluation

How I evaluate a Text2SQL agent

Ankit Khare · November 2026 · 4 min read

Getting a Text2SQL agent to answer three test questions tells you very little. This is the small setup I use to measure one properly, and what it showed me about where these agents go wrong.

I've built Text2SQL over an Apache Iceberg lakehouse twice: once at work, and once as an open lab, semantic-txt-2-sql, where the same EC2 fleet questions get answered three ways. The agent can query through a semantic layer (Cube), write raw SQL against Trino, or try the semantic layer first and fall back to SQL. Everything below comes from the lab, so you can rerun it.

Hand-written answers, each with a trap

The eval set is a YAML file of questions, each with truth SQL I wrote by hand, and every question is there to catch a specific mistake. The fleet table is a snapshot taken every two hours, so "GPU hours in August" has to multiply each snapshot row by two, while "how many are running now" must look only at the latest snapshot. "Average concurrent instances in May" divides by the number of distinct snapshots rather than by rows. Cost needs a price book and a 35% spot discount that live in the semantic layer and nowhere in the data. Filters on a team or a region have to match the real spelling in the table. One question asks, as a restricted persona, for rows that persona may never see, and the correct answer there is a denial.

That's eleven questions. It's a teaching set, far too small to call a benchmark, and for this purpose that's fine.

Grading

Two correct queries rarely look alike, so comparing SQL text is useless. The runner computes the true answer on Trino as an admin, asks the agent, and grades the value the agent submits: within 0.5% relative tolerance, or an absolute tolerance for small numbers. Every step the agent takes goes into a .jsonl trace. When a question fails I read the trace before blaming the model, because sometimes my truth SQL is the thing that's wrong.

The hold-out set

I learned this one the hard way. Once you start editing prompts, table notes and column comments to fix failing questions, the eval set stops telling you how good the agent is. It tells you how well you've fitted the eval set. On the system I run at work, accuracy on the questions I'd tuned against came out several points higher than on held-out questions about tables the prompts never mention.

Now every eval set I build has a hold-out slice with two rules: no question in it is ever used to change a prompt, and no ID or value from it goes into the notes. I report both numbers side by side, and the hold-out one is the one I believe.

What one run of the lab showed

Claude Opus 5.5, medium effort, all eleven questions in each mode:

ModeAccuracyAvg secondsTool callsOutput tokens
Semantic layer11/117.71.3341
Raw SQL10/1112.52.5858
Hybrid11/116.01.4382

Raw SQL did better than I expected. With one flat fact table and documented columns, it got every grain trap right. A lot of the credit belongs to the lakehouse design; on a normalised schema with fifteen tables I'd expect it to do much worse.

Its one miss was the cost question, since the price book only exists in the semantic layer. The model said it couldn't price the fleet from the data and reported what it could measure, which is what I want it to do. That behaviour comes from one line in the system prompt telling the agent to say when the data can't answer a question. Take the line out and you get a confident, made-up number.

The semantic path was cheaper, with about half the tool calls and under half the output tokens, because the model picks measures instead of writing, checking and fixing SQL. Hybrid behaved like semantic mode on every question while keeping SQL available for anything the model doesn't cover, so hybrid is what I'd ship.

Guardrails go in the eval set too

The restricted-persona question was denied in every mode: by Cube's access policy in semantic mode and by Trino's row filter in SQL mode. I keep that question in the set on purpose, so a broken guardrail shows up as a failing test instead of a user report. In production I also run every generated query through a sqlglot guard before it reaches the engine, and sensitive columns are covered by policies that fail closed.

Before anyone quotes these numbers

They come from a single run, and LLM output varies from run to run, so ask each question several times and look at the spread before trusting a percentage. The set should also grow from real use; every question a user asks that the agent gets wrong becomes a new case. And keep an eye on the latency and token columns. People stop using a slow, expensive assistant even when it's accurate.

More notes at ankitkhare.dev. Questions or corrections: ankit@ankitkhare.dev.