Multi-tenancy
Konstera is multi-tenant by default. Every piece of tenant data — invocations, state, vectors, credentials, approvals, cost events — is scoped so an agent instance can only read/write its own tenant's rows.
You don't configure this. The kernel enforces it. This page covers how it enforces it so agent authors know what invariants they can rely on.
The invariant#
Every tenant-scoped DB write and read sets the
app.tenant_idPostgreSQL session GUC viaSET LOCAL. Row-Level Security policies enforcetenant_id = current_setting('app.tenant_id')::uuid.
Translation:
- The agent's SDK calls the kernel via MCP
- The kernel resolves
tenant_idfrom the caller'sinstance_id(viatenant_agent_instancestable) - Before any DB query, the kernel sets
app.tenant_idon the connection - PostgreSQL's RLS layer filters every row by that GUC
- The application DB role is
konstera_appwithNOBYPASSRLS— so even a buggy query cannot leak across tenants
A buggy agent that accidentally sends another tenant's UUID in a request would just get a "not found" response — the row exists, but RLS hides it.
The ambient tenant scope#
Server-side, every request handler enters an AmbientTenantScope block
that pins the tenant for the duration of the request. Nested calls
(including Temporal activities) inherit it:
// Konstera kernel — simplified
using var scope = AmbientTenantScope.Enter(resolvedTenantId);
await _dbContext.Database.ExecuteSqlRawAsync(
"SET LOCAL app.tenant_id = {0}", scope.TenantId
);
// ... any DB query here is tenant-scoped
The SET LOCAL is transaction-scoped. If a Temporal activity opens its
own transaction, the kernel re-enters the scope + re-sets the GUC.
X-Konstera-Tenant header#
For HTTP requests that cross tenant boundaries (admin UIs, inbound webhooks
not tied to an agent instance), the request carries an
X-Konstera-Tenant: <uuid> header. The kernel validates the header
against the authenticated user's grants + sets the ambient scope.
Agents don't send this header — the agent's identity (the instance ID) already tells the kernel which tenant.
SoD (Segregation of Duties)#
Beyond tenant isolation, M8 added role-level SoD inside a tenant:
kernel_admincan set the ambient tenant on any requesttenant_usercan only operate on their own tenantagent_runtimecan only operate on invocations their granted agents initiated- A submitter of an approval cannot be the approver — enforced at the DB level, not just the application layer
See the human-in-the-loop recipe for
how SoD shows up in agent code (via submitter_role on
ctx.human.request).
What's tenant-scoped#
| Resource | Table | Agent surface |
|---|---|---|
| Agent instances | tenant_agent_instances | ctx.agent_id, ctx.instance_id |
| State | instance_state | ctx.state.* |
| Vectors | instance_vectors | ctx.vector.* |
| Credentials | credentials | ctx.credentials.get() |
| Invocations | invocations + invocation_logs | automatic |
| Approvals | approvals | ctx.human.* |
| Cost events | usage_events | automatic + ctx.cost.record() |
| Rules | rules | ctx.rule.* |
| Context | tenant_context_term/entity/skill | ctx.semantic.* |
What's NOT tenant-scoped#
Shared across all tenants (same bytes for everyone):
- Agent package images — one
konstera/customer-service-agent:0.9.2image across tenants; instance config varies per tenant - Capability catalogue — the canonical capability names + schemas
- Standard Context Pack — the 24+24+2 pack ships to every tenant (tenant can author overrides, those are scoped)
These live in BYPASSRLS tables accessed via the konstera_admin role.
What the agent sees#
From the agent's perspective, multi-tenancy is invisible. The code:
async def handle(self, req, ctx):
cursor = await ctx.state.get("last_processed_id") # scoped to this tenant
orders = await ctx.capability.call("orders.lookup_by_email", email=req.input)
# these orders belong to this tenant's Shopify connection
await ctx.vector.upsert(
namespace="shared:policies", # 'shared:' = all this tenant's agents
content="...",
)
...runs on tenant A and tenant B with identical semantics. The agent reads/writes its tenant's data; no code change needed.
Cross-tenant admin ops (operator surface)#
When an operator needs to run cross-tenant queries (platform health, cost
rollups), the kernel uses a separate connection with konstera_admin
(BYPASSRLS). These ops are:
- Admin-only HTTP endpoints under
/api/admin/* - Behind IAM checks + a separate audit log (
admin_actions) - Not reachable from agent code
Agent authors can safely treat multi-tenancy as a black box.
Debugging multi-tenancy#
If an agent gets unexpected "no results" and you suspect tenant scoping, check the invocation trace in the portal — the trace shows:
- The resolved
tenant_idfor this invocation - The
app.tenant_idGUC set on each DB call - The RLS policy that filtered each query
Mismatches here are a kernel-side bug; file an issue.
See also#
- Why Temporal — how tenant scope persists across workflow retries
- Kernel MCP services — every service is tenant-scoped
- Human-in-the-loop recipe — SoD in action