Identity by key
How API keys carry tenancy in Brain — a key is bound to a (namespace, space, permissions) scope at creation, and the server derives every request's scope from it.
This is the identity wedge: identity by key. Competing memory APIs ask the caller to send tenancy (user, agent, namespace) on every request, which puts correctness on every line of integration code. Brain flips that — the moment a key is minted it records which namespace (the company / tenant) it operates on, and the server refuses to honour any field that disagrees. Every memory, entity, statement, and index is isolated per (namespace, space); no key can reach another tenant's data.
The key carries the namespace. The space may be per request.
There are two axes here and they are carried differently, which is the single most important thing on this page.
The namespace always comes from the credential, resolved at authentication, and nothing in a request can widen a read past it. A key may also pin a space, and for a single-tenant app that is the simplest arrangement — one key, one space, nothing to pass.
But a space is the per-end-user isolation unit, designed for up to a
million per organisation, and at that cardinality it is selected per request
through act_as rather than baked into a credential. Minting a key per end
user is the documented anti-pattern: per-end-user isolation belongs in the
storage-key prefix, not in your key-management system. See
Namespaces, spaces and sessions for the full model.
The practical effect: a client is constructed with just a key. encode, recall, and forget take no scope — the credential fixes the tenant, and no verb carries tenancy. This holds on both surfaces: the low-level wire client (BrainClient, straight to brain-server) and the HTTP client (BrainHttpClient, or plain curl, against the brain-edge gateway).
// Wire — BrainClient straight to brain-server
import { BrainClient } from "@brain-db/sdk";
const client = await BrainClient.connect("127.0.0.1", 9090, {
auth: { kind: "token", token: new TextEncoder().encode(process.env.BRAIN_API_KEY!) },
});
// no namespace, no space, no scope — the key carries all three// HTTP — BrainHttpClient against the brain-edge gateway
import { BrainHttpClient } from "@brain-db/sdk";
const brain = new BrainHttpClient({
apiKey: process.env.BRAIN_API_KEY!, // a `brain_…` key
baseUrl: process.env.BRAIN_BASE_URL, // unset → http://127.0.0.1:8080
});
// still no namespace, no space, no scope — the key carries all threeThe rest of this page is the mechanism: how the scope is built, what it contains, what the handshake hands back, and the two patterns that scale.
How the server derives scope
Authentication runs at the handshake, before any verb. It parses the credential, looks up the bound key, constructs the caller's (namespace, space, permissions) scope, and binds it to the connection — handlers read the scope from there, never from the request body.
You can introspect the bound identity any time. On the wire client it comes straight off the handshake — client.namespace, client.spaceId, and client.session (which carries permissions). Over HTTP, GET /v1/whoami returns the same resolved namespace, space_id, and permissions. Either way it costs nothing beyond the call itself.
Authentication
Over the HTTP edge, send the key on either header — they are equivalent:
Authorization: Bearer <brain_key>X-API-Key: <brain_key>Authentication happens once, before any verb, and both surfaces fail closed: a bad credential is an authentication error, and a valid key missing the required capability bit is an authorization error raised before the handler runs rather than after the work is done.
Over the wire protocol, auth is sent after the WELCOME handshake as a discriminated Auth value — either a token (your brain_-prefixed key) or an mTLS assertion, after which clients can call GET_CAPABILITIES to introspect the live shard capabilities.
Both surfaces agree on the rest: the credential is the whole identity; there is nothing else to configure. There is no namespace header/field and no space header/field — sending tenancy alongside a scoped key is not a supported input, because the key already fixes it. Bad credentials return an authentication error; a key without the right permission bit returns an authorization error before the handler runs. See API: authentication and Wire client.
What the scope actually contains
The binding the server constructs at authentication has three authoritative claims and one that deliberately isn't:
| Claim | Authoritative? | Role |
|---|---|---|
namespace | yes | The tenant data boundary. Resolved once at auth to its interned id |
space | yes | The isolation unit — one end user, or one purpose |
permissions | yes | The capability bits that gate every verb |
user | no | A human or service-account tag stamped onto audit rows |
That last row earns its place by being explicitly not a boundary. The user
tag answers "who did it" on an audit row and nothing else — it never gates
access and never narrows a scope. If you are tempted to use it as a filter,
you want a space.
Fail-closed, with no default
Authentication is mandatory, so the scope is always present on an
established connection and always key-derived. There is no anonymous
connection lacking one and no default to fall back to: a key resolving to no
provisioned namespace is rejected as PermissionDenied rather than quietly
landing somewhere plausible.
The absolute version of the guarantee: operations that legitimately span spaces within one namespace need an admin key with explicit permissions — and no key of any kind can act across namespaces. Cross-tenant isolation is not a permission you can be granted.
What the handshake hands back
AUTH_OK carries more than a yes. Two fields are worth using:
bound_shard_id— which shard this connection's scope routes to, so a client can keep its connection pool aligned with the shard topology instead of guessing.server_time_unix_nanos— the server's clock. Compare it with your own and warn past about a second of skew. Several things here are time-sensitive — validity intervals,occurred_at, idempotency horizons — and a client with a drifting clock produces data that looks fine and sorts wrong.
Permissions
The bound-permissions set gates encode / recall / admin capabilities at the auth layer, before the handler runs. A key minted without the recall capability calling recall is rejected without touching the read pipeline. Permissions are fixed at key creation — to change them, mint a new key with the desired scope and rotate.
Checking at the auth layer rather than inside each handler is deliberate: an unauthorised call costs a rejection, not a retrieval that is then discarded, and there is no handler that could forget to check.
The database never holds your secret
Brain stores only BLAKE3(secret) for a minted key. The secret itself exists
once, at mint time, and is never persisted — so a compromised metadata store
or a leaked backup does not yield usable credentials.
Keys minted by Brain also cannot spend anything: they authenticate a connection and nothing more. The credential that pays for LLM work is a separate, namespace-scoped grant resolved from the broker, and no provider key ever reaches Brain at all.
What this prevents
The failure mode this design closes is worth naming, because it is a real class of bug rather than a hypothetical.
With identity carried in the request, a client that constructs the wrong
space_id or namespace — a template bug, an off-by-one in a loop, a stale
variable in a retry path — writes into another tenant's space. Nothing rejects
it, because the request is well-formed and the server has no second source of
truth to check it against. The data lands, and you find out when someone sees
something they shouldn't.
With identity bound to the credential, the same buggy request is rejected at the handshake. There is no field to get wrong, because tenancy is not an input. That is the whole argument, and it is why the SDKs expose no scope argument to pass — the absence is the feature.
The corollary catches people migrating from other memory APIs: there is no
namespace header and no space header. Sending tenancy alongside a scoped
key is not "belt and braces", it is an unsupported input. If you find
yourself wanting to pass tenancy, the question to ask is which key you should
be holding, or whether you want act_as.
Namespace = company, space = app
The two identity axes are deliberately coarse:
- Namespace is the tenant boundary — a company or customer. It owns memories, entities, the typed graph, and its schema declarations. You create a namespace per product or per customer tier, not per end-user.
- Space is the application acting inside a namespace. Two spaces in one namespace are isolated from each other: one space's recall never sees the other's memories.
Every row, index, and resolver lookup is keyed under (namespace, space). See Namespaces for when to draw a new boundary.
Acting on behalf of a tenant — act_as
A trusted service principal (a gateway, a backend serving many tenants from one connection) can run an operation on behalf of a tenant space by carrying an act_as field. When it's set, the effective identity — the (namespace, space) named in act_as — is what scopes everything: shard routing, the idempotency key, and the isolation boundary for both reads and writes. A recall under act_as returns the effective identity's memories only, never the service principal's own and never another tenant's.
act_as is a privilege, not a field anyone can set. It is backed by a
dedicated permission bit on the minted key, granted only at key-mint over the
admin surface, and it comes with a may_act allowlist bounding which
namespaces the principal may act for. Naming a namespace outside that list is
hard-rejected as ActAsDenied — never silently downgraded to the connection's
own identity, which would be the dangerous failure. No data-plane operation can
mint or elevate the bit, so a compromised tenant key cannot grow into a gateway
key.
An ordinary tenant key never carries it, and that asymmetry is the point:
can_act_as does not widen what the connection's own space may do. It only
authorises running an operation as someone else, within an allowlist fixed
when the key was created.
There is no re-authentication
A connection authenticates once, for its whole life. Re-AUTH mid-connection
is unsupported and not planned — act_as is the stateless per-request
alternative, one auth with the identity varying per operation.
That is what lets a single shared connection pool serve every tenant without pinning a connection (and a credential) per tenant, and without a gateway having to forward each tenant's raw secret downstream.
Why the effective identity keys idempotency
Keying idempotency (and routing) on the effective (namespace, space) + RequestId is a safety rule, not an optimization. One service principal issuing ops for many tenants over a shared connection could otherwise reuse a RequestId across tenants and have a bare-RequestId key collide — serving one tenant's cached ack to another. Scoping the key by the effective identity makes that collision impossible.
Nothing about this is specific to the wire protocol, either. The HTTP edge derives the same scope from the same key material, so a service that starts on curl and moves to the wire client keeps identical isolation semantics — there is no second authorization model to reason about per transport.
Introspecting what you hold
Because the scope is server-derived, the way to find out what a key can do is
to ask. A whoami-style probe returns the resolved namespace and space, and
GET_CAPABILITIES reports what the shard itself has loaded — including
rerank: false on a stock deployment.
Worth wiring into your startup path rather than your debugging session: a key pointed at the wrong namespace behaves perfectly and writes everything somewhere unexpected, and the only cheap moment to catch that is before the first write.
Multi-tenant patterns
One key per tenant app. Mint a key bound to a customer's (namespace, space) and let that customer's traffic flow through it. Cleanest isolation; you manage a key per tenant and rotate them.
One gateway key + act_as. For a backend serving many tenants from one process, hold a trusted service key and set act_as per request to pin the effective identity. Routing, idempotency, and isolation all follow the effective identity. This is the right pattern when one process fronts many tenants.
One gateway key + a space per end user. The pattern at scale, and the one the tenancy model is designed around: a single trusted service key, one namespace for your product, and a space per end user selected per request. A million users cost a million index prefixes and one credential — rather than a million credentials to rotate, revoke and audit.
What you should never do is share one key across tenants with no act_as —
provenance and isolation collapse to a single principal and you lose the
ability to reason about one tenant's data in isolation. Every audit row says
"the gateway did it", which is true and useless.
Choosing between them
The second row is why the first pattern stops scaling: it is fine at ten tenants and a liability at ten thousand. The third is why the allowlist exists and why it should contain the minimum set rather than every namespace you own.
Was this page helpful?
Entities
The entity graph backing Brain — canonical nouns with a stable EntityId, resolved per tenant, that statements are about and relations connect.
Namespaces, spaces and sessions
Two isolation axes and one grouping — a namespace is the tenant wall, a space is the isolation unit that scales to a million per org, and a session is explicitly not a boundary.