AI SQL Agent with Safety Guardrails
Ask a MySQL database a question in plain English and get back the SQL and the matching rows. Every generated statement has to pass a read-only check before it runs.
SELECTread-only SQL
The problem
Small teams keep their operational data in MySQL, but the people with questions about it, such as which invoices are overdue or who signed up this month, usually can't write SQL. Every question turns into a request to a developer. An LLM can write the query instead, but connecting one to a database adds two risks: it invents table and column names it has never seen, and it can produce a statement that changes or deletes data, either because it misread the question or because someone asked it to.
Architecture
A FastAPI app reads the table and column names from the connected database on each request and passes them, with the question, into a LangChain prompt. The chain calls a model on OpenRouter at temperature 0 and asks for one bare SELECT statement, or a fixed refusal sentence for anything else. The server strips markdown from the reply and checks it in code: it must start with SELECT and contain none of the write or schema keywords. Only then does it run against MySQL, and the page shows the generated SQL above the result rows.
Request pipeline
Server-side safety check
Markdown fences are stripped, then the reply must start with SELECT and contain none of INSERT, UPDATE, DELETE, DROP, ALTER or TRUNCATE. A refusal from the model fails the same check.
Engineering decisions
- Schema from the live database
- get_schema() runs SHOW TABLES and SHOW COLUMNS on every request and sends only table and column names to the model. The prompt stays small, the model sees the tables that exist right now, and the same text drives the schema tree in the sidebar, so the user sees exactly what the model saw.
- Never trust the prompt alone
- The prompt tells the model to return SELECT statements only and to answer write requests with “Agent cannot perform that operation.” Models don't always follow instructions, so the server checks every reply before execution: it must start with SELECT and must not contain INSERT, UPDATE, DELETE, DROP, ALTER or TRUNCATE. A refusal fails the same check, so refusals and unsafe output reach the user through one error path.
- Output that runs as-is
- Temperature is 0 and the prompt bans markdown and explanations, so the reply should be a bare statement. clean_sql() still strips markdown code fences before validation, because models add them anyway.
- Show the SQL, not just the answer
- The result panel prints the generated query above the rows. Someone checking a number can see exactly which filter ran, and a wrong query is obvious instead of passing as a wrong fact.
- One model string to swap
- LangChain's ChatOpenAI client points at OpenRouter's OpenAI-compatible endpoint. The default is the free DeepSeek R1 model; moving to GPT, Claude or Mistral changes one line, not the code around it.
Outcomes
Everyday questions take one step. “Show all due invoices” becomes SELECT * FROM invoices WHERE status = 'due'; and the matching rows appear under it. Requests to delete, update or drop data are refused whether or not the model follows its instructions, and because the query is printed next to the result, the person asking can check what ran instead of trusting a summary.

- Allowed statements
- SELECT only
- Guardrail layers
- Prompt rule + server check
- LLM access
- LangChain + OpenRouter
- Source
- Public on GitHub
What I’d change next
The server check handles the cases it was written for, but it matches substrings instead of reading the SQL. The panel below runs the same logic as is_query_safe() in main.py, and the last two examples show where it breaks.
Guard check
Same logic as main.py · runs in your browserStarts with SELECT · no blocked wordsruns
- Read-only at the database
- The keyword check should be the second line of defence, not the only one. Connecting as a MySQL user with SELECT privileges only means a statement that slips past the filter still can't write.
- Parse the statement
- Substring matching blocks harmless columns such as updated_at or deleted_at, and lets SELECT … INTO OUTFILE through, which can write a file on the database server. Parsing with sqlglot and accepting exactly one SELECT statement fixes both.
- Bound every query
- Append a LIMIT when the model leaves one out and set MAX_EXECUTION_TIME, so a SELECT * across a large table can't tie up the database or flood the page.
- Cache the schema
- Introspection runs one SHOW COLUMNS query per table on every request. A short-lived cache removes that cost, and adding foreign keys to the prompt would help the model write joins.