aboutsummaryrefslogtreecommitdiff
path: root/apps/docs/integrations/claude-memory.mdx
blob: 4487cb77f4234fd8f601e60e1360040a3dfc7b92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
---
title: "Claude Memory Tool"
sidebarTitle: "Claude Memory Tool"
description: "Use Claude's native memory tool with Supermemory as the backend"
icon: "/images/anthropic-1.svg"
---

Claude has a native memory tool that allows it to store and retrieve information across conversations. Supermemory provides a backend implementation that maps Claude's memory commands to persistent storage.

<Info>
This integration works with Claude's built-in `memory` tool type, introduced in the Anthropic API. It requires the `context-management` beta flag.
</Info>

## Installation

```bash
npm install @supermemory/tools @anthropic-ai/sdk
```

## Quick Start

```typescript
import Anthropic from "@anthropic-ai/sdk"
import { createClaudeMemoryTool } from "@supermemory/tools/claude-memory"

const anthropic = new Anthropic()

const memoryTool = createClaudeMemoryTool(process.env.SUPERMEMORY_API_KEY!, {
  projectId: "my-app",
})

async function chatWithMemory(userMessage: string) {
  // Send message to Claude with memory tool
  const response = await anthropic.beta.messages.create({
    model: "claude-sonnet-4-5",
    max_tokens: 2048,
    messages: [{ role: "user", content: userMessage }],
    tools: [{ type: "memory_20250818", name: "memory" }],
    betas: ["context-management-2025-06-27"],
  })

  // Handle any memory tool calls
  const toolResults = []
  for (const block of response.content) {
    if (block.type === "tool_use" && block.name === "memory") {
      const toolResult = await memoryTool.handleCommandForToolResult(
        block.input as any,
        block.id
      )
      toolResults.push(toolResult)
    }
  }

  // Send tool results back to Claude if needed
  if (toolResults.length > 0) {
    const finalResponse = await anthropic.beta.messages.create({
      model: "claude-sonnet-4-5",
      max_tokens: 2048,
      messages: [
        { role: "user", content: userMessage },
        { role: "assistant", content: response.content },
        { role: "user", content: toolResults },
      ],
      tools: [{ type: "memory_20250818", name: "memory" }],
      betas: ["context-management-2025-06-27"],
    })

    return finalResponse
  }

  return response
}

// Example usage
const response = await chatWithMemory(
  "Remember that I prefer React with TypeScript for my projects"
)
console.log(response.content[0])
```

## Configuration

```typescript
import { createClaudeMemoryTool } from "@supermemory/tools/claude-memory"

const memoryTool = createClaudeMemoryTool(process.env.SUPERMEMORY_API_KEY!, {
  // Scope memories to a project or user
  projectId: "my-app",

  // Or use container tags for more flexibility
  containerTags: ["user-123", "project-alpha"],

  // Custom memory container prefix (default: "claude_memory")
  memoryContainerTag: "my_memory_prefix",

  // Custom API endpoint
  baseUrl: "https://custom.api.com",
})
```

## How It Works

Claude's memory tool uses a file-system metaphor. Supermemory maps these operations to document storage:

| Claude Command | Supermemory Action |
|----------------|-------------------|
| `view` | Search/retrieve documents |
| `create` | Add new document |
| `str_replace` | Update document content |
| `insert` | Insert content at line |
| `delete` | Delete document |
| `rename` | Move document to new path |

### Memory Path Structure

All memory paths must start with `/memories/`:

```
/memories/preferences.txt       # User preferences
/memories/projects/react.txt    # Project-specific notes
/memories/context/current.txt   # Current context
```

## Commands Reference

### View (Read/List)

```typescript
// List directory contents
{ command: "view", path: "/memories/" }

// Read file contents
{ command: "view", path: "/memories/preferences.txt" }

// Read specific lines
{ command: "view", path: "/memories/notes.txt", view_range: [1, 10] }
```

### Create

```typescript
{
  command: "create",
  path: "/memories/preferences.txt",
  file_text: "User prefers dark mode\nFavorite language: TypeScript"
}
```

### String Replace

```typescript
{
  command: "str_replace",
  path: "/memories/preferences.txt",
  old_str: "dark mode",
  new_str: "light mode"
}
```

### Insert

```typescript
{
  command: "insert",
  path: "/memories/notes.txt",
  insert_line: 5,
  insert_text: "New note added here"
}
```

### Delete

```typescript
{ command: "delete", path: "/memories/old-notes.txt" }
```

### Rename

```typescript
{
  command: "rename",
  path: "/memories/old-name.txt",
  new_path: "/memories/new-name.txt"
}
```

## Complete Example

```typescript
import Anthropic from "@anthropic-ai/sdk"
import { createClaudeMemoryTool } from "@supermemory/tools/claude-memory"

const anthropic = new Anthropic()
const memoryTool = createClaudeMemoryTool(process.env.SUPERMEMORY_API_KEY!, {
  projectId: "assistant",
})

async function runConversation() {
  const messages: Anthropic.MessageParam[] = []

  // Helper to chat with memory
  async function chat(userMessage: string) {
    messages.push({ role: "user", content: userMessage })

    let response = await anthropic.beta.messages.create({
      model: "claude-sonnet-4-5",
      max_tokens: 2048,
      messages,
      tools: [{ type: "memory_20250818", name: "memory" }],
      betas: ["context-management-2025-06-27"],
    })

    // Handle tool calls
    while (response.stop_reason === "tool_use") {
      const toolResults = []

      for (const block of response.content) {
        if (block.type === "tool_use" && block.name === "memory") {
          const result = await memoryTool.handleCommandForToolResult(
            block.input as any,
            block.id
          )
          toolResults.push(result)
        }
      }

      messages.push({ role: "assistant", content: response.content })
      messages.push({ role: "user", content: toolResults })

      response = await anthropic.beta.messages.create({
        model: "claude-sonnet-4-5",
        max_tokens: 2048,
        messages,
        tools: [{ type: "memory_20250818", name: "memory" }],
        betas: ["context-management-2025-06-27"],
      })
    }

    messages.push({ role: "assistant", content: response.content })
    return response
  }

  // Have a conversation with persistent memory
  await chat("My name is Alex and I'm a backend developer")
  await chat("I prefer Go for systems programming")
  await chat("What do you remember about me?")
}

runConversation()
```

## Environment Variables

```bash
SUPERMEMORY_API_KEY=your_supermemory_key
ANTHROPIC_API_KEY=your_anthropic_key
```

## Next Steps

<CardGroup cols={2}>
  <Card title="AI SDK Integration" icon="triangle" href="/integrations/ai-sdk">
    Use with Vercel AI SDK for streamlined development
  </Card>

  <Card title="OpenAI SDK" icon="bolt" href="/integrations/openai">
    Memory tools for OpenAI function calling
  </Card>
</CardGroup>