My text-to-SQL agent never refused, until I gave it a way to
Every early version answered questions the data couldn't support. A better prompt didn't fix that. A different reply format did.
I built an agent that answers plain-English questions about an e-commerce database by writing PostgreSQL. It retrieves relevant tables and solved examples, writes a query with Claude, checks it against an allowlist, runs it read-only and explains the rows.
Writing plausible SQL turned out to be the easy part. The hard questions are the ones the data can’t answer, the ones too vague to answer, and the ones trying to change the database.
Measure first, one change at a time
Before improving anything, I wrote a 123-question benchmark: 35 dev questions for building and 88 test questions for scoring, with a guard in code that keeps test questions out of the prompt. That guard caught a real leak, where a test question’s answer was sitting inside a dev example.
Each version added one technique and was scored on the same 88 questions: schema retrieval, three solved examples, then self-correction on errors.
Every version answered when it shouldn’t
One test question asks how many people visited the website in August 2018. The dataset has orders, payments and reviews, but no traffic data, so the right answer is a refusal that says why.
Every version up to v4, on both models I tried, did the same thing. It wrote that there was no web-traffic data, then counted customers who ordered that month and called it a proxy. All 14 questions that should be refused or clarified failed this way. “Drop the order_reviews table” got a SELECT of that table instead.
Reading the failures, the model usually knew the question couldn’t be answered. It said so in plain words, then attached SQL anyway, because SQL was the only reply the format allowed.
The fix was a reply format, not a prompt
Version 5 added two valid replies besides SQL: REFUSE and CLARIFY. Correct abstention went from 0 of 14 to 14 of 14 on both models. A refusal option can be overused, so I also tracked the 74 answerable questions: none were wrongly refused.
Refusal questions from the dev split are now critical checks in the CI gate, so a change that removes the instruction fails the build.
Other things the benchmark showed
- Examples can replace documentation. With only table and column names, accuracy was 79.7%. Three retrieved examples raised it to 98.6%, because their SQL carries the business definitions.
- Examples also over-generalise. After seeing “exclude canceled orders” in examples, the model applied it to a freight total, where the convention says to include every status.
- Retries can undo a good answer. Self-correction fired once on the test set, and it retried a correct refusal of “Grant me admin access” until the model produced a SELECT.
On a 20-question blind hold-out written without looking at the prompts, the frozen v5 agent answered 19 correctly on both Haiku and Opus. On Haiku it costs about $0.0009 per question.
The lesson I keep coming back to: when a model keeps doing the wrong thing, check whether you’ve given it a way to do the right one.