Brainby arc-labs/docs
Python SDK

Wire client

BrainClient — the native BRN0 wire client. connect / connect_with, ClientConfig, Auth, the handshake, and the session it exposes.

Connecting

The client opens a TCP connection, runs the handshake (HELLOWELCOMEAUTHAUTH_OK), and returns a client bound to the session the server granted.

token / mTLSstable
BrainClient.connect(host: str, port: int, auth: Auth) -> BrainClient
BrainClient.connect_with(host: str, port: int, config: ClientConfig) -> BrainClient
from brain_db_sdk import BrainClient, Auth

# Simple path — default transport settings.
client = BrainClient.connect("127.0.0.1", 9090, Auth.token(b"my-token"))

# Or use it as a context manager; __exit__ sends BYE and closes the socket.
with BrainClient.connect("127.0.0.1", 9090, Auth.token(b"my-token")) as client:
    ...

connect is the shorthand; connect_with takes an explicit ClientConfig for full control.

Auth

Auth is mandatory — the credential is the connection's whole identity, and the server resolves (namespace, agent, permissions) from it. There is no anonymous mode.

OptionTypeDefault / Env
Auth.token(token: bytes)shared bearer token

A shared bearer token, presented at AUTH.

Auth.mtls(claim: MtlsClaim)mTLS subject claim

An mTLS subject claim.

from brain_db_sdk import Auth

auth = Auth.token(b"my-token")

ClientConfig

connect_with takes a ClientConfig. auth is the only required field; the rest default to a local/dev server (wire version 1, streaming advertised).

ParameterTypeRequired
authAuthrequired

The credential. The server assigns the agent and namespace from it; a config cannot be built without one.

client_idstroptional

Advertised in HELLO. Defaults to "brain-db-sdk-python".

supported_versionslist[int]optional

Wire versions the client offers. Defaults to [1].

capabilitiesHelloCapabilitiesoptional

Advertised transport capabilities. Defaults to streaming on, zstd compression off, server push off.

connect_timeoutfloat | Noneoptional

TCP connect timeout in seconds. Defaults to 10.0.

request_timeoutfloat | Noneoptional

Per-request timeout in seconds. Defaults to 30.0.

from brain_db_sdk import BrainClient, Auth, ClientConfig

config = ClientConfig(
    auth=Auth.token(b"my-token"),
    client_id="my-service",
    request_timeout=60.0,
)
client = BrainClient.connect_with("127.0.0.1", 9090, config)

The session

After the handshake the client exposes the session the server granted:

OptionTypeDefault / Env
client.sessionSessionInfo

The full negotiated session: agent_id, server_id, chosen_version, session_id, bound_shard_id, permissions, namespace, and server_features.

client.agent_idbytes

The agent id this connection acts as.

client.namespacestr

The owning tenant the server bound this connection to (server-derived from auth). Empty when the connection resolves to the reserved brain system namespace. Read-only — the client never sends a namespace.

print(client.namespace)
print(client.session.chosen_version, client.session.bound_shard_id)
print(client.session.permissions)

Multiplexing and pooling

A single BrainClient multiplexes many concurrent requests over one socket — a background reader thread demultiplexes responses by stream_id, so the verbs are thread-safe and many requests can be in flight at once from multiple threads. You do not need a pool for concurrency.

A Pool buys socket-level parallelism — spreading load across N independent TCP connections (and N server-side connection slots), and isolating a slow socket from the rest:

from brain_db_sdk import Pool, Auth

with Pool.connect("127.0.0.1", 9090, size=4, auth=Auth.token(b"my-token")) as pool:
    client = pool.get()          # round-robin borrow
    client.recall(...)

The verb surface

BrainClient serves the full v1 surface. Every verb takes a wire request dataclass and returns a typed response:

  • Core memoryencode, recall (recall_frames), forget, memory_list (memory_list_frames), memory_inspect.
  • Edgeslink, unlink.
  • Typed graphcreate_entity, get_entity, resolve_entity, update_entity, rename_entity, merge_entities, unmerge_entity, tombstone_entity, list_entities; create_statement, get_statement, supersede_statement, tombstone_statement, retract_statement, statement_history, list_statements; create_relation, get_relation, supersede_relation, tombstone_relation, traverse_relations, list_relations_from, list_relations_to; upload_schema, get_schema, validate_schema, list_schemas; graph_fetch.
  • Transactionstxn_begin, txn_commit, txn_abort.
  • Subscriptionssubscribe.
  • Reasoningplan, reason, materialize_procedural.
  • Introspectioncapabilities, extractor_list, query_explain, query_trace.

The core verbs are covered in Encode, Recall, and Forget; the graph, transaction, and subscription verbs in Typed graph.

Closing

client.close()   # sends BYE, then closes the socket

Or use the context-manager form (with BrainClient.connect(...) as client:), which closes on exit.

Was this page helpful?

On this page