---
name: mem0-agent-memory
description: "Persistent agent memory with Mem0 — fact extraction, add()/search() retrieval, per-user/session scoping in the LLM loop. Wire Mem0 into an agent's call loop for scoped long-term memory: Memory.add() for LLM-based fact extraction and consolidation, search() for semantic retrieval, and user_id/agent_id/run_id scoping. Covers get_all/update/delete and history, metadata filters, the self-hosted Memory vs hosted MemoryClient (Platform) split, vector-store and LLM/embedder config, graph memory for entity relations, and injecting retrieved memories into the system prompt."
license: MIT
---

# Mem0 Memory Layer

Give your agent a memory that survives across turns, sessions, and users. This skill wires [Mem0](https://github.com/mem0ai/mem0) into your agent's call loop so it extracts durable facts from conversations, stores them in a vector store, and retrieves the relevant ones before each response. It's for Python developers building chatbots, support agents, or assistants that need to remember who the user is and what they said last week — without stuffing the whole history into the prompt.

## When to use it

- A user tells your agent "I'm vegetarian" or "I use TypeScript" and you want that recalled in future sessions.
- You're building a multi-tenant assistant and need memories isolated per `user_id`.
- Your context window is overflowing with chat history and you'd rather retrieve only the facts that matter for the current query.
- You need an audit trail of how a stored fact changed over time.

## How it works

The core is two calls. `Memory.add(messages, user_id=...)` runs an LLM pass that extracts atomic facts from the turn, then reconciles them against existing memories — adding, updating, or deleting as needed (it won't store five copies of "likes coffee"). `Memory.search(query, user_id=...)` does semantic retrieval and returns ranked memories to inject into your prompt.

Scope everything with `user_id`, `agent_id`, and `run_id` (session). Round out the lifecycle with `get_all()`, `get(memory_id)`, `update()`, `delete()`/`delete_all()`, and `history(memory_id)` to inspect changes. `search()` and `get_all()` accept metadata `filters` for narrowing by your own tags.

Two backends share the same API: self-hosted `Memory` (you bring the vector store, LLM, and embedder via a config dict) and hosted `MemoryClient` (managed by Mem0, API-key auth).

## Usage

```bash
curl -fsSL --create-dirs -o ~/.claude/skills/mem0-agent-memory/SKILL.md https://claudeskil.com/skills/mem0-agent-memory/SKILL.md
pip install mem0ai
```

```python
from mem0 import Memory

m = Memory()  # or Memory.from_config(cfg) to pick vector store / LLM

# After a turn, persist facts
m.add(
    [{"role": "user", "content": "I'm allergic to peanuts"}],
    user_id="alex",
)

# Before the next response, retrieve and inject
hits = m.search("what can this user eat?", user_id="alex", limit=3)
context = "\n".join(h["memory"] for h in hits["results"])
```

## Tips & gotchas

- `add()` makes its own LLM calls for extraction — it has latency and token cost. Call it after responding, not in the hot path, or batch it.
- Don't store raw transcripts. Mem0 distills facts; passing huge blobs wastes the extraction budget. Pass the recent turns.
- Migrating from `MemoryClient` to self-hosted `Memory`? The method names match, but configure the same embedder or your existing vectors won't match queries.
- Always pass at least one scope id. Memories added without `user_id`/`agent_id`/`run_id` are hard to retrieve cleanly later.

## FAQ

**Self-hosted vs hosted — which?** Use `Memory` (self-hosted) when you need data residency or your own Qdrant/pgvector/Chroma. Use `MemoryClient` when you'd rather not run infrastructure.

**Does it support async?** Yes — `AsyncMemory` mirrors the sync API for `add`/`search`/etc.

**How are conflicting facts handled?** `add()` detects contradictions and updates the existing memory rather than appending; use `history()` to see the prior value.
