# Make Platform Memory Operations Smarter

**Prerequisites**

- Platform workspace with API key
- Python 3.10+ and Node.js 18+

Need a refresher on the core concepts first? Review the [Add Memory](https://docs.mem0.ai/core-concepts/memory-operations/add) overview, then come back for the advanced flow.

## Install and authenticate

- Python

1. Install the SDK
   
   ```
   pip install mem0ai
   ```

2. Export your API key
   
   ```
   export MEM0_API_KEY="sk-platform-..."
   ```

3. Create an async client
   
   ```
   import os
   from mem0 import AsyncMemoryClient
   
   memory = AsyncMemoryClient(api_key=os.environ["MEM0_API_KEY"])
   ```

- TypeScript

1. Install the OSS SDK
   
   ```
   npm install mem0ai
   ```

2. Load your API key
   
   ```
   export MEM0_API_KEY="sk-platform-..."
   ```

3. Instantiate the client
   
   ```
   import MemoryClient from 'mem0ai';
   
   const memory = new MemoryClient({ apiKey: process.env.MEM0_API_KEY! });
   ```

## Add memories with metadata

- Python

1. Record conversations with metadata
   
   ```
   conversation = [\
       {"role": "user", "content": "I'm Morgan, planning a 3-week trip to Japan in May."},\
       {"role": "assistant", "content": "Great! I'll track dietary notes and cities you mention."},\
       {"role": "user", "content": "Please remember I avoid shellfish and prefer boutique hotels in Tokyo."},\
   ]
   
   result = await memory.add(
       conversation,
       user_id="traveler-42",
       metadata={"trip": "japan-2025", "preferences": ["boutique", "no-shellfish"]},
       run_id="planning-call-1",
   )
   ```

2. Capture context-rich memories
   
   ```
   const conversation = [\
     { role: "user", content: "I'm Morgan, planning a 3-week trip to Japan in May." },\
     { role: "assistant", content: "Great! I'll track dietary notes and cities you mention." },\
     { role: "user", content: "Please remember I avoid shellfish and love boutique hotels in Tokyo." },\
   ];
   
   const result = await memory.add(conversation, {
     userId: "traveler-42",
     metadata: { trip: "japan-2025", preferences: ["boutique", "no-shellfish"] },
     runId: "planning-call-1",
   });
   ```

Successful calls return memories tagged with the metadata you passed. In the dashboard, verify the `trip=japan-2025` tag exists on the new memory.

## Retrieve and refine

- Python

1. Filter by metadata + reranker
   
   ```
   matches = await memory.search(
       "Any food alerts?",
       filters={"user_id": "traveler-42", "metadata.trip": "japan-2025"},
       rerank=True,
   )
   ```

2. Update a memory inline
   
   ```
   await memory.update(
       memory_id=matches["results"][0]["id"],
       text="Morgan avoids shellfish and prefers boutique hotels in central Tokyo.",
   )
   ```

- TypeScript

1. Search with metadata filters
   
   ```
   const matches = await memory.search("Any food alerts?", {
     filters: { user_id: "traveler-42", "metadata.trip": "japan-2025" },
     rerank: true,
   });
   ```

2. Apply an update
   
   ```
   await memory.update(matches.results[0].id, {
     text: "Morgan avoids shellfish and prefers boutique hotels in central Tokyo.",
   });
   ```

## Clean up

- Python

1. Delete scoped memories
   
   ```
   await memory.delete_all(user_id="traveler-42", run_id="planning-call-1")
   ```

- TypeScript

1. Remove the run
   
   ```
   await memory.deleteAll({ userId: "traveler-42", runId: "planning-call-1" });
   ```

## Quick recovery

- Empty results with filters: log `filters` values and confirm metadata keys match (case-sensitive).

Metadata keys become part of your filtering schema. Stick to lowercase snake_case (`trip_id`, `preferences`) to avoid collisions down the road.

[**Tune Metadata Filtering**](https://docs.mem0.ai/open-source/features/metadata-filtering)

[**Explore Reranker Search**](https://docs.mem0.ai/open-source/features/reranker-search)
