Back to Blog
·3 min read

The unsexy part of the agentic revolution nobody at GTC mentioned: RLS policies, tenant isolation, and error recovery at 2am

engineeringaiautomation
The unsexy part of the agentic revolution nobody at GTC mentioned: RLS policies, tenant isolation, and error recovery at 2am
Table of Contents

Everyone's building AI agents that can book flights, write code, and manage your calendar. The demos look incredible. But while everyone at GTC was talking about reasoning capabilities and model benchmarks, they skipped the part where your agent accidentally deletes your customer's data at 2am because you forgot about row-level security.

Here's what nobody mentions when they show off their agent demos: the infrastructure nightmare that happens when autonomous systems start touching real databases with real customer data.

Row-Level Security Isn't Optional Anymore

Your AI agent needs database access to do useful work. But unlike your carefully controlled API endpoints, agents make unpredictable queries. They might ask for "all customer data from last month" when they should only see data for the current tenant.

Row-level security (RLS) policies become your safety net:

CREATE POLICY tenant_isolation ON customer_data
FOR ALL TO ai_agent_role
USING (tenant_id = current_setting('app.current_tenant_id')::uuid);

The agent can query however it wants, but PostgreSQL enforces tenant boundaries at the database level. No fancy prompt engineering required.

Set the tenant context when your agent connects:

SET app.current_tenant_id = 'tenant-uuid-here';

Now even if your agent goes rogue, it can't cross tenant boundaries. This isn't theoretical. I've seen agents try to "help" by pulling data from similar companies to provide better context. RLS stops that cold.

Error Recovery When Agents Break Things

Agents fail differently than traditional code. Instead of clean exceptions, you get partial database updates, half-sent emails, and API calls that succeeded on their end but failed on yours.

Build idempotent operations from day one:

-- Instead of INSERT, use UPSERT
INSERT INTO processed_orders (id, status, processed_at) 
VALUES ($1, 'completed', NOW())
ON CONFLICT (id) DO UPDATE SET 
  status = EXCLUDED.status,
  processed_at = EXCLUDED.processed_at;

Every agent action should be resumable. If your agent crashes halfway through processing 100 customer records, it should pick up where it left off, not start over.

Log everything with correlation IDs:

agent_id: agent-7f3b2
correlation_id: req-8x9k1
action: process_customer_data
status: failed
error: rate_limit_exceeded
retry_count: 2

When things break at 2am (and they will), you need to trace exactly what the agent was trying to do.

The Multi-Tenant Agent Architecture

Most agent frameworks assume single-tenant deployment. But if you're building a SaaS product, your agents need to handle multiple customers safely.

Create separate agent instances per tenant, not shared agents with tenant-aware prompts. Shared state is where security bugs hide.

Use database connection pooling with tenant-specific pools:

const pool = new Pool({
  host: 'localhost',
  database: 'main',
  user: 'ai_agent_role',
  password: process.env.DB_PASSWORD,
  application_name: `agent-${tenantId}`
});

This makes debugging easier and gives you per-tenant connection limits.

Monitoring Agents That Think for Themselves

Traditional monitoring tracks requests per second and error rates. Agent monitoring needs different metrics:

  • Decision audit trails: Why did the agent choose action X over Y?
  • Resource consumption per reasoning step: Is the agent stuck in loops?
  • Cross-system state consistency: Did the CRM update match the email that was sent?

Set up alerts for agent behavior, not just system health. If an agent suddenly starts making 10x more database queries, something changed in its reasoning pattern.

Start Building the Boring Stuff Now

The sexy AI capabilities will keep improving. The infrastructure challenges won't solve themselves.

Before you deploy that demo to production, ask yourself: Can you safely roll back an agent's actions? Do you know which tenant's data it touched? Can you debug what happened when it inevitably breaks at 2am?

The companies that figure out the unsexy infrastructure parts will be the ones still running when the hype cycle ends.

ShareXLinkedIn
TK

Tobias Koehler

Founder, ConnectEngine