Brain Docs

Reference

Rust SDKInstallationHTTP clientWire client
encoderecallforgetencode_vectormemory_listmemory_inspectgraph_fetchlinkunlink
ErrorsRetriesPagination
Brain/Docs
✦ ASKBrain on GitHubDashboard
Rust 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
async fn encode(&self, input: &EncodeInput) -> Result<EncodeResult, BrainHttpError>
Handshake credentialstable
async fn encode(&self, request: &EncodeRequest) -> Result<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

let res = brain
    .encode(&EncodeInput {
        text: "Priya moved from the Berlin office to Lisbon in March.".into(),
        session: Some(42),
        occurred_at_unix_nanos: Some(1_741_000_000_000_000_000),
    })
    .await?;

if res.was_deduplicated {
    println!("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. The verbs borrow their request, so build once and retry against the same value.

use brain_db_sdk::{wire::types::WaitMode, EncodeBuilder};

let request = EncodeBuilder::new("Priya moved to Lisbon in March.")
    .session(42)
    .wait(WaitMode::Derived)
    .build();

let res = client.encode(&request).await?;

Because encode takes &EncodeRequest, retrying is simply calling it again with the same borrow — the request_id does not change, so the server treats the repeat as the same write.

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