Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
SaaS & Product

AI SaaS Architecture: The Layers That Matter

Last updated:

The model is the smallest box on the diagram

Founders often draw their AI product as a user, an arrow and a model. The real system is a conventional SaaS application with a pipeline in the middle that happens to call a model somewhere inside it. Nearly every production incident we see comes from that pipeline, not from the model.

A 12-person startup building contract review software, say, will spend perhaps a tenth of its engineering time on prompts. The rest goes into ingesting documents, keeping tenants apart, validating outputs, handling provider outages and working out why one customer's bill tripled. That ratio surprises people. It should shape the architecture from the start.

The seven layers, from the outside in

  1. Product application. Accounts, tenants, permissions, billing, the UI. Ordinary SaaS, and it should stay ordinary.
  2. Orchestration. The code that turns a user action into a sequence of steps: fetch context, call the model, validate, maybe call again, store the result. Usually a job queue plus workers.
  3. Retrieval. Finding the right documents or records to give the model. Search indexes, vector search, filters by tenant and permission.
  4. Model gateway. One internal interface for every model call: provider choice, retries, timeouts, rate limits, logging, cost capture.
  5. Validation and guardrails. Schema checks on structured output, business rules, confidence thresholds, routing to human review.
  6. Evaluation and feedback. Fixed test sets, scoring on each release, capture of user corrections.
  7. Metering and observability. Tokens, latency, errors and cost per tenant, per feature, per request.

None of these is novel. The mistake is merging them. When the prompt, the retry logic and the tenant filter all live in one function inside a request handler, every change is risky and every incident is a detective story.

What each layer protects you from

LayerWithout itBuild before launch?
Orchestration with queuesTimeouts on long tasks, lost work when a request failsYes
Retrieval with tenant filtersOne customer's data in another's answerYes
Model gatewayProvider change means touching every featureYes, even a thin one
Output validationMalformed or nonsensical output reaches usersYes
Evaluation setNo idea whether a change made things betterA small one, yes
Feedback captureCorrections vanish, quality never improvesBasic version
Per-tenant meteringSurprise margins and unfixable pricingYes, at least logging

Almost everything is a yes, which is uncomfortable for a team trying to ship. The trick is that each layer can start very thin. A gateway can be a single module of two hundred lines. An evaluation set can be fifty real examples in a spreadsheet with expected answers.

Synchronous or asynchronous

Decide early which AI tasks run while the user waits and which run in the background. Anything that might take more than a few seconds, touch many documents or call the model repeatedly belongs on a queue, with the UI showing progress.

Chat-style features feel synchronous but benefit from streaming and from a server-side record of each turn. Batch features such as nightly classification of every new ticket should never run in a web request at all. Getting this wrong is the most common reason AI products feel flaky: a request times out, the user retries, and now the model has been paid twice for the same work.

Where the data lives

An AI SaaS product stores more kinds of data than a normal one, and each kind needs a clear home and a clear retention policy.

  • Source content: uploaded files, synced records, emails, usually in object storage with references in the database
  • Derived content: extracted text, chunks and embeddings, rebuildable from the source
  • Model interactions: prompt version, inputs, outputs, model, cost, latency
  • Human feedback: accept, reject, edits, with who and when
  • Evaluation data: curated examples with expected results, kept separate from live customer data

PostgreSQL with a vector extension handles the database side comfortably for most products until well into the thousands of tenants. A dedicated vector database earns its place later, if at all. Deletion matters more than people expect: when a customer leaves, their embeddings, logs and cached outputs have to go too. Our multi-tenant SaaS architecture guide covers the tenancy patterns underneath this.

A sensible build order

  1. Standard SaaS foundations: auth, tenants, billing hooks, audit log
  2. A thin model gateway with logging and cost capture from the very first call
  3. One end-to-end AI workflow on a queue, with validation and a human review screen
  4. Fifty to two hundred real examples as an evaluation set, run before each release
  5. Per-tenant usage dashboards for your own team
  6. Retrieval improvements, caching, model routing and fallbacks as volume demands

Step two is the one we insist on at SpiderHunts. Retrofitting logging and cost tracking after launch means months of flying blind on exactly the numbers that decide whether the business works.

When this is too much architecture

If you are validating an idea with five design partners, most of this is premature. A prototype that calls a model directly, with a person checking every output, will teach you more in a month than a beautifully layered platform. Our note on Python AI service architecture shows how to keep early code clean enough to evolve.

The point at which layers become non-negotiable is the first customer who is not a friend. From then on, a data leak or an unexplained wrong answer is a commercial problem, not a learning experience. If you want a second pair of eyes before that point, our SaaS development team reviews AI product architectures regularly.

Frequently asked questions

Do we need a vector database for an AI SaaS product?

Usually not at the start. PostgreSQL with a vector extension, combined with ordinary keyword search and tenant filters, covers most products well. Move to a dedicated service when scale or query patterns genuinely require it.

Should AI logic live in a separate service?

A separate worker process on a queue is sensible early, because AI tasks are slow and bursty. A fully separate microservice with its own team is rarely worth it until you are much larger. Keep clear module boundaries and you can split later.

What is a model gateway?

It is a single internal layer that every model call goes through. It handles provider selection, retries, timeouts, rate limits, logging and cost recording, so product features never talk to a provider directly.

How do we keep costs visible in the architecture?

Record tokens, model and computed cost on every call, tagged with tenant, feature and request ID. Aggregate daily. It is cheap to build on day one and painful to reconstruct later.

Keep reading

Planning the architecture for an AI product?

Share a sketch of what you have in mind. We will point out which layers you need before launch and which can safely wait until you have customers.

Book a free 30-minute call Get a project estimate WhatsApp us

Related services

What we build for problems like this one

SaaS DevelopmentCustom Software Development