How to Build AI Agents That Don't Delete Your Database A developer outlines a three-layer safety framework for AI agents that interact with databases, emphasizing structural safeguards over simple prompts. The approach includes action boundaries, pre-execution validation, and post-execution monitoring, with idempotency keys and human-in-the-loop approval for high-risk operations. The system stores before-images of records to enable data-level rollbacks in case of errors. Suppose an AI agent starts making bulk edits across thousands of records. Not deleting data, but rewriting descriptions with hallucinated details. The system catches it because an automated validation gate rejects the output. No real client is harmed, but the scenario shows why safety needs to be structural. If you're building an AI-powered SaaS where an agent can write, update, or delete data, you need a safety framework before you need features. Here's what I've learned from shipping production systems that let LLMs touch real databases. Most teams start with one guardrail and call it done. A prompt that says "don't delete anything." A confirmation dialog. A rate limit. That's not enough. I structure agent safety in three layers that each catch a different failure mode. Layer 1: Action boundaries. The agent can only call functions you explicitly define. No raw SQL access. No direct database writes. Every action goes through a typed function with its own validation. Layer 2: Pre-execution validation. Before any write happens, the system checks the action against business rules. Is this user authorized? Does the data pass schema validation? Is the operation idempotent? Layer 3: Post-execution monitoring. After the action completes, you log what happened, compare it to what was expected, and alert on anomalies. Here's what this looks like in practice. The most dangerous property of LLM-generated actions is that they're not naturally idempotent. An agent might call "update job listing" twice because it didn't get a clear confirmation the first time. If that action increments a counter or appends to a field, you've got corrupted data. A production pipeline processing thousands of records daily needs an idempotency layer. Every write action requires an idempotency key, usually a hash of the action type plus the target record ID plus a timestamp window. interface AgentAction { type: 'update listing' | 'create draft' | 'flag content'; targetId: string; payload: Record