# Tenancy

Run several customers or business units on one instance with server-enforced separation, per-tenant settings and optional per-tenant keys.

## How separation works

- A tenant owns **sources**. A ticket belongs to the tenant of the source it arrived through. A ticket filed in the console belongs to the caller's selected tenant.
- Users are granted specific tenants, or `*`. Owners see every tenant.
- Reads and writes are scoped by tenant on the server, over one shared database. Cedar policies can also test the tenant.
- Budgets, the semantic cache, the learned taxonomy, retention windows and data residency can differ per tenant.
- On a multi-tenant instance, channels with no signed-in person (Teams, the answer API) must be bound to one tenant, and refuse to answer until they are.

## Create a tenant

1. Open **Tenants** and add one (`PUT /api/tenants/{tenant_id}`).

   | Field | Notes |
   | --- | --- |
   | name | Display name |
   | about | Description |
   | region | A free-text note of where its data lives. It is not enforced |
   | residency_region | Enforced: an ISO code such as `DE`, or `EU`. Model calls and embeddings for the tenant stay in that geography or are refused |
   | no_cross_geo | Also refuse cross-region routing inside the geography |
   | environment | For example `production` (the default) or `test` |
   | retention | Per-store retention windows for this tenant |
   | sources | Source ids this tenant owns |
   | owns_unsourced | Whether tickets with no source land here |

2. Bind other resources, such as agents or triggers, with `PUT /api/tenants/bindings/{kind}/{name}`.
3. Grant users with `PUT /api/tenants/grants/{username}`.
4. Users switch tenant from the console header (`POST /api/tenants/select/{id}`).

## Isolation tiers

Each tenant has an isolation tier. Every tenant starts on **shared**, and nothing changes for it until you move it.

| Tier | Where the tenant's rows live | Cost |
| --- | --- | --- |
| `shared` (default) | The instance's tables, beside other tenants', kept apart by the tenant column and row-level security | None |
| `schema` | A Postgres schema of the tenant's own (`t_<tenant>_<hash>`) in the same database. Connections working for the tenant read and write it; no other tenant's connection can see it | None: same database |
| `dedicated` | A database of its own, served by a separate Liyagent instance (a cell). This instance sends the tenant's requests there with `421` and the cell's address | Only once you provision the cell |

The tier covers every table with a tenant column that row-level security covers. Instance-wide settings and accounts stay in the shared tables.

### Move a tenant

1. Check what would move: `GET /api/admin/platform/tenant-tiers/{tenant}/plan` lists the tenant's row count per table.
2. Move it: `POST /api/admin/platform/tenant-tiers/{tenant}/migrate` with `{"to": "schema"}`. The rows move in one transaction with the tier change, so a move happens whole or not at all.
3. For `dedicated`, first create the cell's database and set an environment variable named `TICKETIQ_CELL_<NAME>` to its URL on this instance, then send `{"to": "dedicated", "target_db_env": "TICKETIQ_CELL_<NAME>", "endpoint": "https://<cell URL>"}`. The rows are copied, counted on the cell, and only then deleted here. The database URL is never sent in a request.

Moving to a less isolated tier (`schema` or `dedicated` back to `shared`) loosens isolation, so it needs `governance.weaken` and a reason. Move between `schema` and `dedicated` through `shared`.

A request another replica handled in the seconds before it saw the new tier can leave a row in the shared tables. `POST /api/admin/platform/tenant-tiers/{tenant}/drain` moves any such rows into the tenant's schema, and every start of the instance does the same, adding any table or column an upgrade brought.

> [!NOTE]
> Instance-wide background jobs read the shared tables. A job that should cover a schema-tier tenant runs for that tenant. Keep a tenant on `shared` if it relies on instance-wide sweeps over its tenant-column tables.

Every move writes `tenant.tier.migrate` (or `tenant.tier.migrate.failed`), and a drain writes `tenant.tier.drain`.

### Your default tenant

A person with no saved tenant choice lands in the instance's own home (the tenant the legacy, unscoped records were adopted into) when they can see it. Otherwise they land in the first tenant they can see whose environment is not a rehearsal one (`demo`, `sandbox`, `test`, `staging`, `dev`, `qa`, `trial`), not in whichever tenant sorts first alphabetically.

## Retire a tenant

`POST /api/tenants/{tenant_id}/retire` retires a tenant by crypto-shredding: it writes an erasure receipt, deletes the tenant, destroys its data keys (making everything sealed under them unreadable, in backups too) and removes its blobs. It needs `access.write` and `admin.write`, access to every tenant (`*`), a reason and the tenant id repeated as confirmation. It is refused unless per-tenant keys (`TICKETIQ_TENANT_KEYS`) and content sealing (`TICKETIQ_SEAL_CONTENT`) are both on and nothing the tenant owns is left outside its key, and while a legal hold covers the tenant. Each retirement's receipt is listed at `GET /api/tenants/retirements`.

> [!WARNING]
> Retirement can't be undone. Export what you need first.

## Next steps

- [Identity and roles](/docs/govern/identity-roles)
