encode
Store a memory from text — the write verb, its wait modes, and the deduplication result.
encode(text: str, session: int | None = None, occurred_at: int | None = None) -> EncodeResultencode(request: EncodeRequest) -> EncodeResponseParameters
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.
occurred_at_unix_nanos64-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
memory_idstring (wire: 64-bit integer)alwaysThe memory's id. Pass it to forget, memory_inspect, or link.
was_deduplicatedbooleanalwaystrue 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.
created_at_unix_nanos64-bit integeralwaysRecord time — when Brain wrote it, never the event time you passed.
auto_edges_addedintegeralwaysHow many memory-to-memory edges the write pipeline inferred and attached.
Example
res = brain.encode(
text="Priya moved from the Berlin office to Lisbon in March.",
session=42,
occurred_at=1_741_000_000_000_000_000,
)
if res.was_deduplicated:
print("already knew this:", res.memory_id)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.
pending_stagesStageKind[]alwaysWhich derivation stages are still outstanding. Empty under Derived.
has_active_schemabooleanalwaysWhether a typed-graph schema was live for this write. false means
statements and relations were not extracted — not that extraction failed.
embedding_model_fpbytesalwaysThe fingerprint of the model that embedded this memory.
space_id / session_id / edges_out_count—alwaysWhere the write landed, and its outgoing degree.
Building the wire request
EncodeBuilder fills request_id with a fresh UUID, which is what makes a
retry idempotent.
EncodeBuilder(text)EncodeBuilderrequired.session(session_id: int)EncodeBuilderoptional0..occurred_at(occurred_at_unix_nanos: int | None)EncodeBuilderoptionalNone..wait(mode: int = WaitMode.ACK)EncodeBuilderoptional.derived()EncodeBuilderoptional.wait(WaitMode.DERIVED) — the Python SDK has this; the TypeScript one does not..allow_duplicates(allow: bool = True)EncodeBuilderoptional.act_as(namespace, space)EncodeBuilderoptional.build()EncodeRequestrequiredrequest_id.from brain_db_sdk import EncodeBuilder
res = client.encode(
EncodeBuilder("Priya moved to Lisbon in March.").session(42).derived().build()
)On the asyncio tier the call is a coroutine with the same signature:
res = await aclient.encode(
EncodeBuilder("Priya moved to Lisbon in March.").session(42).derived().build()
)Python has no 53-bit float problem — int is arbitrary precision, so session
ids, LSNs and nanosecond timestamps are plain int.
Was this page helpful?