Designing a Multi-Tenant AI SaaS: Lessons from Building Agentopia by CynetIQ

9 min read
Share:

The Challenge of Multi-Tenant AI

Building a multi-tenant AI SaaS platform presents unique challenges. Unlike traditional SaaS where tenants share the same logic, an AI platform must isolate:

  • AI agent execution — One tenant's agents shouldn't affect another's
  • Token budgets — Usage limits per organization
  • Vector memory — Tenant-scoped similarity search
  • Git credentials — Secure per-org GitHub/Azure tokens
Agentopia by CynetIQ handles all of this with a clean, async-first architecture.

Architecture Decisions

Organization-Scoped Everything

Every database query in Agentopia by CynetIQ is scoped by organization_id. This isn't just a filter — it's a foundational design principle:

  • All SQLAlchemy models include organization_id
  • API routes extract org context from JWT tokens
  • Vector memory queries filter by organization
  • Worker tasks are isolated per organization

Async-First with FastAPI

Agentopia by CynetIQ's backend is fully async:

  • FastAPI with async route handlers
  • SQLAlchemy 2.0 with AsyncSession
  • Redis async consumer for task queue
  • Async HTTP clients for GitHub/Azure/Jira APIs
This ensures high concurrency — multiple organizations can run AI agents simultaneously without blocking.

Redis Queue with Concurrency Control

The worker system uses Redis for task queuing with key safeguards:

  • MAX_WORKERS — Configurable concurrent task limit
  • Queue lock guard — Prevents same-repo concurrent execution
  • Stale job watchdog — Auto-fails stuck tasks
  • Retry with backoff — Handles transient LLM API failures

Usage Enforcement and Billing

Agentopia by CynetIQ enforces per-organization limits:

  • Task quotas — Free tier: 5 tasks/month, Pro: unlimited
  • Token tracking — Per-task and per-org token usage counters
  • Cost allocation — Track AI spend by task, sprint, and organization
  • Stripe + Iyzico — Dual payment provider support

Security Patterns

JWT Authentication

Every API request is authenticated with JWT tokens containing:

  • User ID and role
  • Organization ID
  • Token expiration

Organization Isolation

Even if a user has valid credentials, they can only access data within their organization. This is enforced at the ORM level, not just the API level.

Credential Encryption

GitHub tokens, Azure PATs, and Jira credentials are encrypted at rest and only decrypted during agent execution within the worker process.

Lessons Learned

  • Scope everything by org from day one — Retrofitting multi-tenancy is painful
  • Track AI costs per tenant — Usage-based billing requires granular tracking
  • Async is non-negotiable — AI operations are I/O heavy and slow
  • Isolate worker execution — Queue locks prevent data corruption
  • Observable by default — Every AI stage should be logged and traceable
  • Start Building with Agentopia by CynetIQ →

    Related Articles

    Share:

    Ready to try agentic AI?

    Start free and let Agentopia by CynetIQ's 3D agents handle your development workflow.