Tenant isolation in an AI SaaS product means one customer cannot retrieve another customer's documents, reuse their conversation history or perform actions with their credentials. A login screen alone does not give you that, and neither does a prompt asking the model to keep customers apart. Prompts are not access control.
AI adds new places where customer data travels: retrieval indexes, background workers, caches, conversation memory and tool integrations. Each of them needs the same account separation as the main application. This guide walks through those paths and the checks to run before you invite paying customers.
Start with a trusted tenant context
Determine the active tenant from an authenticated identity and verified membership. If a request picks a workspace, the server must check that the person is allowed into it. A workspace ID sent from the browser only says which workspace the user wants; it says nothing about whether they may open it.
Pass that checked context to every downstream operation. A background task needs the tenant identifier, the operation it is meant to perform and enough authorisation context to apply the right policy. When work runs later, recheck permissions if the operation depends on the person still being a member or still holding the right role.
AWS treats tenant isolation and general authentication as separate problems. A user can be correctly logged in and the application can still let them reach another customer's resources.
Map every boundary an AI request crosses
| Component | Common failure | Boundary to enforce |
|---|---|---|
| Document upload | An object is stored without reliable ownership | Tenant and resource permissions on storage and metadata |
| Retrieval | Search returns chunks from another workspace | Permission filter applied before results reach the model |
| Conversation history | A supplied conversation ID loads another customer's messages | Tenant and user access checks on every history lookup |
| Cache | A repeated prompt returns an answer generated from someone else's documents | Keys that include the relevant tenant and permission context |
| Background jobs | A worker runs with global access and a missing tenant filter | Explicit account context and constrained data access |
| Tools and integrations | The agent uses the wrong customer's CRM connection | Server-side credential selection and operation-level permission checks |
Use the table as a review checklist. Passing it will not make a service secure on its own, and the right controls depend on your data, your tenancy model and the actions your service supports.
Apply retrieval permissions before generation
For an illustrative knowledge-assistant SaaS, two companies may upload documents containing similar project names. If retrieval searches a shared collection without a tenant filter, the model can receive the other company's text, and the final answer may still look plausible. This is a hypothetical example, not a reported client incident.
Apply tenant and resource permissions during retrieval, before any chunk enters the model context. By the time you could filter the final answer, the wrong text may already have shaped the response or been passed to another service.
A shared index can work if its access controls are applied everywhere and tested. Separate collections or infrastructure add another layer of separation, but they also mean more provisioning and operational work. Choose from your isolation requirements; no storage layout solves every access problem by itself.
AWS's guidance on multi-tenant agents applies the same principle to agent knowledge and tools: access policies need to follow the tenant context through the resources the agent uses.
Review memory and caching separately
A conversation ID should never grant access on its own. When loading messages, attachments or summaries, check who owns the conversation and whether the caller may read it. Include generated summaries in retention and deletion plans. If you delete a chat but keep its summary, sensitive material can stay available.
Be careful with shared semantic caches. Two users can send similar prompts and still have access to different documents. If an answer depends on private context, the cache key must include the tenant and the relevant permissions. Cached answers also need invalidating when a person loses access or a document is replaced.
Operational logs should stay useful without becoming another document store anyone can read. Decide what gets recorded, who can look at it and how long it is kept. A support dashboard that shows data from several tenants needs the same review as the customer-facing API.
Constrain tools independently of model instructions
If an assistant can search a CRM, send a message or update a record, the tool handler must validate the action using trusted account context. Do not let the model choose an arbitrary tenant identifier or retrieve global credentials from a tool argument.
Pick the customer's integration connection on the server, limit the operation to resources that customer is authorised to use and validate its inputs. Ask for confirmation before consequential actions, following the product's rules. Confirmation is in addition to the permission check: the person who approves the action must also be allowed to perform it.
Prompt injection can arrive through retrieved documents or tool results, so treat that content as data to process, never as instructions with authority. If a malicious document gets in, model instructions should not be the only thing standing between it and another customer's data.
Background work needs explicit ownership
Uploads often start parsing, embedding and indexing jobs. Keep the tenant and resource identity attached at each step, and check that output objects and index entries keep the same owner when a job retries or a worker restarts.
Plan for deletions and membership changes. An old job should not quietly recreate a deleted document or publish content into a workspace after the operation behind it was cancelled. Where needed, compare resource versions or check the current state explicitly.
Set capacity limits per account as well. One customer's large indexing run should not take all the worker capacity or run up an unlimited model bill. Our guide to AI SaaS metering and spending limits covers how admission and usage accounting fit together.
Build an isolation test matrix
Create two test tenants with deliberately similar data and try to reach one from the other. Include members, administrators and a user whose access has been revoked.
- Swap document, conversation and download identifiers between tenants.
- Search for an exact phrase present only in the other tenant's documents.
- Repeat the same prompt in both tenants and inspect cache behaviour.
- Queue work, change access or delete the resource, then let the worker continue.
- Attempt a tool call naming another tenant's record or integration connection.
- Retry ingestion and verify that no output loses its original ownership.
- Check support and administrative paths as well as normal customer endpoints.
Look at the retrieved context and the tool requests, as well as the final answer. An answer can leave the leaked material out, and then the retrieval failure stays hidden. A negative test passes only when unauthorised content never reached generation and no action was taken on it.
Use database controls as one part of the boundary
Where the stack supports it, database policies can enforce access below the individual API handlers. They do not cover external vector stores, object storage or privileged background services, so trace every route that bypasses the database as part of the review.
In our Supabase workspace and role-layer case study, database Row Level Security and tests were part of the implementation. That case covers one specific access layer. Adding AI retrieval or tools to it would mean reviewing those new paths too.
Run a local RLS reference
The tenant-isolation source bundle includes the anonymised workspace schema, article policies, two-tenant fixtures and a Python runner. It starts its own disposable PostgreSQL container; you need Python 3.9+ and a running Docker engine. Extract the archive, open its directory and run:
python3 tenant-isolation/verify.py --output checks.json
python3 tenant-isolation/verify.py --benchmark --output checks-and-plan.json
The compact check set contains 15 assertions: allowed reads and own drafts, guessed foreign IDs, a forged workspace filter, forbidden writes, missing identity, an outsider and revoked membership. It also checks that RLS is active and the tested role cannot bypass it or truncate the article table. The runner removes its test container after completion.
| Try this | Expected result |
|---|---|
| Read a published article as a member of its workspace | Article is visible |
| Use the exact ID of the other workspace's article | No row returned |
| Insert into the other workspace | Permission error |
| Read after the membership is removed | No row returned |
The harness supplies synthetic identity claims directly. A real service must verify identity before the database receives those claims. These checks cover PostgreSQL access rules; add separate tests for Supabase authentication, API endpoints, retrieval, storage, caches, background jobs and tools. The PostgreSQL row-security documentation explains the role and table-owner exceptions that the test setup avoids.
The optional benchmark reproduces the fixture and measurement protocol behind the published synthetic plan comparison. It checks the full visible-ID set for both policy variants before timing them. Hardware, query plans and data distributions affect the result. Use the recorded 5 December run to inspect the check list, environment and raw timings, and the bundle manifest to verify file hashes.
For the broader product architecture, see our multi-tenant SaaS development guide.
Frequently asked questions
Is a separate database required for every customer?
Not always. Shared and dedicated designs have different trade-offs. Whichever you choose, enforce and test the tenant boundary on every access path.
Can a system prompt prevent cross-customer data leaks?
No. Enforce permissions in the services and data layers that supply context and run tools. A prompt can guide the model's behaviour, but access decisions belong in code.
Does an AI feature need a separate security review?
Review the new data and action paths it adds, even if the main application has already been reviewed. Retrieval, memory, integrations and background jobs can change who can reach what.
Review the paths before adding more capabilities
Before giving an assistant more access, list the accounts, resources and operations involved. A focused discovery phase can settle the open architecture questions; then agree specific isolation tests as release criteria. EmphaSoft builds SaaS platforms and AI features. To discuss an existing architecture or a planned release, tell us about your product.
A tenant-access example with a measured query check
The anonymised Supabase sample separates permission tests from query performance. Its published synthetic comparison includes the dataset, visible-row checks, timing samples and environment, so the scope of the result can be inspected.