Memory

Two thin interfaces: key/value MemoryStore and conversation ThreadMemory. Distinct from graph checkpoints.

Key/value store

MemoryStore is a BYO interface. Agents and graphs do not auto-read it — call put / get from tool execute or graph node handlers. Prefer ThreadMemory for chat history.

memory.ts
import { inMemoryStore } from "@monorch/ai";

const memory = inMemoryStore();
await memory.put(["orders"], "ord_9", { lookedUp: true });
const row = await memory.get(["orders"], "ord_9");
await memory.delete?.(["orders"], "ord_9");
const keys = await memory.list?.(["orders"]);

Thread messages with agents

Pass threadId + memory on each run / stream. Monorch loads prior turns into the Rust message list and appends the new user/assistant turns when the run succeeds.

threads.ts
import { agent, inMemoryThreads, mock } from "@monorch/ai";

const threads = inMemoryThreads();
const bot = agent({
  name: "mem",
  model: mock([{ text: "first" }, { text: "second" }]),
  instructions: "Remember prior turns.",
});

await bot.run("hello", { threadId: "t-mem", memory: threads });
await bot.run("again", { threadId: "t-mem", memory: threads });
const hist = await threads.get("t-mem");
// user, assistant("first"), user, assistant("second")

With graphs

Call MemoryStore from node execute for app facts. Checkpoints still own interrupt resume. ThreadMemory is for agent chat transcripts.

graph-memory.ts
.node("lookup", async ({ input }) => {
  await memory.put(["orders"], String(input.orderId), { lookedUp: true });
  return { output: `order:${input.orderId}`, state: { orderId: input.orderId } };
})

Postgres adapters

Durable ThreadMemory and MemoryStore over the same schema helper as checkpoints.

postgres-memory.ts
import {
  ensureMonorchSchema,
  postgresStore,
  postgresThreads,
} from "@monorch/ai/postgres";

await ensureMonorchSchema(pool);
const threads = postgresThreads(pool);
const store = postgresStore(pool);

await bot.run("hello", { threadId: "t1", memory: threads });
await store.put(["orders"], "ord_9", { lookedUp: true });

FAQ