Metadata-Version: 2.5
Name: langchain-recallic
Version: 0.1.0
Summary: Recallic as long-term memory for LangChain and LangGraph agents: self-hosted, no LLM calls for memory, says when it has no record.
Project-URL: Homepage, https://github.com/Recallic/Recallic/tree/main/integrations/langchain
License-Expression: Apache-2.0
Requires-Python: >=3.10
Requires-Dist: langchain-core>=1.0
Requires-Dist: langchain>=1.0
Requires-Dist: recallic>=0.3.0
Description-Content-Type: text/markdown

# langchain-recallic: Recallic memory for LangChain and LangGraph

Long-term memory for [LangChain](https://www.langchain.com) and [LangGraph](https://www.langchain.com/langgraph)
agents, backed by a self-hosted [Recallic](https://github.com/Recallic/Recallic) server.

* **No LLM calls for memory.** Storing and searching use Recallic's local models.
* **Across threads and sessions.** Each turn, what this user said before that bears on their latest message is
  recalled, whatever thread it was said in.
* **Honest.** When nothing answers it, the model is told there is no record instead of being handed near-misses to
  guess from.
* **Your data stays on your server**, separated per user and per agent.

## Install

```bash
pip install langchain-recallic
```

Run Recallic ([quickstart](../../docs/quickstart.md)) and create a key with role `service`, which acts for each of
your users (or use a user's own key and leave `user_id` out):

```bash
python -m backend.tools.keys create --user my-langchain-app --role service
```

## Automatic memory: `RecallicMiddleware`

For agents built with `create_agent` (LangChain 1.x; it runs on LangGraph):

```python
import os

from langchain.agents import create_agent
from langchain_recallic import RecallicMiddleware

memory = RecallicMiddleware(os.environ["RECALLIC_SERVICE_KEY"], base_url="http://localhost:8000",
                            mode="balanced")                       # or "cautious" / "helpful"
agent = create_agent("openai:gpt-5-mini", tools=[...], middleware=[memory])

alice = {"configurable": {"user_id": "alice"}}
agent.invoke({"messages": [{"role": "user", "content": "I moved to Pune in March."}]}, config=alice)

# days later, another thread
agent.invoke({"messages": [{"role": "user", "content": "Which city did I move to?"}]}, config=alice)  # "Pune."
```

Before each model call it recalls on the user's latest message and adds the memories to the system message (your
own system prompt is kept), or tells the model there is no record. Then it stores that message. In a tool loop the
recall runs once per user message.

| Parameter | Default | What it does |
|---|---|---|
| `user_id` | `None` | A fixed user. `None` reads `user_id` per run from the run's `context` (with `context_schema`) or `config["configurable"]`. |
| `agent_id` | `langchain` | Recallic agent id; each agent reads back what it wrote (and what any agent shared). |
| `mode` | server's `recall.mode` | `cautious` checks every answer twice; `helpful` answers more counting, ordering and "how long between" questions. |
| `limit` | 5 | Memories given to the model per turn. |
| `store_user_messages` | `True` | Store the user's latest message each turn (after recalling). |
| `tell_model_on_decline` | `True` | When nothing answers, tell the model not to guess about the user's past. |

## Retriever: `RecallicRetriever`

For chains, RAG pipelines and your own LangGraph nodes:

```python
from langchain_recallic import RecallicRetriever

retriever = RecallicRetriever(api_key=os.environ["RECALLIC_SERVICE_KEY"], user_id="alice", k=5)
docs = retriever.invoke("Where do I work?")    # [] when Recallic has no memory that answers it
```

## Tools: `recallic_tools()`

When the agent should decide itself when to look things up or save them:

```python
from langchain_recallic import recallic_tools

tools = recallic_tools(os.environ["RECALLIC_SERVICE_KEY"])    # recall_memory(query), save_memory(fact)
agent = create_agent("openai:gpt-5-mini", tools=tools)
agent.invoke({"messages": [...]}, config={"configurable": {"user_id": "alice"}})
```

Notes:

* `format_memories(recall_result)` turns any Recallic recall result into the text the middleware gives the model.
* Needs Recallic server 0.2.5 or newer. Tested with langchain 1.5 and langgraph 1.2.

## Tests

Against a running Recallic: `RECALLIC_URL=http://127.0.0.1:8000 RECALLIC_SERVICE_KEY=rx_... pytest tests`.
They include real `create_agent` runs with a scripted model.

Licence: Apache-2.0.
