encode
Store a memory from text — the write verb, its wait modes, and the deduplication result.
encode(input: EncodeInput): Promise<EncodeResult>encode(request: EncodeRequest): Promise<EncodeResponse>Parameters
textstringrequiredThe 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 integeroptionalThe 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 integeroptionalWhen 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
memoryIdstring (wire: 64-bit integer)alwaysThe memory's id. Pass it to forget, memoryInspect, or link.
wasDeduplicatedbooleanalwaystrue 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.
saliencefloatalwaysThe memory's initial salience, in [0, 1]. It decays from here — see Decay and retention.
kindintegeralwaysThe memory kind Brain assigned: 0 episodic, 1 semantic, 2 consolidated.
createdAtUnixNanos64-bit integeralwaysRecord time — when Brain wrote it, never the event time you passed.
autoEdgesAddedintegeralwaysHow 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.
Ack0 — the defaultReturn once the write is durable. Derivation — embedding, extraction, graph — is still running. This is the mode to use on a request path.
Derived1Return 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:
lsn64-bit integeralwaysThe log sequence number this write landed at. Pass it as the resume position to subscribe to continue exactly here.
pendingStagesStageKind[]alwaysWhich derivation stages are still outstanding. Empty under Derived.
hasActiveSchemabooleanalwaysWhether a typed-graph schema was live for this write. false means
statements and relations were not extracted — not that extraction failed.
embeddingModelFpbytesalwaysThe fingerprint of the model that embedded this memory.
spaceId / sessionId / edgesOutCount—alwaysWhere 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.
new EncodeBuilder(text)EncodeBuilderrequired.session(sessionId: bigint)thisoptional0n..occurredAt(unixNanos: bigint)thisoptional.wait(mode?: WaitMode)thisoptionalWaitMode.Derived. Defaults to WaitMode.Ack..allowDuplicates(allow?: boolean)thisoptionaltrue..actAs(namespace, space)thisoptional.build()EncodeRequestrequiredrequestId.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.
Was this page helpful?