Skip to content

AI Engineering

Human-Approved AI Agents with n8n & PostgreSQL in Production

A production reference architecture for AI agents that customers and auditors can trust: bounded tool permissions, citation-grounded RAG, approval gates, idempotent webhooks, and full execution logs.

TECH360 Engineering · October 28, 2026 · 11 min read

The trust problem

An AI agent that can send messages, move money, or edit records is a liability unless every action is bounded, grounded, logged, and — where it matters — approved by a human first. This is the architecture we deploy for clients who need agents in production, not in a demo.

Layer 1: Bounded tool permissions

Each agent gets an explicit tool allow-list stored in PostgreSQL:

CREATE TABLE agent_tools (
  agent_id     UUID NOT NULL REFERENCES agents(id),
  tool_key     TEXT NOT NULL,          -- e.g. 'crm.create_lead'
  scope        JSONB NOT NULL,         -- row/tenant boundaries
  requires_approval BOOLEAN NOT NULL DEFAULT true,
  PRIMARY KEY (agent_id, tool_key)
);

The runtime refuses any tool call not present in this table. Scope JSONB enforces tenant isolation: an agent for Client A can never read Client B rows, even by prompt injection, because the database query layer appends the tenant filter server-side.

Layer 2: Citation-grounded RAG

Answers must cite records. Retrieval pulls from a knowledge table with versioned, approval-stamped content; the agent's response is assembled with citation IDs that the UI renders as footnotes. If retrieval confidence is below threshold, the agent says "I'll have a human follow up" and creates a task — it does not improvise.

Layer 3: Human-in-the-loop approval gates

Any action with requires_approval = true writes to an approvals queue instead of executing:

  1. Agent drafts the action (message, refund, record change) with its grounding citations.
  2. A human reviews in the CRM inbox: Approve / Edit / Send or reject.
  3. Only on approval does the executor run the tool, with the approver's identity journaled.

This single pattern converts "the AI sent something wrong" from a liability into a non-event: nothing client-facing leaves without a human signature.

Layer 4: n8n orchestration with retry safety

n8n runs the plumbing: inbound webhooks (WhatsApp Cloud API, Stripe, forms), scheduled jobs, and CRM-to-ERP syncs. Production rules:

  • Idempotency keys on every webhook — dedupe on (source, event_id) before processing.
  • Exponential backoff with a bounded retry count, then a dead-letter table a human can inspect.
  • Execution audit log — every run, duration, status, and payload hash is queryable by clients.
CREATE TABLE workflow_executions (
  id           BIGSERIAL PRIMARY KEY,
  workflow     TEXT NOT NULL,
  idempotency_key TEXT UNIQUE NOT NULL,
  status       TEXT NOT NULL,          -- SUCCESS | RETRY | FAILED
  attempts     INT NOT NULL DEFAULT 1,
  duration_ms  INT,
  payload_hash TEXT,
  created_at   TIMESTAMPTZ DEFAULT now()
);

Layer 5: BYOK economics

Clients bring their own LLM API key. Token spend goes directly to the provider at list price — zero markup — while the engineering value stays in the guardrails, grounding, and logs around the model. Model choice becomes a swap, not a migration.

What production actually looks like

A WhatsApp sales agent under this architecture: receives an enquiry (idempotent webhook), retrieves inventory and pricing with citations, drafts a grounded reply, waits for a human Approve/Edit/Send, sends via the official Cloud API, advances the pipeline stage, and journals every step. Auditors can replay any conversation end to end. That is the bar — and it is achievable with boring, proven components.

Put this into practice

Get a written Scope of Work with USD milestone pricing — or estimate your build first with our free interactive calculators.