There is a common shape to how database agents reach production. Someone builds a demo against a read replica with their own credentials, it works, and the credentials never change because nothing has gone wrong yet.
The guardrails below are the ones I would put in before that demo goes anywhere near real data. Almost none of them live in the prompt, which is the point. A prompt is a request. A permission is a rule.
Do not rely on the prompt
"Only generate SELECT statements" in a system prompt is a preference. It holds most of the time, which is the worst possible property for a safety control, because it holds long enough for you to trust it.
Every control below sits outside the model, where it cannot be talked out of.
The database account
Create a dedicated role for the agent. Not your application's role, and certainly not an admin one.
Key Takeaways
- Do not rely on the prompt
- The database account
- Statement timeout and query cost
- Run it somewhere that is not production
Grant SELECT and nothing else. No INSERT, no UPDATE, no DELETE, no DROP, no CREATE. If your database supports it, mark the session read-only as well, so a write fails at the transaction level even if a grant is wrong somewhere.
Restrict it to the schemas the agent is meant to see. If there is a table of password hashes, salary data or anything else the asker should not be able to read, the agent must not be able to reach it, because anything it can query it can be persuaded to summarise.
This single step removes most of the catastrophic outcomes. It costs about twenty minutes.
Statement timeout and query cost
Set a statement timeout on the role. Something short: five or ten seconds. Nothing in the generation step knows what an expensive query costs, and a cartesian join across two large tables is trivially easy to generate. Without a timeout it sits on your replica until somebody notices.
If your database can reject a query on estimated cost before running it, use that too. Postgres can be persuaded to via a planner hook or a wrapper that runs EXPLAIN first and refuses above a threshold. It is the difference between a slow answer and an incident.
Cap the rows returned. A LIMIT the agent cannot remove, applied in your code. Asking for it in the prompt is not the same thing. Nobody needs four million rows summarised into a chat window.
Run it somewhere that is not production
A read replica, ideally. The agent's workload is unpredictable by design, which is exactly the workload you do not want sharing resources with checkout.
Replica lag is worth thinking about once and then telling people. If the replica is thirty seconds behind, say so next to the answer. Someone will eventually ask why the agent's number differs from the dashboard, and the answer being visible saves that conversation.
Log every query
Every generated query, who asked, what English produced it, how long it ran, how many rows came back. Store it.
You want this for three reasons. It is how you debug a wrong answer. It is how you find out which questions your data cannot actually answer. And if anyone ever asks what the agent has been reading, the log is the only honest reply.
Injection through the question
The question is user input that becomes part of a prompt that becomes SQL. Treat it that way.
Parameterisation does not apply here in the usual sense, because the model is writing the whole statement. The controls that do apply are the ones above: a role that can only read, a schema it cannot escape, a timeout, a row cap. Someone crafting a question designed to make the agent read a table it should not is stopped by the grant. The wording of your instructions will not do it.
Also strip or reject questions containing SQL keywords and semicolons if your interface has no reason to accept them. It is a weak control on its own and it is free alongside the strong ones.
Show the SQL to the person asking
A safety control that is also a product feature. If the person can see the query, they can catch the query that ran fine and answered a slightly different question, which is the failure mode most likely to reach a decision. That case is worth its own read: [the wrong number that never throws an error](/wrong-number-text-to-sql-agent/).
The short version
Dedicated read-only role, limited schemas, statement timeout, row cap, replica, full query log, visible SQL.
Seven things, most of them configuration, and together they turn a database agent from something you would not put near production into something you would. None of them is interesting work, which is probably why the tutorials leave them out.
The rest of the failure surface is in [what breaks in production](/langgraph-sql-agent-natural-language-database/), and the schema problem that shapes everything else is in [your schema does not fit in the prompt](/agent-schema-does-not-fit-in-the-prompt/).
Read-only by default: guardrails for a database agent
About the Author
Ronnie Huss is a serial founder with multiple successful product launches. He builds technology products across SaaS, AI, and blockchain. Learn more about Ronnie Huss →
Follow on LinkedIn
Written by
Ronnie Huss Founder and Consultant, AI Search Visibility, SEO and ConversionSerial founder with multiple successful product launches across SaaS, AI tools and blockchain. Based in London. Writing on AI agents, GEO, RWA tokenisation, and building AI-multiplied teams.