Multi-tenancy for AI agents: tenant sessions and isolation
The backend validates which customer uses the copilot. Tenant sessions, subtenants, and scoped memory so SaaS accounts do not mix.

The backend validates which customer is using the copilot. Tenant sessions, subtenants, and memory scope keep a SaaS's accounts separate.
A SaaS can use the same agent for several customers. The problem appears when the browser, or the model itself, can decide on its own which organization it is acting for. Then you can end up with other customers' conversations, another customer's memory, or usage attributed to the wrong account. This article explains how to avoid that mixing.
If you are looking for who the agent is and what specific permissions it has over an API, that is covered in identity, permissions, and limits. If you are looking for what working memory is versus state, we cover it in agent memory. Here the focus is how the runtime knows which customer each conversation, each memory, and each tool call belongs to.
The typical failure when the tenant travels as a parameter
Imagine a copilot widget in your product. The page carries an API key and each call includes a tenantId. Both values are visible to anyone using the page. If the API trusts the identifier it receives to grant access, it can hand over conversations, usage data, or memory from another account. This is an object-level authorization failure.
There is a related risk when the model can choose the account when invoking a tool. A support agent only needs to act within the organization that opened the chat. If the tool gives it access to all accounts, its scope exceeds that of the task. OWASP GenAI LLM Top 10
The design rule is simple. Your backend authenticates the person and checks which organization they have access to. From that decision, the runtime receives the identity it must use. If the person belongs to several organizations, they can select one in the interface, and the backend validates their membership before opening the session.
Tenant and subtenant as isolation levels
In a B2B product, "the company" is almost never enough. Within Acme there are teams, offices, or users. If all memory is stored at the company level, Sales can read what Operations left behind. If each user only accesses their private memory, the assistant needs another way to retrieve what they share within Acme.
Level | What it represents | Example |
|---|---|---|
Tenant | Your SaaS's customer, normally an organization |
|
Subtenant | A person, a team, or a workspace within that customer |
|
Decide the granularity at which you need to separate conversations, usage, connected applications, and memory. You can have private conversations per user and a shared budget for the whole organization. This separation applies to system resources, including the tools and data the agent queries. AWS guide on multi-tenant agents
How a tenant session is created
A tenant session is a short-lived signed token that fixes the tenant and, when applicable, the subtenant. The browser presents that token in subsequent calls. The service that receives it validates its signature and expiration and takes the effective identity from it.
The person signs in to your product.
Your backend identifies the user and validates their access to the active organization.
The backend requests a session token for that identity.
The widget uses the token in its calls. If it needs to renew it, it goes back through your backend and the access check.
The credential capable of issuing sessions stays on the server. The frontend only needs the temporary token. If the user loses access to an organization, the next renewal must fail.
What permissions a tenant session has
The token fixes who the call is made for. The service still has to decide which operations that session can perform. A support copilot may need to start chats, view their history, upload attachments, or show the customer's usage. Managing projects, changing plans, and creating agents are operations at a different level.
The same logic applies to tools. If the assistant looks up the status of a return, the SaaS API must validate the credential with which it receives the call and check that the return belongs to the authorized tenant. The copilot's session and the authorization over the return are separate controls. Identity, permissions, and limits of agents
Shared and private memory across conversations
Memory across sessions is where a mix-up hurts most. If an Acme summary ends up in Globex's store, the model can retrieve it in another conversation. The separation must be applied both when saving and when retrieving data.
Core memory contains information the assistant uses routinely, such as the user's profile, and is added to the prompt on every turn when enabled. Long-term memory keeps facts that are retrieved when they are relevant. Both can contain customer information. If you want to go deeper into the difference between memory, context, and state, see types of memory.
The bucket is the store where that memory lives. Its scope determines who shares it. Sales and Operations can read the same Acme policy and, at the same time, save their own memories in different buckets. If you enable shared reads, also decide who can write to the common bucket. Globex must keep resolving its reads and writes within its own scopes.
How to implement multi-tenancy in your own backend
Imagine a support copilot that serves Acme and Globex. Both companies may have an order with the same number. Chats and memory are private to each user within their company. The support team can look up their organization's orders. In this example, Express receives the request, PostgreSQL stores the data, and runAssistant stands for your function that calls the model and runs its tools. If an identifier is only unique within an organization, include the tenant in the table key. For orders, use PRIMARY KEY (tenant_id, id).
1. Validate the user in the requested organization
The URL indicates which organization the user wants to open. requireLogin identifies the user from your application's session. The query to memberships checks that they are still authorized to work in that organization. With the result, the backend fixes the context that the whole execution will use.
2. Apply the same context to chat, memory, and tools
Looking up a chat requires its ID, the organization, and the user to match. Memory is queried with the same pair of identifiers. For the orders tool, the model only supplies the order number. The server function already carries the authorized tenant and is only offered to users with the support role.
In this example, runAssistant returns text and receives the tools as server functions. If your application decides to save a fact, it calls saveMemory with the same authorized context. For memory shared across the whole company, use a store separated by tenantId or an explicit scope. If you retrieve memories through vector search, apply the tenant and user filter in the query to the store. Parameterized queries in node-postgres
3. Preserve identity if the place of execution changes
If the browser calls your backend and the backend runs the agent, you can use your SaaS's session and pass the context inside the server. If the browser calls a separate runtime directly, your backend can issue a short-lived token for that service after checking access. The runtime validates signature, issuer, audience, and expiration before using the token's tenant and user. If the agent works from a queue, store the context when creating the job and check the permissions that must still be valid before running actions. AWS guide on identity and tenant context in agentic systems · JWT best practices
A concrete integration test. Create order ORD-42 with different statuses in Acme and Globex. The copilot must return the status from the active organization in each case. Also create two chats under Acme users and verify that each one only retrieves its own history and memory. Test the behavior after revoking access to the organization.
Checklist before opening the copilot to customers
Question | What to check |
|---|---|
Who validates the active tenant? | Your backend checks the user's membership in the organization before requesting the session. |
What credential does the browser receive? | A temporary token with the identity fixed. The issuing key stays on the server. |
Who can access each conversation and memory? | Test with two organizations and with two users of the same organization. |
Who authorizes tool calls? | The target API applies permissions to the requested resource and operation. |
Can you delete the memory of a single customer? | Check that deletion affects the selected scope. |
Who is usage attributed to? | Usage appears under the correct tenant. If you have configured quotas, check what happens when they are reached. |
It is the same leap as going from demo to production. A demo with a single account can show that the copilot works. To prove isolation you need at least two.
How Devic handles it in practice
With a single Devic account you can serve several customers with the same assistant. The first message or execution that carries the tenant's identity registers it automatically. From there, Devic attributes its conversations, cost, and subtenants. The attribution applies to traffic identified from that point on. Devic multi-tenant guide
Issuing and renewing sessions
Configure a server API key in signed mode so that its direct use is limited to issuing sessions. Store it on the backend and use a different key for administrative operations. Devic rejects direct issuance if the request carries an Origin header or if the key is configured for browser domains. A session token also cannot issue another one. API keys
With the server SDK, your endpoint authenticates the user and checks their membership in the organization before requesting the token. getActiveMembership stands for that check in your application.
The same request over HTTP, always from the server, uses this route. On this endpoint the /api prefix is mandatory.
In React, getTenantSession from @devicai/ui calls your authenticated endpoint. If you use the Express example, configure it to make a POST. The provider can renew the token before it expires. The default duration is one hour, with a range from 60 seconds to 12 hours. If you disable the issuing key, its sessions are revoked within seconds. Tenant sessions
The session has a closed list of operations. Depending on the credential's permissions, the user can converse, view their chats, follow an execution, upload attachments, view their usage, and manage their connected applications. They can also view or edit the enabled core memory of the bucket that corresponds to them. Administration of assistants, agents, projects, tool servers, tenants, and plans is outside that list. Tenant sessions · Core memory
Bucket configuration
| What memory it shares |
|---|---|
| One memory per tenant and subtenant combination. |
| One memory per organization, shared among its subtenants. |
| One memory for the whole assistant, shared across its conversations. |
| One memory common to the account's assistants configured with that scope. |
owner decides which family the bucket belongs to. self uses the assistant's own, and project allows sharing it across a project while keeping its partition. user creates one memory per person that can follow them across tenants. If you set the bucket explicitly, reads and writes go to that bucket and the configured scope no longer takes part in resolution.
For an embedded SaaS with private memory per person, start with scope: subtenant and owner: self and require both identifiers. If the subtenant is missing, Devic uses the tenant's bucket. If the tenant is also missing, it uses the assistant's. With inherit shared you can enable reads from a higher scope and keep writes in the private bucket. Memory deletion can be limited to a tenant, subtenant, session, or entity. Memory with buckets and scope
Usage limits and tools
You can give Acme a shared monthly budget and also limit tokens per user. Devic applies rules by cost or tokens, with configurable windows and scope by tenant or subtenant. When a limit is reached, it returns 429 before calling the model. If the counter store fails, the request continues. This mechanism manages consumption. Usage limits and plans
When connecting an MCP to your SaaS's API, configure the credential that API expects. For example, the MCP can use a JWT resolved dynamically for the customer. The session identifies the user and the tenant to Devic. The SaaS API validates the tool's credential and authorizes access to the resource. MCP configuration
If you want to try this pattern in your copilot, you can start with the Devic product and follow the multi-tenant guide.
Turn your SaaS AI-Native
Get a free trial just by signing up
Latest posts





