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
- Product application. Accounts, tenants, permissions, billing, the UI. Ordinary SaaS, and it should stay ordinary.
- 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.
- Retrieval. Finding the right documents or records to give the model. Search indexes, vector search, filters by tenant and permission.
- Model gateway. One internal interface for every model call: provider choice, retries, timeouts, rate limits, logging, cost capture.
- Validation and guardrails. Schema checks on structured output, business rules, confidence thresholds, routing to human review.
- Evaluation and feedback. Fixed test sets, scoring on each release, capture of user corrections.
- 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
| Layer | Without it | Build before launch? |
|---|---|---|
| Orchestration with queues | Timeouts on long tasks, lost work when a request fails | Yes |
| Retrieval with tenant filters | One customer's data in another's answer | Yes |
| Model gateway | Provider change means touching every feature | Yes, even a thin one |
| Output validation | Malformed or nonsensical output reaches users | Yes |
| Evaluation set | No idea whether a change made things better | A small one, yes |
| Feedback capture | Corrections vanish, quality never improves | Basic version |
| Per-tenant metering | Surprise margins and unfixable pricing | Yes, 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
- Standard SaaS foundations: auth, tenants, billing hooks, audit log
- A thin model gateway with logging and cost capture from the very first call
- One end-to-end AI workflow on a queue, with validation and a human review screen
- Fifty to two hundred real examples as an evaluation set, run before each release
- Per-tenant usage dashboards for your own team
- 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?
Should AI logic live in a separate service?
What is a model gateway?
How do we keep costs visible in the architecture?
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.
Related services
What we build for problems like this one