Build
Task recipes for an agent you have already written — wiring in recall, handling corrections, combining with RAG, isolating tenants, and erasing a subject.
If "agent memory" is a new idea, read Learn first — it takes about twenty minutes and will make the choices here obvious. If you're here because something is already broken in production, start below.
First: prove your key works
Before anything else. Point BRAIN_BASE_URL at your Brain and put your key in
the environment — every snippet on these pages reads both:
export BRAIN_BASE_URL=http://127.0.0.1:8080 # or your own gateway
export BRAIN_API_KEY=brain_…Then one call, no side effects:
curl -s "$BRAIN_BASE_URL/v1/whoami" \
-H "Authorization: Bearer $BRAIN_API_KEY"{ "namespace": "…", "space_id": "…", "permissions": { … } }That tells you the key is live and, more usefully, which namespace and space it is bound to. The scope comes from the key itself — you never construct it in client code, and a key cannot read outside what it was issued for.
If this returns 401, stop here. Every other call will fail the same way and the errors downstream are much harder to read.
The two calls
Everything else is detail on top of these.
// write — what the agent learned
await brain.encode({ text: "Priya prefers terse answers with no preamble" });
// read — what it needs this turn
const answer = await brain.recall({ query: userMessage, maxResults: 5 });# write — what the agent learned
brain.encode(text="Priya prefers terse answers with no preamble")
# read — what it needs this turn
answer = brain.recall(query=user_message, max_results=5)// write — what the agent learned
brain.encode(&EncodeInput { text: "Priya prefers terse answers with no preamble".into(), ..Default::default() }).await?;
// read — what it needs this turn
let answer = brain.recall(&RecallInput { query: user_message, max_results: Some(5), ..Default::default() }).await?;Over HTTP that's POST /v1/memories and POST /v1/recall.
Recipes
Start from the loop you already have.
Drop it into an existing loop
Where the two calls go, and what to do with what comes back.
A chatbot that remembers
Memory that survives across sessions, without re-sending the transcript.
Handling corrections
When the user says "actually, no" — the highest-value write you can make.
Alongside your RAG
Documents and memory answer different questions. Use both.
One space per customer
Isolation that holds at the retrieval boundary, not in a filter.
Erasing a subject
Deleting everything about a person, and proving it is gone.
Working with the API
The cross-cutting behaviour every integration meets sooner or later.
| Page | The question it answers |
|---|---|
| Error handling | Which failures are verdicts on my request, and which are worth repeating? |
| Retries and cancellation | How do I retry safely, and how do I stop work I no longer need? |
| Idempotency | A queue redelivered my message — will I write it twice? |
| Streaming | How do I render a large response as it arrives, and how is that different from a live feed? |
| Graph queries | I want structure, not prose. Which surface answers which question? |
| Faithfulness checking | How do I show that an answer came from something Brain actually holds? |
| Identity walkthrough | Who am I, which space am I in, and how do I act as another one? |
| Namespace lifecycle | How do I provision, operate and tear down a tenant? |
When you need the reference instead
These pages are recipes. For the exact shape of a call, go to the surface you are using:
| Surface | Reference |
|---|---|
| HTTP / JSON | REST API — one page per route |
| TypeScript | @brain-db/sdk |
| Python | brain-db-sdk |
| Rust | brain-db-sdk |
Was this page helpful?
What to write, and what not to
The judgement call every memory deployment gets wrong at least once — plus exactly which duplicates the database collapses for you, and which ones it lets through.
Drop it into an existing loop
Where recall and encode go in an agent loop you have already written, what to do with the answer, and why writing every turn is the wrong default.