Metadata-Version: 2.5
Name: langchain-mnemoverse
Version: 0.1.0
Summary: Mnemoverse long-term memory for LangChain: a retriever, search, save and rate tools, and middleware for create_agent.
Project-URL: Homepage, https://mnemoverse.com
Project-URL: Documentation, https://mnemoverse.com/docs/api/langchain
Project-URL: Repository, https://github.com/mnemoverse/langchain-mnemoverse
Project-URL: Issues, https://github.com/mnemoverse/langchain-mnemoverse/issues
Project-URL: Changelog, https://github.com/mnemoverse/langchain-mnemoverse/blob/main/CHANGELOG.md
Author: Mnemoverse
License-Expression: MIT
License-File: LICENSE
Keywords: agents,langchain,long-term-memory,memory,middleware,mnemoverse,retriever,tools
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: langchain-core<2,>=1.4.7
Requires-Dist: mnemoverse<0.6.0,>=0.5.2
Requires-Dist: pydantic<3,>=2.9.1
Provides-Extra: agents
Requires-Dist: langchain<2,>=1.3.0; extra == 'agents'
Provides-Extra: dev
Requires-Dist: langchain-tests==1.1.9; extra == 'dev'
Requires-Dist: langchain<2,>=1.3.0; extra == 'dev'
Requires-Dist: mypy>=1.13; extra == 'dev'
Requires-Dist: pytest-asyncio<2,>=1.3.0; extra == 'dev'
Requires-Dist: pytest-socket<1,>=0.7; extra == 'dev'
Requires-Dist: pytest<10,>=9.0.3; extra == 'dev'
Requires-Dist: ruff>=0.8; extra == 'dev'
Description-Content-Type: text/markdown

# langchain-mnemoverse

Long-term memory for [LangChain](https://github.com/langchain-ai/langchain) agents and chains, backed by [Mnemoverse](https://mnemoverse.com). The memory lives in your Mnemoverse account, so what an agent saves here is also there in Claude, ChatGPT, Cursor, VS Code and any other tool connected to the same account.

- **`MnemoverseRetriever`** returns the memories relevant to a query as LangChain `Document` objects, for chains and RAG.
- **`MnemoverseToolkit`** gives an agent three tools: `mnemoverse_search`, `mnemoverse_save` and `mnemoverse_rate`. Rating a recalled memory as helpful or misleading changes what comes back next for similar questions. The tools are also importable one by one: `MnemoverseSearchTool`, `MnemoverseSaveTool`, `MnemoverseRateTool`.
- **`MnemoverseMiddleware`** recalls the memories relevant to the user's latest message once per `create_agent` run and adds them to the system message, within a time budget. If recall is slow or fails, the run goes on without memory.

This integration is maintained by the Mnemoverse team, who run the Mnemoverse memory service.

## Installation

```bash
pip install langchain-mnemoverse
```

The middleware needs the `langchain` package, which the retriever and the tools do not:

```bash
pip install "langchain-mnemoverse[agents]"
```

or with uv:

```bash
uv add "langchain-mnemoverse[agents]"
```

Without the extra, `MnemoverseMiddleware` raises an `ImportError` that says how to install it, and `from langchain_mnemoverse import *` leaves it out.

Create an API key at [console.mnemoverse.com](https://console.mnemoverse.com/dashboard/keys) and set it in the environment, or pass `api_key=`. With neither, and no client passed in (see [Settings](https://github.com/mnemoverse/langchain-mnemoverse/blob/main/README.md#settings)), building any of the components raises `ValueError`.

```bash
export MNEMOVERSE_API_KEY=mk_live_...
```

## Usage

### An agent with memory

The example below needs a chat model; it uses OpenAI through `create_agent`'s model string, which needs `pip install langchain-openai` and `OPENAI_API_KEY`. Any LangChain chat model works the same way.

```python
from langchain.agents import create_agent

from langchain_mnemoverse import MnemoverseMiddleware, MnemoverseToolkit

agent = create_agent(
    model="openai:gpt-4.1-mini",
    tools=MnemoverseToolkit(domain="user:ana").get_tools(),
    middleware=[MnemoverseMiddleware(domain="user:ana")],
    system_prompt="You are a helpful assistant.",
)

agent.invoke({"messages": [{"role": "user", "content": "Remember that I always take the window seat."}]})
result = agent.invoke({"messages": [{"role": "user", "content": "Which seat do I take on flights?"}]})
print(result["messages"][-1].content)
```

The middleware reads once at the start of every run, so a tool loop does not read again. The agent saves only when it calls `mnemoverse_save`, which its description limits to things the user asks it to remember.

### A retriever for chains

```python
from langchain_mnemoverse import MnemoverseRetriever

retriever = MnemoverseRetriever(domain="project:docs", k=4)

for doc in retriever.invoke("Which database does the ledger use?"):
    print(doc.id, doc.metadata["relevance"], doc.page_content)
```

Each document's `id` is the memory's id. `metadata` holds `atom_id`, `relevance`, `similarity`, `valence`, `importance`, `domain`, `concepts`, `created_at`, `source`, `provenance`, `metadata` (what was saved with the memory) and `superseded_by`. `ainvoke` works the same way.

### Calling a tool directly

```python
from langchain_mnemoverse import MnemoverseSearchTool

search = MnemoverseSearchTool(domain="user:ana")
message = search.invoke(
    {"type": "tool_call", "id": "1", "name": "mnemoverse_search", "args": {"query": "seat", "limit": 3}}
)
print(message.content)
```

The answer lists one memory per line with its id, which is what `mnemoverse_rate` takes.

## Which memory a run uses

Every call reads and writes one Mnemoverse **domain**, a namespace inside your account such as `user:ana` or `project:support`, of up to 100 characters.

- **One user:** pass `domain="user:ana"` to the components.
- **Several users in one app:** leave `domain` out of the tools and the middleware, and name the domain per run:

  ```python
  agent.invoke(
      {"messages": [{"role": "user", "content": "Which seat do I take?"}]},
      config={"configurable": {"mnemoverse_domain": f"user:{user_id}"}},
  )
  ```

  The middleware and the tools also read `mnemoverse_domain` from the agent run's `context` (`agent.invoke(..., context=...)`, a mapping key or an attribute). If a run names it in both places, the two must agree: when they differ, the tools answer the model with an error and the middleware runs without memory, so one run never reads one user's memory and writes another's. The retriever takes `domain=` per call, `retriever.invoke(query, domain=f"user:{user_id}")`, or as a configurable field: `retriever.configurable_fields(domain=ConfigurableField(id="mnemoverse_domain"))`.

Take `user_id` from your app's signed-in user on the server, never from client input or model output. The domain is never an argument the model can set. A run with no domain gets no memory: the tools answer the model with an error and the middleware logs one warning; it is never served from another domain.

A domain separates how memory is used, not who may read it: the API key can read every domain of its account. Give each app its own key, so you can revoke one without the others. A shared room's address, `xroom:<room_id>`, works as a domain. How Mnemoverse handles keys and data: [mnemoverse.com/docs/api/security](https://mnemoverse.com/docs/api/security).

## Settings

Every component takes `api_key` (default: `MNEMOVERSE_API_KEY`), `base_url` (default `https://core.mnemoverse.com`), and `client` and `async_client` for a ready `mnemoverse.MnemoClient` and `AsyncMnemoClient`. Sync calls (`invoke`) use the sync client and async calls (`ainvoke`) the async one.

- **Neither passed:** the package opens its own clients from the key. Components built with the same key and `base_url` share them, including the copies `configurable_fields` makes for each call: one sync client, and one async client per event loop, so a component also works from several `asyncio.run()` calls.
- **Only `client`:** async calls run it in a worker thread.
- **Only `async_client`:** sync calls raise `ValueError` unless you also pass `api_key=`. They never fall back to `MNEMOVERSE_API_KEY`, which may belong to another account than your client.
- An `AsyncMnemoClient` you pass in works on one event loop: SDK 0.5.2 binds its connections to the loop that first used it, and from a second `asyncio.run()` its calls fail with "Event loop is closed". Pass one per loop, or let the package open its own.

Clients you pass in are never closed by this package.

| `MnemoverseRetriever` | Default | What it does |
|---|---|---|
| `domain` | required | The domain every call reads; override per call with `invoke(query, domain=...)` |
| `k` | 4 | Documents per call, 1 to 500; override per call with `invoke(query, k=...)` |
| `min_relevance` | 0.3 | Lowest relevance, 0 to 1, of a returned memory |

| Tools and `MnemoverseToolkit` | Default | What it does |
|---|---|---|
| `domain` | `None` | The domain every call uses; without it, the run's `config` or `context` |
| `save` (toolkit) | `True` | Include `mnemoverse_save` |
| `rate` (toolkit) | `True` | Include `mnemoverse_rate` |
| `limit` (search argument, set by the model) | 5 | Memories per search, 1 to 50 |

There is no delete tool. The toolkit's tools share one pair of clients.

| `MnemoverseMiddleware` | Default | What it does |
|---|---|---|
| `domain` | `None` | The domain every run reads; without it, the run's `context` or `config` |
| `top_k` | 5 | Memories per recall, 1 to 500 |
| `min_relevance` | 0.3 | Lowest relevance, 0 to 1, of a recalled memory. A broad message ("what do you know about me?") scores lower than a specific one; lower this if broad messages recall nothing |
| `recall_timeout` | 8.0 | Seconds the run waits for recall before it goes on without it; also how long it waits for a capture. A call that has started is not cancelled: it finishes in the background, with `ainvoke` only while the event loop keeps running. `asyncio.run()` cancels it when it returns, and the middleware logs that it did not complete |
| `context_prompt` | a heading and one line | Text above the recalled memories. The default line tells the model the memories are reference data, not instructions; keep it if you write your own |
| `capture` | `False` | Save the user's messages of each run as one memory. Prefer the `mnemoverse_save` tool, which saves only what the user asks to keep |

With `invoke`, the middleware runs each recall and capture on one of four worker threads, each with a client of its own when the middleware opens its clients from the key. A slow call therefore never holds up the agent's tool calls, and a call still waiting for a worker when its run stops waiting is dropped, not sent later. A `MnemoClient` passed as `client=` runs one call at a time, so the middleware gives it a single worker; do not pass the tools the same client, because a recall that outlives `recall_timeout` keeps it busy and the next tool call waits for it. For a server with many users at once, call the agent with `ainvoke`, where calls run concurrently on the async client.

At most `k`, `limit` or `top_k` memories are used, also when the server returns more.

Memory text is written by other people and tools too: members of a shared room, the other AI tools on the account. In the middleware's memory block and in the search tool's answer each memory is therefore one line, with line breaks turned into spaces and `<` and `>` into `‹` and `›`, so a memory cannot close the block or pose as another memory with an id the server never returned. The retriever's documents keep the text unchanged.

## Errors

- **Retriever:** SDK errors propagate unchanged (`MnemoAuthError`, `MnemoRateLimitError`, `MnemoUnavailableError`, `MnemoError`), so a chain fails loudly; wrap it with `.with_fallbacks(...)` if you prefer. An invalid domain or `k` raises `ValueError`.
- **Tools:** every failure reaches the model as a `ToolMessage` with `status="error"`, so the agent can tell the user what happened: a refused call with the server's reason, and arguments that fail the schema (such as `limit` over 50) with the field and why. This holds inside `create_agent` and when you invoke a tool yourself with a `ToolCall`.
- **Middleware:** memory never breaks a run. A timeout or a failed call is logged under the `langchain_mnemoverse` logger and the run continues without memory. A refusal is logged with the server's reason and stops recall, or capture, for 15 minutes: a 401 (the key is refused) for every domain, a 403 (the key may not use this domain, such as a room it is not a member of) for that domain only. After the pause the middleware tries again.
- **Logs:** nothing this package logs contains the key, a query or memory text. Domains often carry a user id, so a refused domain is logged as `#` and the first 12 hex digits of its SHA-256 (`hashlib.sha256(domain.encode()).hexdigest()[:12]`), not by name.

Input is kept within the API limits before any request: queries and memories are stripped of surrounding space, queries are then cut to their last 5,000 characters, the save tool refuses a memory over 10,000 characters, and capture saves the first 10,000. A blank query or memory makes no request: the retriever returns no documents, and the search and save tools answer the model with an error.

## Cost

Each retriever call and each tool call is one API call, which counts as at most one query against your plan: the SDK retries a call that failed for a passing reason (a timeout, a lost connection, a rate limit or a server error) up to three more times, and only successful requests count. A blank query or memory, a save refused for its length and a rating with no ids make no call. The middleware makes at most one read per agent run, and one write more with `capture=True`. Reads, writes and ratings count together against your plan's daily query limit: the Free plan allows 1,000 queries a day. Plans and limits are on [mnemoverse.com/pricing](https://mnemoverse.com/pricing).

## Examples

[`examples/agent_with_memory.py`](https://github.com/mnemoverse/langchain-mnemoverse/blob/main/examples/agent_with_memory.py) is an agent with the middleware and the toolkit, and [`examples/rag_retriever.py`](https://github.com/mnemoverse/langchain-mnemoverse/blob/main/examples/rag_retriever.py) a retriever in a small chain. Both need `MNEMOVERSE_API_KEY` and `OPENAI_API_KEY`:

```bash
pip install "langchain-mnemoverse[agents]" langchain-openai
python examples/agent_with_memory.py
```

## Compatibility

Tested with the `mnemoverse` Python SDK 0.5.2, `langchain-core` 1.8.4, `langchain` 1.5.2 and `langchain-tests` 1.1.9 on Python 3.11, 3.12, 3.13 and 3.14, and with the lowest supported versions, `langchain-core` 1.4.7, `langchain` 1.3.0 and `pydantic` 2.9.1, on Python 3.11. The tests use fake SDK clients; a live suite runs on demand. It needs SDK 0.5.2 or later: in earlier versions a call refused with a client error (such as 401, 403 or a quota 429) while the client was probing the service after an outage could leave that client refusing every call until the process restarted.

## Links

- Python SDK: [mnemoverse on PyPI](https://pypi.org/project/mnemoverse/), [docs](https://mnemoverse.com/docs/api/python-sdk)
- Pricing and limits: [mnemoverse.com/pricing](https://mnemoverse.com/pricing)
- Security and data handling: [mnemoverse.com/docs/api/security](https://mnemoverse.com/docs/api/security)
- Issues: [github.com/mnemoverse/langchain-mnemoverse/issues](https://github.com/mnemoverse/langchain-mnemoverse/issues/new)

## License

MIT, as LangChain. See [LICENSE](https://github.com/mnemoverse/langchain-mnemoverse/blob/main/LICENSE).
