Session memory ​
anvia memory (CLI 1.6.0+) configures durable agent session memory in an existing project. It installs the matching @anvia/memory-* adapter, writes a small createMemoryStore() helper, and, for Drizzle and Prisma, runs the adapter's own schema generator. It never connects to a database or applies migrations.
Use it to start a new project or add memory quickly. Configure the adapter by hand, as shown in Configure memory, when you need custom scope keys, an injected database client, or delegates that differ from the generated helper. The CLI output is ordinary source you own and can edit.
Choose a backend ​
anvia memory listlist prints the four backends, their packages, and whether each is already in package.json. It marks one as recommended from your existing dependencies, in this order:
| Backend | Package | Recommended when the project has |
|---|---|---|
drizzle | @anvia/memory-drizzle | @anvia/memory-drizzle or drizzle-orm |
prisma | @anvia/memory-prisma | @anvia/memory-prisma, @prisma/client, @prisma/orm-postgres, or prisma |
postgres | @anvia/memory-postgres | @anvia/memory-postgres or pg |
sqlite | @anvia/memory-sqlite | none of the above (default) |
Dependencies, devDependencies, and peerDependencies all count.
Initialize memory ​
anvia memory init sqlite
anvia memory init postgres --env-var DATABASE_URL
anvia memory init drizzle --schema src/db/schema.ts
anvia memory init prisma --prisma-version 7 --schema prisma/schema.prisma
anvia memory init prisma --prisma-version 8 --contract src/prisma/contract.prismaOmit the backend name to choose one. In an interactive terminal, the CLI asks for the backend (defaulting to the recommended one), its paths, whether to install missing packages, then shows a preview and asks to apply (default yes). Without a terminal, or with --yes or --json, it uses the recommended backend and the defaults below and applies immediately. Add --dry-run to print the planned changes without writing files, installing packages, or running a generator.
| Option | Backends | Default and effect |
|---|---|---|
--output <path> | all | Helper file path. src/anvia/memory.ts when src/ exists, otherwise anvia/memory.ts; .mjs instead of .ts when the project has neither tsconfig.json nor a typescript dependency. |
--db-path <path> | sqlite | data/anvia-memory.sqlite, or :memory: for an in-process database. |
--env-var <name> | postgres | DATABASE_URL. Must be a valid environment variable name. |
--schema <path> | drizzle, prisma 7 | Read from drizzle.config.* or prisma.config.* when present; otherwise the first existing src/db/schema.ts, db/schema.ts, or src/schema.ts for Drizzle, and prisma/schema.prisma for Prisma. |
--contract <path> | prisma 8 | Must end in .prisma. Read from prisma.config.*, then the first existing src/prisma/contract.prisma, prisma/contract.prisma, or contract.prisma. |
--prisma-version 7|8 | prisma | Inferred from --contract (8) or --schema (7), then from dependencies: @prisma/orm-postgres or a prisma/@prisma/client range starting with 8 selects 8; otherwise 7. |
--append | drizzle, prisma | Add the generated tables or models to your schema or contract inside a marker block instead of a separate file. |
--no-install | all | Do not install packages. |
--skip-schema | drizzle, prisma | Do not run the schema generator; print its command as a next step. |
--force | all | Overwrite a helper file that differs, and regenerate existing generated schema output. |
--dry-run | all | Preview only. |
The global --cwd, --yes, and --json options also apply. Paths must stay inside the project. A backend-specific option used with another backend fails with a usage error.
What it changes ​
| Backend | Writes or patches | Next steps it prints |
|---|---|---|
sqlite | Helper file; a # anvia-memory:start … # anvia-memory:end block in .gitignore listing the database file and its -wal and -shm files (skipped for :memory:). | Import the helper and pass store to Agent({ memory: { store } }). |
postgres | Helper file; a marked block in .env.example with <VAR>=postgresql://postgres:postgres@localhost:5432/anvia. | Set the variable in your .env, then use the helper. |
drizzle | Helper file; generated tables in a sibling anvia-memory.ts next to the schema, or appended with --append. | Install drizzle-orm if missing, run drizzle-kit generate and drizzle-kit migrate, then pass your db to the helper. |
prisma 7 | Helper file; generated models in models/anvia-memory.prisma beside the schema, or appended with --append. | Install @prisma/client if missing, run prisma validate and prisma migrate dev --name add_anvia_memory, then pass your client to the helper. |
prisma 8 | Helper file importing @anvia/memory-prisma/v8; memory models in the contract. | Install @prisma/orm-postgres if missing, run prisma contract emit and prisma db update, then pass your client to the helper. |
The .env.example value is a local-development placeholder, not a credential. Keep real connection strings in an untracked .env or your secret manager. The CLI does not read or write .env.
Marker blocks are replaced in place on later runs; content outside them is preserved. A file with mismatched markers stops the command with an error instead of being rewritten. An existing helper that differs from the template is skipped unless you pass --force.
Helper file ​
For SQLite, the generated helper is:
import { SqliteMemoryClient } from "@anvia/memory-sqlite";
const defaultPath = "data/anvia-memory.sqlite";
export async function createMemoryStore(path = defaultPath) {
const database = new SqliteMemoryClient({ path });
const store = database.memoryStore();
await store.ensure();
return { database, store };
}The Postgres helper reads the connection string from your variable and throws when it is unset, then calls ensure(). The Drizzle and Prisma helpers take your existing db or Prisma client, construct the store, and call validate(); they never create tables. Add a scope key or other store options by editing the helper. See Configure memory.
Packages ​
When package.json exists, init installs whichever of @anvia/core and the backend's @anvia/memory-* package is missing, as regular dependencies, with the detected package manager: the packageManager field first, then the nearest pnpm-lock.yaml, bun.lock/bun.lockb, yarn.lock, or package-lock.json up the directory tree, then npm_config_user_agent, falling back to pnpm. It does not install drizzle-orm, @prisma/client, @prisma/orm-postgres, or the prisma CLI; those appear as next steps. Pass --no-install to manage packages yourself.
Schema generation ​
For Drizzle and Prisma, init runs the schema generator shipped in the installed adapter package, equivalent to:
pnpm exec anvia-memory-drizzle init --schema src/db/schema.ts --write
pnpm exec anvia-memory-prisma init --prisma-version 7 --schema prisma/schema.prisma --write
pnpm exec anvia-memory-prisma init --prisma-version 8 --contract src/prisma/contract.prisma --write--append adds --append-to-schema (or --append-to-contract for Prisma 8), and --force is passed through. The generator runs only when the adapter package is installed or about to be installed, and, for Drizzle and Prisma 7, only when the schema file exists. When the generated output already exists, init skips the generator and tells you to pass --force. See the Drizzle and Prisma schema guides for what each generator writes.
For Prisma 8, a contract that already exists requires --append (an interactive run asks first); otherwise init stops rather than overwrite it. --append with a missing contract also fails.
The generator is code from node_modules/@anvia/memory-drizzle or node_modules/@anvia/memory-prisma, executed with the same Node.js binary as the CLI. Review lockfile changes before running init in a repository you do not control.
Check the setup ​
anvia memory check
anvia memory check postgres --env-var DATABASE_URLcheck is read-only and does not connect to a database. Without a backend name it checks every @anvia/memory-* package in package.json. Each result is pass, warn, or fail, and the command exits 1 only when a check fails.
| Check | fail | warn |
|---|---|---|
@anvia/core and the backend package in package.json | Missing | — |
| Any memory package (no backend given) | None installed | — |
| SQLite runtime | — | node:sqlite needs --experimental-sqlite on the Node.js running the CLI. Bun passes; Node.js passes from 22.13, 23.4, or 24. |
| Postgres connection variable | — | Not set in the environment, .env, or .env.local |
| Drizzle peer and tables | drizzle-orm missing | Generated tables not found in anvia-memory.ts or the schema |
| Prisma peer and models | @prisma/client (7) or @prisma/orm-postgres (8) missing | Memory models not found in the generated file, schema, or contract |
check inspects files only. Confirm migrations ran and the store validates by starting the application or calling validate().
What your application owns ​
- Database credentials, connection pooling, and where secrets are stored.
- Migrations and when they run;
initwrites schema source but never applies it. - The
sessionId,userId, and tenant metadata that scope each conversation, and authorization before a session is loaded. See Sessions. - Retention and deletion of stored conversations.