memories-sdk
Use the SDK packages when an application needs memories.sh programmatically. Prefer @memories.sh/core for direct typed API access and @memories.sh/ai-sdk only when the caller already uses the Vercel AI SDK.
Workflow
- Pick the integration surface:
- Use
@memories.sh/core for backend routes, workers, cron jobs, and non-AI-SDK agents.
- Use
@memories.sh/ai-sdk for generateText, streamText, middleware, or tool loops built on ai.
- If the task is about the CLI or MCP configuration, switch to
memories-cli or memories-mcp.
- Set scope before writing code:
- Keep
MEMORIES_API_KEY server-side.
tenantId selects the tenant or workspace database.
userId narrows memory to a user inside that tenant.
projectId narrows reads and writes to a product area, repo, or feature slice.
- Use the narrowest pattern that solves the task:
- Direct CRUD or context lookup:
MemoriesClient
- Automatic prompt injection:
memoriesMiddleware
- Agent loops with explicit memory tools:
memoriesTools and memoriesSystemPrompt
- Fetch once and reuse:
preloadContext
- Persist after completion:
createMemoriesOnFinish
- Verify the integration:
- Confirm the same scope is used on both reads and writes.
- Catch
MemoriesClientError.
- Do not expose the API key to browser-only code.
Quick Start
@memories.sh/core
import { MemoriesClient } from "@memories.sh/core"
const client = new MemoriesClient({
apiKey: process.env.MEMORIES_API_KEY,
tenantId: "acme-prod",
userId: "user_123",
})
const context = await client.context.get({
query: "billing architecture",
projectId: "dashboard",
mode: "all",
strategy: "hybrid",
limit: 8,
})
await client.memories.add({
content: "Enterprise billing uses Stripe invoices.",
type: "fact",
projectId: "dashboard",
tags: ["billing"],
})
@memories.sh/ai-sdk
import { generateText, stepCountIs, wrapLanguageModel } from "ai"
import { openai } from "@ai-sdk/openai"
import {
memoriesMiddleware,
memoriesSystemPrompt,
memoriesTools,
} from "@memories.sh/ai-sdk"
const model = wrapLanguageModel({
model: openai("gpt-4o"),
middleware: memoriesMiddleware({
tenantId: "acme-prod",
userId: "user_123",
projectId: "dashboard",
}),
})
const result = await generateText({
model,
system: memoriesSystemPrompt(),
tools: memoriesTools({
tenantId: "acme-prod",
userId: "user_123",
projectId: "dashboard",
}),
stopWhen: stepCountIs(5),
prompt: "Summarize prior decisions about billing.",
})
console.log(result.text)
Decision Guide
- Need direct typed access from your own backend code: use
MemoriesClient
- Need automatic context injection into prompts or messages: use
memoriesMiddleware
- Need the model to read or write memory explicitly through tools: use
memoriesTools
- Need to manage stored skill files or procedure fragments: use
client.skills.* or the AI SDK skill-file tools
- Need tenant, key, or embedding usage administration: use
client.management.*
- Need internals of the memories monorepo or server endpoints: use
memories-dev
Reference Files
references/core.md: direct client methods, transport choices, errors, management APIs, and skill-file APIs
references/ai-sdk.md: middleware, tools, preload, post-finish persistence, and query extraction patterns
references/scoping.md: tenant/user/project scoping rules, server-side safety, and debugging checklist
1---2name: memories-sdk3description: Build against the memories.sh SDK packages in application code. Use when working with `@memories.sh/core` or `@memories.sh/ai-sdk`, including: (1) Initializing `MemoriesClient`, (2) Reading, writing, searching, or editing memories from backend code, route handlers, workers, or scripts, (3) Integrating memories with the Vercel AI SDK via `memoriesMiddleware`, `memoriesTools`, `preloadContext`, or `createMemoriesOnFinish`, (4) Choosing and applying `tenantId` / `userId` / `projectId` scoping, (5) Managing SDK skill files or management APIs, or (6) Debugging memories SDK usage in TypeScript or JavaScript applications. Use `memories-cli` for CLI workflows, `memories-mcp` for MCP setup, and `memories-dev` for monorepo internals.4---56# memories-sdk78Use the SDK packages when an application needs memories.sh programmatically. Prefer `@memories.sh/core` for direct typed API access and `@memories.sh/ai-sdk` only when the caller already uses the Vercel AI SDK.910## Workflow11121. Pick the integration surface:13 - Use `@memories.sh/core` for backend routes, workers, cron jobs, and non-AI-SDK agents.14 - Use `@memories.sh/ai-sdk` for `generateText`, `streamText`, middleware, or tool loops built on `ai`.15 - If the task is about the CLI or MCP configuration, switch to `memories-cli` or `memories-mcp`.162. Set scope before writing code:17 - Keep `MEMORIES_API_KEY` server-side.18 - `tenantId` selects the tenant or workspace database.19 - `userId` narrows memory to a user inside that tenant.20 - `projectId` narrows reads and writes to a product area, repo, or feature slice.213. Use the narrowest pattern that solves the task:22 - Direct CRUD or context lookup: `MemoriesClient`23 - Automatic prompt injection: `memoriesMiddleware`24 - Agent loops with explicit memory tools: `memoriesTools` and `memoriesSystemPrompt`25 - Fetch once and reuse: `preloadContext`26 - Persist after completion: `createMemoriesOnFinish`274. Verify the integration:28 - Confirm the same scope is used on both reads and writes.29 - Catch `MemoriesClientError`.30 - Do not expose the API key to browser-only code.3132## Quick Start3334### `@memories.sh/core`3536```ts37import { MemoriesClient } from "@memories.sh/core"3839const client = new MemoriesClient({40 apiKey: process.env.MEMORIES_API_KEY,41 tenantId: "acme-prod",42 userId: "user_123",43})4445const context = await client.context.get({46 query: "billing architecture",47 projectId: "dashboard",48 mode: "all",49 strategy: "hybrid",50 limit: 8,51})5253await client.memories.add({54 content: "Enterprise billing uses Stripe invoices.",55 type: "fact",56 projectId: "dashboard",57 tags: ["billing"],58})59```6061### `@memories.sh/ai-sdk`6263```ts64import { generateText, stepCountIs, wrapLanguageModel } from "ai"65import { openai } from "@ai-sdk/openai"66import {67 memoriesMiddleware,68 memoriesSystemPrompt,69 memoriesTools,70} from "@memories.sh/ai-sdk"7172const model = wrapLanguageModel({73 model: openai("gpt-4o"),74 middleware: memoriesMiddleware({75 tenantId: "acme-prod",76 userId: "user_123",77 projectId: "dashboard",78 }),79})8081const result = await generateText({82 model,83 system: memoriesSystemPrompt(),84 tools: memoriesTools({85 tenantId: "acme-prod",86 userId: "user_123",87 projectId: "dashboard",88 }),89 stopWhen: stepCountIs(5),90 prompt: "Summarize prior decisions about billing.",91})9293console.log(result.text)94```9596## Decision Guide9798- Need direct typed access from your own backend code: use `MemoriesClient`99- Need automatic context injection into prompts or messages: use `memoriesMiddleware`100- Need the model to read or write memory explicitly through tools: use `memoriesTools`101- Need to manage stored skill files or procedure fragments: use `client.skills.*` or the AI SDK skill-file tools102- Need tenant, key, or embedding usage administration: use `client.management.*`103- Need internals of the memories monorepo or server endpoints: use `memories-dev`104105## Reference Files106107- `references/core.md`: direct client methods, transport choices, errors, management APIs, and skill-file APIs108- `references/ai-sdk.md`: middleware, tools, preload, post-finish persistence, and query extraction patterns109- `references/scoping.md`: tenant/user/project scoping rules, server-side safety, and debugging checklist