Brain Docs

Reference

TypeScript SDKInstallationHTTP clientWire client
encoderecallforgetencodeVectormemoryListmemoryInspectgraphFetchlinkunlink
ErrorsRetriesPagination
Brain/Docs
✦ ASKBrain on GitHubDashboard
TypeScript SDKMemoryencode

encode

Store a memory from text — the write verb, its wait modes, and the deduplication result.

PURPOSE

encode is Brain's write verb. You hand it text; Brain durably records it, then derives an embedding, keyword fields, statements and entities from it in the background. The two tiers differ only in how the request is built — the operation, and what "done" means, are the same.

POST/v1/memoriesBearer API keystable
encode(input: EncodeInput): Promise<EncodeResult>
Handshake credentialstable
encode(request: EncodeRequest): Promise<EncodeResponse>

Parameters

ParameterTypeRequired
textstringrequired

The memory, verbatim. Brain stores the text as given and derives everything else from it. Empty text is rejected (TextEmpty), and text over the shard's cap is rejected (TextTooLarge) rather than truncated.

session64-bit integeroptional

The conversation this memory belongs to. Omit it and the memory is recorded against session 0 — the space's default session, not an error. See Create a session.

occurredAtUnixNanos64-bit integeroptional

When the thing the memory describes happened, as distinct from when Brain recorded it. Omit it and Brain uses record time for both. Backfilling history is the case that needs it: without it, a year of imported conversation all appears to have happened at import.

Returns

FieldTypePresence
memoryIdstring (wire: 64-bit integer)always

The memory's id. Pass it to forget, memoryInspect, or link.

wasDeduplicatedbooleanalways

true means Brain recognised this text as one it already holds and returned the existing id instead of writing a second copy. The call still succeeded; nothing new was stored. Treating this as a fresh write is how a caller over-counts what it has written.

saliencefloatalways

The memory's initial salience, in [0, 1]. It decays from here — see Decay and retention.

kindintegeralways

The memory kind Brain assigned: 0 episodic, 1 semantic, 2 consolidated.

createdAtUnixNanos64-bit integeralways

Record time — when Brain wrote it, never the event time you passed.

autoEdgesAddedintegeralways

How many memory-to-memory edges the write pipeline inferred and attached.

Example

const res = await brain.encode({
  text: "Priya moved from the Berlin office to Lisbon in March.",
  session: 42n,
  occurredAtUnixNanos: 1_741_000_000_000_000_000n,
});

if (res.wasDeduplicated) {
  console.log("already knew this:", res.memoryId);
}

Wait modes

The wire tier decides when the call returns.

OptionTypeDefault / Env
Ack0 — the default

Return once the write is durable. Derivation — embedding, extraction, graph — is still running. This is the mode to use on a request path.

Derived1

Return once derivation has finished too, so the memory is fully searchable by every retriever. Costs the pipeline's latency, and the client applies its derived-write timeout (120 s) instead of the normal 30 s request timeout — because the normal one would fire first.

Ack does not mean invisible

A memory acked but not yet derived is already semantically searchable — Brain embeds on the write path, before the ack barrier. What Derived additionally waits for is the rest: extraction, the typed graph, keyword fields. See the write pipeline.

The HTTP tier has no wait mode. POST /v1/memories returns at the ack barrier.

The wire response

The wire EncodeResponse carries more than the HTTP result:

FieldTypePresence
lsn64-bit integeralways

The log sequence number this write landed at. Pass it as the resume position to subscribe to continue exactly here.

pendingStagesStageKind[]always

Which derivation stages are still outstanding. Empty under Derived.

hasActiveSchemabooleanalways

Whether a typed-graph schema was live for this write. false means statements and relations were not extracted — not that extraction failed.

embeddingModelFpbytesalways

The fingerprint of the model that embedded this memory.

spaceId / sessionId / edgesOutCount—always

Where the write landed, and its outgoing degree.

Building the wire request

EncodeBuilder fills requestId with a fresh UUID, which is what makes a retry idempotent.

ParameterTypeRequired
new EncodeBuilder(text)EncodeBuilderrequired
The memory text.
.session(sessionId: bigint)thisoptional
Defaults to 0n.
.occurredAt(unixNanos: bigint)thisoptional
Defaults to null.
.wait(mode?: WaitMode)thisoptional
No argument means WaitMode.Derived. Defaults to WaitMode.Ack.
.allowDuplicates(allow?: boolean)thisoptional
Skip the dedup check. No argument means true.
.actAs(namespace, space)thisoptional
Write as another space — see act_as.
.build()EncodeRequestrequired
Produces the request, with a fresh requestId.
import { EncodeBuilder, WaitMode } from "@brain-db/sdk";

const res = await client.encode(
  new EncodeBuilder("Priya moved to Lisbon in March.")
    .session(42n)
    .wait(WaitMode.Derived)
    .build(),
);

bigint, not number

Session ids, LSNs and nanosecond timestamps are 64-bit, typed bigint, and serialised losslessly. Passing one through stock JSON.parse silently rounds past 2^53 — which is why the SDK never does.

See also
recallforgetThe write pipeline

Was this page helpful?

Wire client

BrainClient over the BRN0 protocol — connect, ClientConfig, the two auth methods, act_as, multiplexing, the connection pool, and the id helpers.

recall

Retrieve memories for a cue — the Single / Many / None answer shape, scope, and the retrieval trace.

On this page

ParametersReturnsExampleWait modesThe wire responseBuilding the wire request
Suggest an editOpen in Playground