SQL Permission Isolation and Infinite Loop Prevention When Introducing Internal Agents
Permission Isolation for Legacy Database and Agent Integration
When a backend engineer at a mid-sized IT company connects a text-to-SQL generation agent to a legacy database, the primary risk is data loss or corruption caused by the execution of destructive commands. Because prompt instructions alone cannot completely block prompt injection, building multiple defense lines at the infrastructure and middleware layers is essential. By applying the principle of least privilege, you must fundamentally block the agent from executing DDL and destructive DML commands at the database engine level.
In a PostgreSQL environment, create a dedicated agent role, revoke all privileges on the default schema, and grant SELECT permissions only on authorized views. First, open the terminal and connect to the database with administrator privileges. Next, create an agent-exclusive account, revoke all write permissions except for CONNECT on the default database, and configure it to access only the schema dedicated to business views. Finally, explicitly grant SELECT permissions solely on authorized views so that schema modification attempts are rejected at the storage engine level. Once this configuration is complete, even if a query generated by the agent accidentally deletes data, the database engine blocks it immediately, maintaining system stability.
Regular expression-based keyword matching is vulnerable to bypass techniques such as comment insertion or mixed casing, and carries the risk of ReDoS attacks. Instead, you should introduce an AST-based security middleware that converts queries into an abstract syntax tree via a parser and deterministically validates tree nodes. Install and import the sqlglot library into your project's security middleware module. Inside the parsing class, write a filter that defines Drop, Delete, Truncate, Insert, Update, Alter, and Create nodes as unauthorized. Complete a validation method that parses incoming queries, verifies whether they are single SELECT statements, traverses the tree, and raises a SecurityException when forbidden nodes are detected. Applying this approach achieves a 100 percent variant injection detection rate while minimizing the impact on overall API latency with an overhead of about 500 microseconds (0.5 milliseconds) per query.
If database query results are injected directly into the context window of an LLM prompt, there is a risk that personally identifiable information (PII) will leak to external API providers. You must preprocess the data by building a masking pipeline that combines regular expression patterns and custom identifiers in the application layer. Define a class in the backend application containing a dictionary of regex patterns for detecting resident registration numbers, phone numbers, email addresses, and salary information. Implement pipeline methods that replace columns with alternative strings if sensitive words are included in the retrieved record's keys, and filter patterns within string values using a masking function. Placing this preprocessing step as a mandatory step between data retrieval and agent injection eliminates the risk of sensitive data leakage while reducing context noise, thereby lowering the hallucination rate by over 70 percent.
Control Plane Interface and Budget Management for Non-Development Roles
For non-development departments to safely utilize AI agents, an interface that allows operating control panels through natural language inputs without complex prompt writing, alongside a token control architecture that prevents indiscriminate API call costs, is essential.
Build a minimum viable product (MVP) UI using v0 and open-source frontend components that enables non-developers to configure dashboards using only text input. Initialize a standard interface package based on React, Tailwind CSS, and Shadcn UI in your project directory. Divide the application into responsive components: an input component that receives natural language requirements, a monitoring panel that visually clarifies the queries transformed by the agent and whether they passed the AST, and a data grid that renders query results and activates approval request buttons. This allows operations and CS team members to safely query internal data and process tasks without coding knowledge, setting up the environment in under two hours.
To prevent cost explosions caused by indiscriminate API calls from employees, you must implement reservation and settlement architecture at the gateway level. Create a YAML file for LiteLLM Proxy settings in the server environment and enter the master key and Redis host information. Specify the routing strategy as usage-based routing, set a maximum monthly budget limit, and start the proxy server. By introducing this hierarchical limit middleware, you can permanently block costs from exceeding the specified daily limit even in cases of operational errors by non-development staff or infinite-calling scripts, reducing in-house AI infrastructure operating costs by an average of over 30 percent per month.
For tasks that entail side effects beyond data reading—such as system schema modifications or status updates—the control plane must enforce manual approvals to prevent agents from executing them independently. Import the LangGraph framework and checkpoint memory saver to define the agent control state graph. Write a conditional routing function that determines the task type in the agent planning node and sets the status to pending approval if it is a write operation. When compiling the graph, specify the execution node in the interrupt_before parameter to pause tasks. The non-development interface detects pending approval events to expose approval UI cards, updating the status upon clicking approval to resume the workflow.
Automated Prevention of Agent Infinite Loops and Exception Handling
When an agent receives an error as a result of an external tool call, state oscillation—where it repeatedly recalls the same failing tool with the same arguments—causes unnecessary token consumption. You must implement a LangGraph-based monotonically increasing steps counter and a circuit breaker based on tool call parameter hashes in the routing function.
Write a TypedDict class in the LangGraph state definition that includes a message list, a steps counter, and a tool call history list. Implement logic in the tool execution node to generate a hash value combining the tool call name and arguments of the last message and append it to the history list. Set conditions within the routing function to branch to the exception handling node if steps exceed the allowable range or if identical hash values are detected consecutively. The exception handler node injects a system override message and sets the circuit breaker state to true, gracefully terminating the process. Applying this system prevents entire session collapses caused by exceptions and reduces practical debugging and monitoring time by over 5 hours a day.
When introducing workflows where agents directly modify code or create automated pull requests, security gates that verify vulnerabilities or rule violations must be integrated into the CI pipeline. Create a YAML configuration file in the repository's GitHub Actions workflow directory and set up pull request event triggers. Add a Python environment setup step to the pipeline pipeline, install linter packages, and write commands to run static analysis. In the subsequent step, invoke the Semgrep action to perform OWASP Top 10 and LLM vulnerability scans, completing a configuration that uploads SARIF result files to the security tab. Code authored by agents that fails to pass this security gate is blocked from auto-merging, and the reasons for failure are posted as review comments.
In emergency situations where the circuit breaker triggers in production or external API integration errors accumulate, build a mechanism to send Slack webhook alerts and automatically roll back the control panel state to the previous stable checkpoint. Create an alert and rollback management class that accepts the Slack webhook URL as an argument in its initialization method. Write an alert method that constructs a JSON payload containing the session ID and trigger reason upon error occurrence and sends an HTTP POST request via webhook. Implement a rollback method that queries state history from the checkpoint store to find the previous stable version checkpoint and forcefully restores the current state. Introducing this automated control pipeline allows backend engineers to recover systems to a previous healthy state without manual intervention.