Multi-tenancy for Cortex Agents¶
Multi-tenancy enables a single Cortex Agent to serve multiple tenants while enforcing data
isolation between them. You achieve tenant isolation by configuring session attributes in the
variables block in the agent:run API and pairing them with row access policies (RAP) on
your Snowflake tables. Before the agent executes any generated SQL, Snowflake sets the session
attributes you provide, and the row access policy filters rows based on those values.
This approach follows a shared responsibility model. Snowflake provides the tools — session attributes and row access policies — and you are responsible for configuring them correctly to enforce your tenant boundaries.
How multi-tenancy works¶
When you call the agent:run API, you can include a variables block that defines one or
more attributes and variables. The row access policy on your table can be set up to reference
these attributes to filter data for the current tenant.
The execution flow is:
You call an
agent:runAPI with avariablesblock that includes tenant-specific values.Before executing any generated SQL, the attributes are set on the Snowflake session.
The generated SQL runs in the session where the variables are active.
The row access policy on the table evaluates the session attributes and filters rows accordingly.
Mutable vs. immutable session variables¶
The variables block supports both session attributes and session variables.
Session attributes: Session attributes are immutable and configured using
is_immutable_session_attribute: true. Once the value of an attribute is set, it cannot be modified by generated SQL or tool execution during the session.Mutable session variables: Session variables are mutable and configured using
is_session_variable: true. The value of this variable could be overwritten by other SQL in the session.
Warning
Mutable session variables can be overwritten by generated SQL, code execution in the sandbox, or custom tools. If you have to use mutable session variables for row access policies, disable the code sandbox and remove or restrict any custom tools to prevent unintended modification.
Snowflake strongly recommends using immutable session attributes (is_immutable_session_attribute: true)
for row access policies. This ensures the tenant context can’t be altered during the agent’s execution.
To configure the properties in the variables block in the agent:run API, use the following
parameters:
Property |
Type |
Description |
|---|---|---|
|
Boolean |
Sets the variable in the SQL execution environment. |
|
Boolean |
Sets the variable as an immutable session attribute. Snowflake recommends this for row access policies. |
A single variable can have multiple properties enabled. For example, a username variable can be both a session variable and a session attribute.
Configure variables in agent:run¶
Pass variables in the agent:run API to set tenant context for each invocation:
In this example:
regionis set as an immutable session attribute. The key isregionand the value isNORTH. The value is set before query execution, so the row access policy can reference it.productis set as a session variable. The key isproductand the value isacme. The value is set before query execution, so the row access policy can reference it.
The session variable and session attribute persist for the duration of the interaction. If you set
the same variable again in a subsequent agent:run call, the new value overwrites the previous
one. Session attributes cannot be overwritten.
Row access policies with session attributes¶
To enforce tenant isolation, Snowflake strongly recommends using a session attribute in a row access policy. The policy filters rows so each tenant sees only their own data.
Create a row access policy
The following example creates a row access policy that filters rows based on the region
session attribute:
Apply the policy to your table:
When the agent executes a query against the sales table, the row access policy evaluates the
region session attribute and returns only the rows where the region column matches the
value set in the agent:run call.
Best practices¶
Follow these recommendations when configuring multi-tenancy with Cortex Agents:
Use immutable session attributes for row access policies. Set
is_immutable_session_attribute: truefor any variable referenced by a row access policy. Immutable attributes can’t be modified by generated SQL or tool execution.Disable the code sandbox when using mutable session variables. If you can’t use immutable session attributes and must use mutable session variables, disable the code sandbox to prevent code execution from overwriting session variables.
Restrict custom tools. If you can’t use immutable session attributes and must use mutable session variables, limit custom tools since these could execute SQL and modify session variables.
Scope variables to the narrowest level. Set only the variables each invocation requires. Don’t pass variables that the agent doesn’t need for the current request.
Test your row access policies independently. Verify that your row access policies filter correctly by testing them with
SETstatements outside the agent before deploying.
Access control¶
The following table describes the privileges required for multi-tenancy configuration:
Privilege |
Object |
Required for |
|---|---|---|
MODIFY |
Agent |
Configuring agent instructions with prompt variables |
USAGE |
Agent |
Invoking the agent with session variables |
CREATE ROW ACCESS POLICY |
Schema |
Creating row access policies |
ADD ROW ACCESS POLICY |
Table |
Applying a row access policy to a table |
OWNERSHIP |
Agent |
Full control over the agent configuration |
Limitations¶
The following limitations apply to multi-tenancy with Cortex Agents:
Variable overwrite: Mutable session variables (set with
SETstatements) can be overwritten by generated SQL, code sandbox execution, or custom tools. Use immutable session attributes for row access policies.Session scope: Session variables persist for the duration of the interaction. A subsequent
agent:runcall with the same variable name overwrites the previous value.
Snowflake provides session variables and row access policies as tools for tenant isolation. You are responsible for configuring these correctly, choosing immutable attributes for sensitive policies, and restricting tools that could modify session state.