Brain Docs

Reference

Python SDKInstallationHTTP clientWire clientSync vs async
encoderecallforgetencode_vectormemory_listmemory_inspectgraph_fetchlinkunlink
ErrorsRetriesPagination
Brain/Docs
✦ ASKBrain on GitHubDashboard
Python 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(text: str, session: int | None = None, occurred_at: int | None = None) -> EncodeResult
Handshake credentialstable
encode(request: EncodeRequest) -> 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.

occurred_at_unix_nanos64-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
memory_idstring (wire: 64-bit integer)always

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

was_deduplicatedbooleanalways

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.

created_at_unix_nanos64-bit integeralways

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

auto_edges_addedintegeralways

How 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.

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.

pending_stagesStageKind[]always

Which derivation stages are still outstanding. Empty under Derived.

has_active_schemabooleanalways

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

embedding_model_fpbytesalways

The fingerprint of the model that embedded this memory.

space_id / session_id / edges_out_count—always

Where 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.

ParameterTypeRequired
EncodeBuilder(text)EncodeBuilderrequired
The memory text.
.session(session_id: int)EncodeBuilderoptional
Defaults to 0.
.occurred_at(occurred_at_unix_nanos: int | None)EncodeBuilderoptional
Defaults to None.
.wait(mode: int = WaitMode.ACK)EncodeBuilderoptional
Set the wait mode explicitly.
.derived()EncodeBuilderoptional
Shorthand for .wait(WaitMode.DERIVED) — the Python SDK has this; the TypeScript one does not.
.allow_duplicates(allow: bool = True)EncodeBuilderoptional
Skip the dedup check.
.act_as(namespace, space)EncodeBuilderoptional
Write as another space — see act_as.
.build()EncodeRequestrequired
Produces the request, with a fresh request_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.

See also
recallforgetThe write pipeline

Was this page helpful?

Sync vs async

Three tiers, two of them transports for the same protocol — how AsyncBrainClient differs from BrainClient, and why you pick one per program.

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