Brain Docs
Build

Wire it in

Drop it into an existing loopA chatbot that remembersHandling corrections

Production

Alongside your RAGOne space per customerErasing a subject

Working with the API

Error handlingRetries and cancellationIdempotencyStreamingGraph queriesFaithfulness checkingIdentity walkthroughNamespace lifecycle

Reference

Brain/Docs
✦ ASKBrain on GitHubDashboard
BuildOne space per customer

One space per customer

Namespace is the hard boundary, space is the isolation unit, and a trusted backend selects a tenant's space with act_as — a wire-tier capability the HTTP API does not have.

PURPOSE

Serving many customers from one agent, with isolation that holds at the retrieval boundary rather than in a filter you have to remember to apply.

Two levels, different jobs

Namespace is the hard boundary. A recall can widen to span every space in the caller's namespace, but it never crosses namespaces — there is no scope, flag or parameter that widens past it.

Space is the isolation unit inside a namespace. One per customer, per end-user, per workspace — whatever your tenant is.

A connection's identity resolves to (namespace, space, permissions) from the key itself. You can see what yours is bound to:

curl -s "$BRAIN_BASE_URL/v1/whoami" -H "Authorization: Bearer $BRAIN_API_KEY"
{ "namespace": "acme", "space_id": "…", "permissions": { … } }

Pattern A — a key per tenant

Issue each customer their own key. Isolation is then structural and needs no application code: a key simply cannot address another tenant's space.

Right when tenants are few, long-lived, or hold their own credentials. Wrong at scale — you're now running key lifecycle for every signup.

Pattern B — one principal, act_as

The usual production shape. Your backend holds one key and names the effective tenant per request.

act_as is a wire-tier capability

There is no act_as on the HTTP API. Every /v1/* route takes its space from the credential, and no route accepts a selector. Pattern B therefore needs the native wire client — BrainClient in any of the three SDKs — or a hosted gateway whose own resolver does the selection for you from your session. Reaching for this pattern over plain HTTP is the mistake this warning exists to prevent.

Every tenant-scoped wire request carries an optional selector:

import { EncodeBuilder } from "@brain-db/sdk";

const req = new EncodeBuilder(text)
  .actAs("acme", `support-bot:${session.userId}`)   // ← from the SESSION
  .build();

await client.encode(req);
from brain_db_sdk import EncodeBuilder

req = EncodeBuilder(text).act_as("acme", f"support-bot:{session.user_id}").build()
client.encode(req)

Hand-built requests set the field directly — { namespace, space, grant? }.

Three properties make this safe, and they're worth being precise about:

  • The space value is an opaque selector, not an id. The server derives the 16-byte storage space id from it at ingress. Client code never constructs a space id, so it cannot forge one by guessing.
  • The namespace must be within the principal's allowlist. Outside it, the request is refused with ActAsDenied — it does not silently fall back to the key's own space.
  • Extra capability bits are grantable only if the connecting principal already holds them. act_as cannot escalate.

An empty space selects the connection's own key-bound space, so the same code path works for both single- and multi-tenant callers.

One connection serves every tenant, because the wire protocol multiplexes: you hold a small pool and vary the selector per request rather than opening a connection per tenant.

The selector must be derived from your authenticated session, never from anything the user supplies. space: "support-bot:" + req.body.userId is a tenant-crossing bug with extra steps. Take the id from the verified session and nowhere else.

Scope: isolation, not filtering

recall runs against the caller's space by default. Widen it to the namespace only when a query genuinely spans tenants — an internal analytics view, say.

The distinction that matters: results outside scope aren't removed from the answer, they were never candidates. Post-filtering would mean the data had already crossed into process memory, and your retrieval budget had already been spent on rows you then discard.

Many subjects in one space

Sometimes the tenant isn't the subject — one workspace, several people in it. Pass subject on recall to retrieve claims about a particular person without splitting the space:

await brain.recall({ query: userMessage, subject: "priya", maxResults: 5 });

Use a space when the data must be isolated. Use a subject when it's shared context and you only want one person's claims this turn.

Choosing

Use a space per…
When
Customer / organisation
The common case
Data must never mix, and erasure is per-customer
End user
Consumer products
Each person's memory is private to them
Workspace / project
Collaboration tools
Several people share context, separated by subject inside it

Spaces are cheap to create and delete — POST and DELETE /v1/spaces — so err toward more of them. Merging later is far easier than separating.

See also
Namespaces and spacesact_as on the wire clientErasing a subject

Was this page helpful?

Alongside your RAG

Documents and memory answer different questions — keep both, route the query to the right one, and let each do the job the other structurally cannot.

Erasing a subject

Deleting everything about a person — soft versus hard forget, why the cascade matters, and which parts of a data-subject request are yours to own.

On this page

Two levels, different jobsPattern A — a key per tenantPattern B — one principal, act_asScope: isolation, not filteringMany subjects in one spaceChoosing
Suggest an editOpen in Playground