import { BrainClient } from "@brain-db/sdk";// Brain runs as a server (arena + WAL + redb); the client speaks the wire protocol.// The embedding model is server-owned (BGE-small-en-v1.5, 384-dim) — clients send text.const auth = { kind: "token", token: new TextEncoder().encode(process.env.BRAIN_API_KEY!) };const client = await BrainClient.connect("127.0.0.1", 9090, { auth });
Production (remote server)
import { BrainClient } from "@brain-db/sdk";// Point at your production Brain host; the brain_ API token carries identity (namespace + space).const auth = { kind: "token", token: new TextEncoder().encode(process.env.BRAIN_API_KEY!) };const client = await BrainClient.connect(process.env.BRAIN_HOST!, 9090, { auth });
API Patterns
Encode (store a memory)
import { EncodeBuilder } from "@brain-db/sdk";const stored = await client.encode( new EncodeBuilder("User prefers async communication").build(),);
Recall
import { RecallBuilder } from "@brain-db/sdk";const answer = await client.recall( new RecallBuilder("User communication preferences").maxResults(5).build(),);// answer.answerKind: "Single" | "Many" | "None" — the hits are in answer.memories
Concurrent operations
// Brain has no batch verb; issue encode/recall calls concurrently.const [stored, answer] = await Promise.all([ client.encode(new EncodeBuilder("User prefers dark mode").build()), client.recall(new RecallBuilder("ui preferences").maxResults(5).build()),]);
Performance Targets
Operation
P50
P99
P99.9
Store
5ms
50ms
200ms
Retrieve
10ms
50ms
200ms
Search (1M vectors)
15ms
100ms
500ms
Batch (100 ops)
50ms
500ms
2s
Troubleshooting
Problem
Cause
Fix
High latency
Network round-trip
Co-locate a self-hosted server or add a cache layer
Low recall
Poor embedding model
Upgrade to larger model
High costs
Too many vectors
Implement TTL/eviction
Data loss
No persistence
Use managed Cloud, not cache
Semantic drift
Unstable embeddings
Use fixed embedding model version
Common mistakes
Using only semantic retrieval — Add keyword filtering for precision
No temporal indexing — Always include timestamp for recency bias
Storing raw conversations — Summarize/compress to reduce vectors
Ignoring metadata — Use JSON metadata for filtering and aggregation
No monitoring — Track retrieval quality and latency percentiles