# AI Memory

An assistant that saves what it learns about you into a memory list. New facts replace the ones they contradict, and you can edit or forget any of them.

Install with

```bash
npx shadcn@latest add https://kokonutui.com/r/ai-memory.json
```

### About you

- Goes by Samantha  Aug 3
- Lives in Berlin  Aug 3

### Food

- Is pescatarian  Sep 12
- Likes food very spicy  Sep 12

### Work

- Product designer at Fieldwork  Jul 28
- Wants short, direct answers  Jul 28

## Usage

Pass your saved memories as `initialMemories` and a `remember` that asks your model. It gets the message and the memories in the panel, and returns the reply plus, when there's something worth keeping, a `memory` to save (with `replaces` set to an older memory's id to update it). Return it as a Promise and the assistant shows it thinking until it resolves; throw and the reply shows the error's message. `onSave`, `onEdit` and `onForget` tell you what to persist; a forget only fires once its Undo has passed.

```javascript
"use client";

import AIMemory, { type MemoryChange, type MemoryItem } from "@/components/kokonutui/ai-memory";

export function Assistant({ memories }: { memories: MemoryItem[] }) {
  return (
    <AIMemory
      greeting="Hi. What's new?"
      initialMemories={memories}
      onEdit={(memory) => fetch(`/api/memories/${memory.id}`, { method: "PUT", body: JSON.stringify(memory) })}
      onForget={(memory) => fetch(`/api/memories/${memory.id}`, { method: "DELETE" })}
      onSave={(memory, replaced) =>
        fetch("/api/memories", { method: "POST", body: JSON.stringify({ memory, replaces: replaced?.id }) })
      }
      remember={async (text, memories) => {
        const res = await fetch("/api/remember", {
          method: "POST",
          body: JSON.stringify({ text, memories }),
        });
        if (!res.ok) {
          throw new Error("Couldn't reach the model. Nothing was saved.");
        }
        return (await res.json()) as MemoryChange;
      }}
      suggestions={[]}
    />
  );
}
```

```typescript
// app/api/remember/route.ts
import { anthropic } from "@ai-sdk/anthropic";
import { generateObject } from "ai";
import { z } from "zod";

export async function POST(req: Request) {
  const { text, memories } = await req.json();
  const { object } = await generateObject({
    model: anthropic("claude-sonnet-5-5"),
    schema: z.object({
      reply: z.string(),
      memory: z
        .object({ text: z.string(), group: z.string(), replaces: z.string().optional() })
        .optional(),
    }),
    system: `Reply briefly. Save a memory only for lasting facts about the user. Known: ${JSON.stringify(memories)}`,
    prompt: text,
  });
  return Response.json(object);
}
```

### Installation

AI Memory is a free, open-source React component in the Kokonut UI AI collection. Add it with the shadcn CLI:

```bash
npx shadcn@latest add @kokonutui/ai-memory
```

Or add it by hand: install its packages, then copy the source from above into `components/kokonutui/ai-memory.tsx`:

```bash
npm install lucide-react motion
```

### Props

`title`  
`string`  
Default: `"Assistant"`

`greeting`  
`string`  
Default: `"Hi Samantha. What's new? Tell me anything and …"`

`initialMemories`  
`MemoryItem[]`  
Default: `demo data`

`suggestions`  
`string[]`  
Default: `Object.keys(DEMO_REPLIES)`

`remember`  
`(text: string, memories: MemoryItem[]) => MemoryChange | Promise<MemoryChange>`  
Default: `demo data`

`onSave`  
`(memory: MemoryItem, replaced?: MemoryItem) => void`

`onEdit`  
`(memory: MemoryItem) => void`

`onForget`  
`(memory: MemoryItem) => void`

`className`  
`string`

### MemoryItem

`id`  
`string`  
`text`  
`string`  
`group`  
`string`  
`saved`  
`string`

### MemoryChange

`reply`  
`string`

`memory`  
 `{ text: string; group: string; replaces?: string }`

### Dependencies

| Needs                      | Version      | Comes from                    |
|----------------------------|--------------|-------------------------------|
| `lucide-react`             | `^0.556.0`  | npm, installed by the CLI     |
| `motion`                   | `^12.23.25` | npm, installed by the CLI     |
| `cn` from `lib/utils.ts`   |              | shadcn init                   |
| React                      | `19`        | Your app                      |
| Tailwind CSS               | `4`         | Your app                      |
