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
BuildBuild

Build

Task recipes for an agent you have already written — wiring in recall, handling corrections, combining with RAG, isolating tenants, and erasing a subject.

PURPOSE

You already have an agent loop. These pages start from that loop and show the smallest change that gives it memory.

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.

PageThe question it answers
Error handlingWhich failures are verdicts on my request, and which are worth repeating?
Retries and cancellationHow do I retry safely, and how do I stop work I no longer need?
IdempotencyA queue redelivered my message — will I write it twice?
StreamingHow do I render a large response as it arrives, and how is that different from a live feed?
Graph queriesI want structure, not prose. Which surface answers which question?
Faithfulness checkingHow do I show that an answer came from something Brain actually holds?
Identity walkthroughWho am I, which space am I in, and how do I act as another one?
Namespace lifecycleHow 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:

SurfaceReference
HTTP / JSONREST API — one page per route
TypeScript@brain-db/sdk
Pythonbrain-db-sdk
Rustbrain-db-sdk
See also
Drop it into an existing loopLearn the model firstREST API reference

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.

On this page

First: prove your key worksThe two callsRecipesWorking with the APIWhen you need the reference instead
Suggest an editOpen in Playground