Browse docs

SDK

Tap to expand
SDKUpdated 2026-05-25

User and Session Memory

Store, retrieve, and manage memory for users and conversations with SDK v5.

Applies to: @retaindb/sdk@5

Use user memory for long-term personalization and session memory for a specific conversation or workflow.

Remember a fact

ts
await db.user("user_123").remember("User prefers TypeScript examples.");

Remember a conversation

ts
await db.user("user_123").remember([
  { role: "user", content: "I use Next.js App Router." },
  { role: "assistant", content: "Got it." }
]);

The SDK extracts useful user-provided content from the messages.

Retrieve context

ts
const { context, raw } = await db
  .user("user_123")
  .getContext("What framework does this user use?");

Use context in your system prompt. Use raw when you need scores or result metadata.

Scope to a session

ts
const session = db.user("user_123").session("session_abc");

await session.remember("User is debugging webhook retries.");

const { context } = await session.getContext("What is the current issue?");

Forget a memory

ts
await db.user("user_123").forget("mem_...");

Best practices

  • Use your own stable userId and sessionId.
  • Store user-provided facts, preferences, instructions, and events.
  • Retrieve before the model call; remember after the turn.
  • Keep API keys server-side.

Next: Agent task API.

Was this page helpful?

Your feedback helps us prioritize docs improvements weekly.