Skip to content
All writing

Nov 2026

Three locks between an AI agent and a production database

How I let an AI agent investigate support tickets against real data without giving it a way to change that data.

At work I built a small tool that lets an AI agent investigate support tickets. To be useful, the agent has to look at real records, and the records behind a ticket live in production.

"An AI agent with production access" is a scary sentence, so the agent doesn't get one safeguard. It gets three, and each one assumes the others might fail.

Lock 1: a database user that can't write

The query runner connects with an account that only has read permissions. If everything else goes wrong, the database itself refuses an UPDATE.

This is the lock that matters most, because it doesn't depend on my code or on the model behaving.

Lock 2: a read-only session

Right after connecting, the runner sends:

SET SESSION TRANSACTION READ ONLY;

If the runner is ever pointed at a more powerful account by mistake, MySQL still rejects writes for that session.

Lock 3: fixes are scripts a person runs

The agent's instructions say it never changes production data itself. When a ticket needs a data fix, it writes a SQL script instead, and every script has the same shape:

  • SET @dry_run := 1 at the top, so running it as-is changes nothing
  • backup tables created before anything else
  • the changes inside a transaction, with checks that the rows still look the way the agent saw them
  • a single verdict row at the end, then a rollback unless dry run is switched off

A person reads the script, runs it in dry-run mode, checks the verdict, and only then switches it off.

Why three

A prompt is an instruction, not a guarantee. A model can misread a ticket, copy a bad example, or follow text inside a ticket that tells it to do something it shouldn't. Lock 3 handles the normal case. Locks 1 and 2 handle the case where lock 3 fails.

The result is an agent that's useful on real data from day one, where the worst it can do is write a script nobody runs.