Browse docs

SDK
Tap to expand
SDKUpdated 2026-05-30

SDK Quickstart

Use RetainDB SDK v5 for user memory, company brain queries, sources, and agent tasks.

Install SDK v5:

bash
npm install @retaindb/sdk@latest

Create a client:

ts
import { RetainDB, RetainDBClient } from "@retaindb/sdk";

const db = new RetainDB({
  apiKey: process.env.RETAINDB_API_KEY,
  project: "production"
});

Use RetainDB for high-level memory and agent helpers. Use RetainDBClient when you need direct access to project, source, and company brain API modules.

User Memory

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

await user.remember("User prefers direct answers.");

const { context } = await user.getContext("How should I reply?");

Use user memory for personalization, preferences, history, and user-specific facts.

Session Memory

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

await session.remember("User is setting up a Next.js API route.");

const { context } = await session.getContext("What are we building?");

Use session memory for temporary conversation or workflow state.

Company Brain

ts
const client = RetainDBClient.fromEnv();

const result = await client.context.query({
  project: "production",
  query: "What does our billing runbook say about retries?",
  top_k: 8
});

console.log(result.context);

Use company brain queries for indexed sources such as docs, repos, URLs, files, and source-derived memories.

Sources

ts
await client.sources.create("production", {
  name: "Billing Runbook",
  connector_type: "text",
  config: {
    content: "Billing retries happen automatically for 3 days before escalation."
  }
});

const sources = await client.sources.list("production");

Use source APIs when a backend, CLI, or agent needs to add or update company brain inputs without the dashboard.

One-Call Non-Streaming Turn

Use runTurn when you want the SDK to retrieve memory, call your generator, and store the last user message.

ts
const turn = await user.runTurn({
  messages: [{ role: "user", content: "How should I set up auth?" }],
  generate: ({ messages }) => llm.chat({ messages })
});

For streaming, use getContext() before the stream and remember() after it finishes.

Agent Task Memory

ts
const task = db.agent("planner").task("checkout-redesign");

await task.event({
  type: "decision",
  summary: "Use Stripe Checkout for v1."
});

const context = await task.context("What should the builder know?");

Use agent task memory for decisions, tool results, failures, and handoffs across multiple agents.

Which Helper Should I Use?

NeedSDK surface
user preferencesdb.user(userId)
conversation statedb.user(userId).session(sessionId)
company knowledgeclient.context.query(...)
agent filesystemclient.context.readFile(path, { project })
source automationclient.sources.*
planner/builder/reviewer workflowsdb.agent(agentId).task(taskId)
low-level memory APIclient.memory.*
project/key administrationclient.projects.*, client.keys.*

Next: User and session memory, Company Brain API, or Agent task API.

Was this page useful?

Your feedback helps us make the product easier to ship with.