How to Configure LlamaIndex SQL Server Sandbox
You set up a llamaindex sql server sandbox by pairing read-only database roles with named table lists. You also isolate the database user so the AI agent cannot change data or read every table. Many teams start by giving their AI app a shared admin login to test text-to-SQL fast. That broad role lets a bad prompt alter rows, drop tables, or read private user records. A safe setup limits both how the agent logs in and which tables it sees. How to set up a llamaindex sql server sandbox LlamaInd
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You set up a llamaindex sql server sandbox by pairing read-only database roles with named table lists. You also isolate the database user so the AI agent cannot change data or read every table. Many teams start by giving their AI app a shared admin login to test text-to-SQL fast. That broad role lets a bad prompt alter rows, drop tables, or read private user records. A safe setup limits both how the agent logs in and which tables it sees.
How to set up a llamaindex sql server sandbox
LlamaIndex connects to relational databases through a light wrapper around SQLAlchemy. Its natural language query engine turns user questions into SQL queries and runs them against your database. If you do not name specific tables, the query engine pulls all schema context by default. Pulling every table wastes context tokens and exposes internal tables to the model. Your first step is passing a strict list of allowed tables to the query engine.
Next, you must restrict the database account that the framework uses to log in. The official LlamaIndex docs warn that running arbitrary SQL queries carries real security risks. The docs advise teams to use restricted roles, read-only databases, and sandboxing. In SQL Server, a database user represents the identity of a login inside a specific database. Teams that need access to many databases on an instance often create a login in the master database. When an agent only needs one database, a contained database user with its own password works well. An admin must enable contained databases on the server and database first.
Once you create the user, you assign fixed database roles to control what it can do. You cannot change the permissions built into SQL Server fixed database roles. Every role, including the public role, has permission to connect to the database. Adding the agent user to the data reader role lets it read all user tables and views. You can also add the user to the deny data writer role. That role blocks the account from adding, changing, or deleting data in any user table. Avoid the database owner role, because it allows full maintenance and can even drop the database.
What native roles solve and what they leave open
Native SQL Server roles and LlamaIndex table lists solve the first layer of risk. The deny data writer role stops the AI agent from running destructive write commands. Even if a prompt tricks the model into generating a delete statement, the database rejects it. Naming allowed tables in LlamaIndex keeps the model focused on the right schema context. Contained database users also stop the agent from hopping across other databases on the same server instance.
Still, static database roles only decide who can log in and which broad actions work. The native data reader role grants read access to all user tables and views at once. If you want to block a few tables, role membership alone will not filter them out. Also, the LlamaIndex table list only guides the prompt context. It does not stop the generated SQL query from joining a table outside that list. If the database user has the data reader role, SQL Server still runs that cross-table query.
Kubernetes API Server Access + AI Sandbox Environments: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Native roles also cannot inspect what happens inside an individual query while it runs. A read-only user can still select thousands of customer emails, phone numbers, card numbers, or national IDs. Once the query runs, those raw values flow straight into the model prompt and logs. Data masking that rewrites matched fields in results is needed to keep those fields safe. Finally, storing static database passwords in app files creates secret sprawl across developer machines and servers.
Why runtime query checks matter for AI agents
Text-to-SQL agents behave very differently from standard backend apps with fixed code paths. A normal web app runs a small set of tested queries over and over. An AI agent writes brand-new SQL statements on the fly based on user questions. You cannot predict every join, filter, or column the model will pick ahead of time. Because the queries change on every run, static setup checks are only half the job.
Teams running AI agents in production need visibility into every query the model generates. When an agent runs a slow join or scans a huge table, engineers need clear logs. They need to see which user prompt triggered that specific SQL statement. Pairing least-privilege SQL Server roles with runtime inspection gives teams control over both login rights and live data flows.
Check SQL Server batches and results on the wire
SQL Server logins and roles decide who connects. They do not check what each TDS batch does or mask sensitive values in returned rows. With hoop.dev, you run an open-source proxy, a sidecar for your SQL Server. It reads plain batches and parameterized queries on the TDS wire while passing Windows login through untouched.
Before a batch hits the server, hoop.dev guardrails check the statement against your rules. Disallowed actions get a native TDS error with your message. On the way back, hoop.dev data masking rewrites sensitive values in supported result columns that match your rules. Every query and decision is logged. Explore the MIT-licensed project on GitHub.
Frequently asked questions
Why does LlamaIndex warn against running raw text-to-SQL queries?
LlamaIndex turns user prompts into SQL queries and runs them right against your database. Without strict roles or a sandbox, a bad query can change data or read sensitive tables.
Does the SQL Server data reader role limit access to single tables?
No, members of the fixed data reader role can read all data from all user tables and views. To restrict specific tables, you must configure granular permissions instead of using only fixed roles.
How does a contained database user help isolate an AI agent?
A contained database user stays inside a single database and does not use a login in master. This setup keeps the AI agent tied to one database instead of the whole server instance.
Related guides
In this post