How to Stop an AI Agent From Emailing 400 Customers at 3AM: Budget Caps, Idempotency & a Kill Switch in n8n
Description and use
Implementing a guard pattern in n8n with idempotency, monthly budget, rate cap, and kill switch prevented a mass email incident and controlled LLM spend.
Problem
An unattended AI agent sent 400 incorrect emails at 3 AM, causing operational chaos.
Outcome
The guard pattern stopped unintended mass emailing, enforced budget limits, and added a kill switch, reducing operational risk.
Workflow
- 1. Generate SHA‑256 idempotency key from client, action type, recipient, payload.
- 2. Insert key into guard_actions ledger in Postgres.
- 3. Query ledger for monthly cost per client and compare to cap.
- 4. Count actions in last hour and compare to rate cap.
- 5. If any check fails, send Slack alert, stop workflow with Error node; otherwise proceed with LLM call and outbound action.
Evidence
put one guard step in front of every LLM call and every outbound action
SHA-256 of client, action type, recipient, payload
SUM(cost_usd) by client and month vs cap
COUNT(*) of actions in last hour vs cap
Slack alert, then Stop and Error node
- Record date: 2026-11-02T09:14:17.545Z