Agentic AI Notebook
Back to Projects
ProductionPhase 8 50 hours(broken down below)

Multi-Tenant AI Backend

SaaS AI backend with strict tenant isolation for vector stores, databases, API keys, and per-tenant configuration.

PythonFastAPIPostgreSQLPineconeRedisStripe

Project walkthrough

Multi-Tenant AI Backend

1 / 6

Project Goal

SaaS AI backend where tenant data never leaks across boundaries.

  • API key auth resolving to tenant context
  • PostgreSQL RLS + Pinecone namespaces
  • Per-tenant config, quotas, and usage metering
  • Stripe integration for usage-based billing

Use ← → arrow keys or buttons to navigate the walkthrough

Time breakdown (50h)

Each phase maps to the estimated hours — follow in order for a realistic build schedule.

Tenant model & auth

10h
  • Tenant, user, api_key tables with bcrypt-hashed keys
  • Auth middleware: key → tenant_id → request.state
  • Tenant signup and API key rotation endpoints

Data isolation

12h
  • PostgreSQL RLS policies on all tenant-scoped tables
  • Pinecone namespace per tenant for vector data
  • Integration tests proving cross-tenant access fails

Per-tenant config & billing

10h
  • Tenant config: model preference, chunk size, quota limits
  • Usage meter: tokens in/out per request → Redis counters
  • Stripe webhook for usage-based invoicing

API gateway & quotas

10h
  • Per-tenant rate limits and monthly token quotas
  • 429 responses with upgrade CTA when quota exceeded
  • Admin API for tenant management and usage reports

Security audit & tests

8h
  • Pen-test: attempt cross-tenant data access (must fail)
  • Audit log for all admin actions
  • Load test 50 concurrent tenants

Architecture

Every request is authenticated via API key, which resolves to a tenant context injected into all downstream queries. PostgreSQL RLS enforces row isolation, Pinecone namespaces separate vector data, and a usage meter tracks tokens per tenant for billing and quota enforcement.

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

Prerequisites

  • PostgreSQL row-level security (RLS) concepts
  • Multi-tenancy patterns: shared DB vs schema-per-tenant
  • FastAPI middleware and dependency injection
  • Pinecone namespaces or metadata filtering
  • API key hashing and rotation best practices

Setup steps

  1. Design tenant schema: tenants, users, api_keys, usage_logs tables
  2. Enable PostgreSQL RLS policies scoped to tenant_id
  3. Configure Pinecone with per-tenant namespaces
  4. Build tenant provisioning API (create tenant → namespace + RLS role)
  5. Add API key auth middleware extracting tenant context
  6. Integrate Stripe for usage-based billing webhooks

Features to build

  • Tenant isolation
  • Per-tenant vector namespaces
  • API key management
  • Usage metering
  • Row-level security

Expected result

Provision two tenants, ingest different document sets for each, demonstrate that Tenant A cannot retrieve Tenant B's data even with a crafted query, and show per-tenant usage dashboards with token counts and billing status.

Resume bullet points

  • Architected multi-tenant AI SaaS backend with strict data isolation for 100+ tenants
  • Implemented row-level security and per-tenant vector namespaces

Interview questions

How do you enforce tenant isolation in a shared-database architecture?
PostgreSQL row-level security policies filter every query by tenant_id from auth context. Combine with Pinecone namespaces, never trust client-supplied tenant_id, and integration-test cross-tenant access attempts.
Shared DB vs schema-per-tenant — when to choose each?
Shared DB with RLS scales to thousands of small tenants with lower ops overhead. Schema-per-tenant suits enterprise customers needing physical isolation, custom schemas, or regulatory compliance requiring dedicated resources.
How do you meter and bill AI usage per tenant?
Count input/output tokens per request, aggregate in Redis counters flushed to Postgres, enforce quotas at request time, and sync usage to Stripe via webhooks for usage-based invoicing.