Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

12.1 KB
Raw Blame
---
title: "Memory Export/Import"
description: "Share knowledge across claude-mem installations with duplicate prevention"
---

# Memory Export/Import Scripts

Share your claude-mem knowledge with other users! These scripts allow you to export specific memories (observations, sessions, summaries, and prompts) and import them into another claude-mem installation.

## Use Cases

- **Share Windows compatibility knowledge** with Windows users
- **Share bug fix patterns** with contributors
- **Share project-specific learnings** across teams
- **Backup specific memory sets** for safekeeping

## How It Works

### Export Script

Searches the database using **hybrid search** (combines ChromaDB vector embeddings with FTS5 full-text search) and exports all matching:
- **Observations** - Individual learnings and discoveries
- **Sessions** - Session metadata
- **Summaries** - Session summaries
- **Prompts** - User prompts that led to the work

Output is a portable JSON file that can be shared.

Exports include older matches even when `CLAUDE_MEM_SEARCH_RECENCY_DAYS` limits
interactive semantic search. An empty query exports all saved history, restricted
to the selected project when `--project` is provided.

If a keyword search fails while reading saved memories, the export exits with an
error and preserves the previous file. A successful search with zero matches
writes an empty archive. If the worker is too old to report keyword read
failures, the script asks you to update it before retrying.

> **Privacy Note:** Export files contain all matching memory data in plain text. Review exports before sharing to ensure no sensitive information (API keys, passwords, private paths) is included.

### Import Script

Imports memories with **duplicate prevention**:
- Checks if each record already exists before inserting
- Skips duplicates automatically
- Preserves the owning session of each imported record
- Reports what was imported, skipped, or rejected

When a session already exists in the destination installation, its local title,
prompt, and registered memory session ID are retained. New observations and
summaries from the export attach to that existing session. If it has no memory
session ID yet, the import registers the exported ID before adding those memories.
Re-importing the same export still skips the memories already restored.

Large files are sent in multiple requests within the worker's 5 MiB JSON body
limit, measured in UTF-8 bytes. Parent sessions are imported first, and later
requests use the local session identities returned by the worker. Use a worker
from the same version as the script for this identity response. The script checks
the original input rows before writing: a row whose serialized request alone
exceeds the limit is refused without shortening its content. After importing the
parents, it checks the resolved child rows again before sending any children.
An existing destination session can have a longer identity that makes a child
row exceed the limit; this error names the row's original export index. The
already accepted parent sessions remain, since a multi-request import is not a
single transaction.

**Duplicate Detection Strategy:**
- **Sessions**: By `claude_session_id` (unique)
- **Summaries**: By `sdk_session_id` (unique)
- **Observations**: By `sdk_session_id` + `title` + `created_at_epoch` (composite)
- **Prompts**: By `claude_session_id` + `prompt_number` (composite)

## Usage

### Export Memories

```bash
# Export all Windows-related memories
npx tsx scripts/export-memories.ts "windows" windows-memories.json

# Export bug fixes
npx tsx scripts/export-memories.ts "bugfix" bugfixes.json

# Export specific feature work
npx tsx scripts/export-memories.ts "progressive disclosure" progressive-disclosure.json
```

**Parameters:**
1. `<query>` - Search query (uses hybrid semantic + full-text search)
2. `<output-file>` - Output JSON file path
3. `--project=name` - Optional: filter results to a specific project

**Example Output:**
```
🔍 Searching for: "windows"
✅ Found 54 observations
✅ Found 12 sessions
✅ Found 12 summaries
✅ Found 7 prompts

📦 Export complete!
📄 Output: windows-memories.json
📊 Stats:
   • 54 observations
   • 12 sessions
   • 12 summaries
   • 7 prompts
```

### Import Memories

```bash
# Import from an export file
npx tsx scripts/import-memories.ts windows-memories.json
```

**Parameters:**
1. `<input-file>` - Input JSON file (from export script)

**Example Output:**
```
📦 Import file: windows-memories.json
📅 Exported: 2025-12-10T23:45:00.000Z
🔍 Query: "windows"
📊 Contains:
   • 54 observations
   • 12 sessions
   • 12 summaries
   • 7 prompts

🔄 Importing sessions...
   ✅ Imported: 12, Skipped: 0
🔄 Importing summaries...
   ✅ Imported: 12, Skipped: 0
🔄 Importing observations...
   ✅ Imported: 54, Skipped: 0
🔄 Importing prompts...
   ✅ Imported: 7, Skipped: 0

✅ Import complete!
📊 Summary:
   Sessions:     12 imported, 0 skipped
   Summaries:    12 imported, 0 skipped
   Observations: 54 imported, 0 skipped
   Prompts:      7 imported, 0 skipped
```

### Re-importing (Duplicate Prevention)

If you run the import again on the same file, duplicates are automatically skipped:

```
🔄 Importing sessions...
   ✅ Imported: 0, Skipped: 12  ← All skipped (already exist)
🔄 Importing summaries...
   ✅ Imported: 0, Skipped: 12
🔄 Importing observations...
   ✅ Imported: 0, Skipped: 54
🔄 Importing prompts...
   ✅ Imported: 0, Skipped: 7
```

## Sharing Memories

### For Export Authors

1. **Export your memories:**
   ```bash
   npx tsx scripts/export-memories.ts "windows" windows-memories.json
   ```

2. **Share the JSON file** via:
   - GitHub gist
   - Project repository (`shared-memories/`)
   - Direct file transfer
   - Package in releases

3. **Document what's included:**
   - What query was used
   - What knowledge is contained
   - Who might benefit from it

### For Import Users

1. **Download the export file** to your local machine

2. **Review what's in it** (optional):
   ```bash
   cat windows-memories.json | jq '.totalObservations, .totalSessions'
   ```

3. **Import into your database:**
   ```bash
   npx tsx scripts/import-memories.ts windows-memories.json
   ```

4. **Verify import** by searching:
   ```bash
   WORKER_PORT=$(jq -r .CLAUDE_MEM_WORKER_PORT ~/.claude-mem/settings.json)
   curl "http://127.0.0.1:$WORKER_PORT/api/search?query=windows&format=index&limit=10"
   ```

## JSON Export Format

```json
{
  "exportedAt": "2025-12-10T23:45:00.000Z",
  "exportedAtEpoch": 1733876700000,
  "query": "windows",
  "totalObservations": 54,
  "totalSessions": 12,
  "totalSummaries": 12,
  "totalPrompts": 7,
  "observations": [ /* array of observation objects */ ],
  "sessions": [ /* array of session objects */ ],
  "summaries": [ /* array of summary objects */ ],
  "prompts": [ /* array of prompt objects */ ]
}
```

## Safety Features

✅ **Duplicate Prevention** - Won't re-import existing records
✅ **Explicit rejections** - Rejected rows are reported with their input positions
✅ **Read-only Export** - Export script opens database in read-only mode
✅ **Dependency Ordering** - Sessions imported before observations/summaries
✅ **Validation** - Checks database exists before starting

## Advanced Usage

### Export by Project

```bash
# Export only claude-mem project memories
npx tsx scripts/export-memories.ts "bugfix" bugfixes.json --project=claude-mem

# Export all memories for a specific project
npx tsx scripts/export-memories.ts "" all-project.json --project=my-app
```

### Export by Type

```bash
# Export only discoveries
npx tsx scripts/export-memories.ts "type:discovery" discoveries.json

# Export only bug fixes
npx tsx scripts/export-memories.ts "type:bugfix" bugfixes.json
```

### Export by Date Range

You can filter the export after exporting:

```bash
# Export all memories, then filter manually with jq
npx tsx scripts/export-memories.ts "" all-memories.json
cat all-memories.json | jq '.observations |= map(select(.created_at_epoch > 1700000000000))' > recent-memories.json
```

### Combine Multiple Exports

```bash
# Export different topics
npx tsx scripts/export-memories.ts "windows" windows.json
npx tsx scripts/export-memories.ts "linux" linux.json

# Import both
npx tsx scripts/import-memories.ts windows.json
npx tsx scripts/import-memories.ts linux.json
```

## Troubleshooting

### Database Not Found

```
❌ Database not found at: /Users/you/.claude-mem/claude-mem.db
```

**Solution:** Make sure claude-mem is installed and has been run at least once.

### Import File Not Found

```
❌ Input file not found: windows-memories.json
```

**Solution:** Check the file path. Use absolute paths if needed.

### Partial Import

If a request fails mid-way, earlier accepted rows remain imported and the script
exits with a nonzero status. Fix the reported problem and import the same file
again: duplicate prevention skips rows already restored. A rejected row also
produces a nonzero exit status, with its original position in the export file.

## Contributing Memory Sets

If you've exported valuable knowledge that others might benefit from:

1. Create a PR to the `shared-memories/` directory
2. Include a README describing what's in the export
3. Tag with relevant keywords (windows, linux, bugfix, etc.)
4. Community members can then import your knowledge!

## Examples of Useful Exports

**Windows Compatibility Knowledge:**
```bash
npx tsx scripts/export-memories.ts "windows compatibility installation" windows-fixes.json
```

**Progressive Disclosure Architecture:**
```bash
npx tsx scripts/export-memories.ts "progressive disclosure architecture token" pd-patterns.json
```

**Bug Fix Patterns:**
```bash
npx tsx scripts/export-memories.ts "bugfix error handling" bugfix-patterns.json
```

**Performance Optimization:**
```bash
npx tsx scripts/export-memories.ts "performance optimization caching" perf-tips.json
```

## Export a complete work-state list

The memory export scripts above export observations, summaries and prompts.
For the to-do lists written with `work_state_write`, use the local worker's
`GET /api/work-state/export` endpoint instead. This returns the complete
ordered history as JSON, including closed items, without the MCP text limit.

With the worker running, run this from the checkout whose work state you
want. The worker port is `CLAUDE_MEM_WORKER_PORT` in `~/.claude-mem/settings.json`;
when it isn't set, the default is `37700 + (your user id % 100)` on macOS and
Linux (on Windows, read it from `settings.json`):

```bash
WORKER_PORT=$(jq -r '.CLAUDE_MEM_WORKER_PORT // empty' ~/.claude-mem/settings.json)
WORKER_PORT=${WORKER_PORT:-$((37700 + $(id -u) % 100))}
curl --fail-with-body --get "http://127.0.0.1:$WORKER_PORT/api/work-state/export" \
  --data-urlencode "cwd=$PWD" \
  --data-urlencode 'list=release' \
  --output work-state.json
```

Omit `list` to export all public work-state lists visible to that checkout,
including its configured aliases, worktree parent and adopted projects.
Shared My Work internal records stay excluded. The response contains
`version: 1`, the primary `project`, the checkout's `projects`, the selected
`list` (or `null`) and an `entries` array. An empty history returns `entries: []`.

Each entry preserves its local `id`, origin `project`, `scope_project` when
present, `list_name`, `fields` and `created_at_epoch`. The array is already in
the worker's replay order; do not sort it by local IDs. Group by
`scope_project ?? project` and `list_name`, then apply patches in array order.
Fields without a `task` update the list state; a named `task` updates that
item. The latest value wins and `null` clears a field. This preserves the
current state and the recorded done/dropped transitions without creating new
task identities. IDs identify records in this installation, not across devices.

`work_state_read` keeps its existing text response. Its MCP result metadata
contains `claude-mem/truncated`; when true, the text also points to this export.
Exports always include recorded closures, regardless of `includeClosed`.
They are read-only and contain plaintext working state: choose the destination
deliberately and review the file before sharing. This is an export, not a
work-state import or synchronization format.