encode
Store a memory from text with encode — request fields and response shape for both the HTTP and wire clients.
Signature
// HTTP client
brain.encode(input: http.EncodeInput): Promise<http.EncodeResult>
// wire client
client.encode(request: EncodeRequest): Promise<EncodeResponse>Request fields
The HTTP input is loose — only text is required.
textstringrequiredThe text to remember.
contextnumberoptionalContext id that scopes the memory (e.g. a conversation or session). Defaults
to 0.
occurred_atnumberoptionalUnix-nanos wall-clock time the remembered event occurred. Omit to let Brain stamp write time.
The wire request mirrors the protocol field-for-field. Build it by hand or with
EncodeBuilder, which fills defaults and mints the requestId.
textstringrequiredThe text to remember.
contextIdbigintrequiredContext id that scopes the memory. Use 0n for none.
requestIdUint8Arrayrequired16-byte idempotency key. EncodeBuilder mints one for you.
txnIdUint8Array | nullrequiredEnclosing transaction id, or null for an autocommit write.
occurredAtUnixNanosbigint | nullrequiredWall-clock time the event occurred, or null.
actAsActAs | nulloptionalRun as an effective (namespace, agentId) on behalf of the connection
principal. null runs as the connection's own identity. Requires the
canActAs grant.
waitWaitModeoptionalWrite-completion mode. WaitMode.Ack (the default) returns after the durable
ack; WaitMode.Derived blocks until async derivation finishes and the
response carries the full write-analysis trace. This is the write knob.
allowDuplicatesbooleanoptionalOpt out of content dedup. By default Brain dedupes byte-identical text on
(agentId, contextId, BLAKE3(text)) and returns the existing memory
(wasDeduplicated = true) without writing. Set true to force a distinct
memory.
Response fields
memory_idstringalwaysThe id assigned to the memory.
was_deduplicatedbooleanalwaystrue when identical text already existed and no new memory was written.
saliencenumberalwaysInitial salience score.
kindnumberalwaysMemory-kind discriminant.
created_at_unix_nanosnumberalwaysWrite timestamp in unix-nanos.
auto_edges_addednumberalwaysCount of similarity/temporal edges auto-added at write time.
The wire response carries the HTTP fields plus write-pipeline detail.
memoryIdbigintalwaysThe id assigned to the memory (numeric on the wire).
wasDeduplicatedbooleanalwaystrue when identical text already existed.
saliencenumberalwaysautoEdgesAddednumberalwayslsnbigintalwaysLog sequence number of the durable write — usable as a SUBSCRIBE cursor.
kindMemoryKindWirealwayscreatedAtUnixNanosbigintalwayspendingStagesStageKind[]alwaysAsync derivation stages still pending (auto-edge, temporal-edge, extractor).
hasActiveSchemabooleanalwaysWhether a user schema is active on the shard.
traceEncodeTraceFull synchronous write-analysis trace — present only when the request set
wait = WaitMode.Derived.
Also returned: agentId, contextId, edgesOutCount, embeddingModelFp.
Examples
import { BrainHttpClient } from "@brain-db/sdk";
const brain = new BrainHttpClient({ apiKey: process.env.BRAIN_API_KEY! });
const res = await brain.encode({
text: "I prefer dark mode and live in Berlin.",
context: 42,
});
console.log(res.memory_id, res.was_deduplicated);import { BrainClient, EncodeBuilder } from "@brain-db/sdk";
const client = await BrainClient.connect("127.0.0.1", 9090, {
auth: { kind: "token", token: tokenBytes },
});
const res = await client.encode(
new EncodeBuilder("I prefer dark mode and live in Berlin.")
.context(42n)
.build(),
);
console.log(res.memoryId, res.pendingStages);On the wire client, call .wait() on the builder (equivalent to
WaitMode.Derived) when you need the write to be fully derived — entities,
statements, and relations extracted — before the call resolves, plus the
populated trace.
Was this page helpful?
Wire client
BrainClient — connect over the native BRN0 protocol, authenticate after the handshake, and reach the full verb surface including the typed graph, transactions, and subscriptions.
recall
Retrieve an answer for a cue with recall — the Single / Many / None membership shape, request fields, and response for both clients.