When you connect an LLM to a SQL database so people can ask questions in plain English, writing valid SQL is the easy part. Wrong answers often come from the data itself, such as a column that stores cents instead of dollars, or a country column that mixes names with ISO codes. On October 22, 2026, we ran a text-to-SQL agent on six current models through OpenRouter, using a synthetic shop database and 22 questions with known answers. Across completed runs, it answered 95% correctly from the schema alone (244 of 256) and 99% with a five-line note about the data (246 of 248; 93.2% of all 264 attempts including provider errors).
You can wire the same read-only run_sql tool loop with OpenRouter’s tool calling API.
Tl;dr
- Adding a five-line note about data quirks to the prompt raised accuracy from 95% (244 of 256 completed runs) to 99% (246 of 248).
- Five of the six models never sent a failing query.
- The wrong answers came from messy data, such as Germany stored as DE, de, and Germany.
- With the notes, openai/gpt-6-luna answered all 44 runs correctly at $0.16 per 1,000 questions, the same score as anthropic/claude-sonnet-5.5 at $6.20.
- Open the database connection read-only.
- In our SQLite setup, WITH x AS (SELECT 1) DELETE FROM refunds passed a check that the query starts with SELECT or WITH, and the read-only connection still rejected it.
How does a text-to-SQL agent work?
A text-to-SQL agent runs a loop in which the model writes SQL, your code runs it read-only, and the model reads the result before querying again or answering.
Your application sits between the model and the database. At startup, it reads the CREATE TABLE statements, so the prompt matches the live tables, and adds notes on the data, today’s date, and the answer format.
When a user asks a question, the model calls a run_sql tool with a query. Your code runs it and returns at most 50 rows, plus a byte or token limit on the serialized result, so neither a broad query nor a few oversized text/JSON fields can fill the context window. If the database rejects the query, the tool returns an error, such as no such column: total, and the model fixes the query on its next step. Our Agent SDK’s callModel runs this loop until the model answers or hits a stop condition such as stepCountIs(10).
How we tested text to SQL across six models
To see where this loop breaks, we built a SQLite shop database with the kinds of problems real databases have. It has five tables holding 600 customers, 24 products, 7,000 orders from January 2025 to September 2026, 13,952 order items, and 299 refunds.
We built these traps into the data:
- Money columns store integer cents (unit_price_cents, amount_cents).
- customers.country mixes DE, de, Germany, and NULL.
- Prices paid on order_items differ from list prices after April 2026.
- One product, the Camp Chair, was never ordered.
- Relative dates such as “last month” depend on today’s date, which we fixed at 2026-10-01 in the instructions so every run had the same answers.
We wrote 22 questions and computed each answer with our own SQL, from “How many customers are based in Germany?” (60) to revenue for non-canceled Q2 2026 orders ($284,724.18). We stripped Markdown from replies and scored numbers to 0.01.
We ran the six models listed in the model table under “Which model should you use?”, from five labs, each with reasoning.effort: “low”, the same run_sql tool, and stepCountIs(10). Every question ran twice with the schema only and twice with the schema plus the notes block. During the run, 24 of the 88 requests to google/gemini-3.8-flash failed with a provider server_error, so its scores count only the runs that completed.
What did the models get wrong?
Every model answered most questions correctly from the schema alone. Joins, subqueries, medians, and NOT EXISTS queries worked, and most of the mistakes came from the data traps.
SQL errors were rare. Five of the six models sent no failing queries at all, and google/gemini-3.8-flash averaged under 0.1 failed queries per question and recovered on the next step. Gemini’s failed requests left the two setups with different totals, so we also compared the other five models alone, and they went from 209 to 218 correct out of 220.
The most serious failures were queries that ran without errors and returned a believable wrong number. With the schema alone, only 4 of 11 completed runs answered the Germany question correctly, the two runs each from deepseek/deepseek-v4.1-flash and anthropic/claude-haiku-5.5. openai/gpt-6-luna, minimax/minimax-m3.1-flash, and google/gemini-3.8-flash answered 15 because they counted only rows stored as Germany. anthropic/claude-sonnet-5.5 answered 46 because it counted DE and Germany and missed the 14 rows stored as lowercase de. A count of 15 or 46 comes back with no error, so your application has no signal that it is wrong. After we added one note explaining customers.country, every completed run answered 60.
Which model should you use?
With the notes, five of the six models scored 43 or 44 out of 44, so on this test cost and speed separated them more than accuracy did.
Measured on October 22, 2026, through OpenRouter. Gemini’s scores are out of its completed runs, because 24 of its 88 requests failed. Costs are from usage.cost in our run.
If you document your data quirks in the prompt, start with openai/gpt-6-luna. It answered every question with about one query and 4.2 seconds per question, at less than 3% of the cost of anthropic/claude-sonnet-5.5. If your database has quirks you haven’t documented yet, deepseek/deepseek-v4.1-flash was the only model to score 44 of 44 in both setups.
With 44 runs per model on one synthetic database, a one-question gap is too small to rank models, so rerun your own questions on two or three before you choose. Only the model string changes between them.
How to write notes that make a text-to-SQL agent accurate
Both setups included the schema, today’s date, and the answer format, and differed only in this notes block. These are the exact notes that took accuracy across completed runs from 95% to 99%:
Write notes for what the schema can’t show
A CREATE TABLE statement shows column names and types, but it can’t show units, allowed status values, mixed casing, or how your team calculates a figure like order total. Write one line for each of these. If a question has two reasonable readings, state the formula you want. Our question about the Solo Tent’s price change gave 5.8% averaged per order line and 5.9% weighted by quantity, so we accepted both.
Put today’s date in the instructions
The model has no clock, so add a line such as Today is 2026-10-01. to the instructions from your server’s clock on each request, and say how timestamps are stored. The model can then turn “last month” into a filter on September 2026.
Test on questions with known answers
Write 10 to 20 questions about your own database, including some on your messiest columns, and compute each answer with your own SQL. Run each one through the agent at least twice and compare the answers with yours. When an answer is wrong, read the agent’s SQL and add a note for the quirk it missed, as we did for Germany.
How do you keep a text-to-SQL agent from changing data?
Open the database connection in read-only mode, or connect as a database user with SELECT permission only, because a check on the prompt or the query text can be bypassed. Read-only access only prevents writes, not unauthorized reads—on a shared customer database, also enforce tenant and column restrictions in the database (such as views or row-level security) or in the tool itself so a SELECT-only account cannot return another tenant’s data.
A check that the query starts with SELECT gives the model a clear error when it tries a direct UPDATE. In SQLite, a query that starts with a common table expression gets past it. WITH x AS (SELECT 1) DELETE FROM refunds starts with WITH, and when we ran it against our read-only SQLite connection, the database rejected it with attempt to write a readonly database. Which statements can follow WITH varies by database, so test this on yours.
When we asked openai/gpt-6-luna to “Delete every refund with the reason damaged, then tell me how many are left”, the agent ran only a SELECT on the same read-only connection. It replied that it couldn’t delete rows and that removing them would leave 217. The connection would have rejected the write if the model had tried.
Frequently asked questions
Is text-to-SQL accurate?
In our test of six current models on 22 questions, a text-to-SQL agent answered 95% of completed runs correctly from the database schema alone and 99% with a five-line note describing units, status values, and inconsistent country codes. The models rarely wrote invalid SQL, and most wrong answers came from data quirks the schema doesn’t show.
What is NL2SQL?
NL2SQL (natural language to SQL) is another name for text-to-SQL, which turns a plain-English question into a SQL query. An agent also runs the query, reads the result, and can query again before it answers.
Can an LLM run SQL queries?
An LLM can run SQL queries through a tool that your code provides. With our Agent SDK’s callModel, you pass a run_sql tool connected to a read-only database, and the SDK runs the loop between the model and your database.
Next steps
To build this agent and run it on your own database, follow the tool calling guide. To compare models on your own questions, filter the models page to models that support tool calling.












