01 Work

02 Writing

Nothing published yet

03 Deep dives

Nothing published yet

04 Teardowns

Nothing published yet

The story

↑↓ moveEnter openEsc close

All work

MSFA Multi-Tenant AI Agent Platform

A production AI assistant for a multi-tenant enterprise SaaS, secure and cost-controlled on every run.

Architecture diagram: a user request passes gate checks for platform switches, plan features and user permissions, then reaches the AI agent core, which applies PII redaction, token budgets and human-in-the-loop checks. The agent runs report generation, a SQL parser, clarification interrupts and Redis checkpoints, on a provider-agnostic model layer (OpenRouter, OpenAI, local Ollama) with encrypted keys.

The problem

Tenants on an enterprise platform wanted an assistant that could answer questions and generate reports from their own data, without one tenant ever seeing another's data, and without runaway model costs.

The approach

  1. A LangChain/LangGraph ReAct agent behind a NestJS API and a Next.js UI.
  2. Layered gate checks before any run: platform switches, plan features, then user permissions.
  3. A middleware stack around every call: permission-based tool filtering, PII redaction, model retry with cross-provider fallback, call limits, human-in-the-loop approvals and per-tenant token budgets.
  4. Tools for report generation and read-only SQL. SQL is restricted to curated views by a parser allowlist and a locked-down database role, on top of PostgreSQL row-level security.
  5. Clarification and approval interrupts are checkpointed in Redis, so a run can pause for a human and resume exactly where it stopped.
  6. A provider-agnostic model layer (OpenRouter, OpenAI-compatible, local Ollama) with encrypted keys and task-based model profiles.

Architecture

How a request flows through the system, top to bottom.

  1. Client
    • Next.js UI
  2. API
    • NestJS
    • Gate checks: platform → plan → permission
  3. Agent
    • LangGraph ReAct
    • Redis checkpoints
    • Human-in-the-loop interrupts
  4. Middleware
    • Tool filtering
    • PII redaction
    • Retry + fallback
    • Token budgets
  5. Tools
    • Report generation
    • Read-only SQL (allowlist, curated views)
  6. Models & data
    • OpenRouter / OpenAI-compatible / Ollama
    • PostgreSQL + RLS
    • LangSmith tracing

Results

isolation enforced at gate, tool and database layers
Multi-tenant
token budgets and call limits keep cost predictable
Per-tenant
behind one model layer, with automatic fallback
3 providers

Stack

TypeScript · NestJS · Next.js · LangChain · LangGraph · PostgreSQL (RLS) · Redis · OpenRouter · Ollama · LangSmith · Docker