> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anonalabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain & LangGraph

> Persistent memory for LangChain chains and LangGraph agents.

Anona gives a LangChain chain or LangGraph agent memory that survives the
process. You bring no LLM key and run no vector database. Anona is managed,
and every call is scoped so one space can serve many end users without their
memories mixing.

```bash theme={null}
pip install 'anona[langchain]'
```

## LangGraph agents

```python theme={null}
from anona.integrations import MemoryBridge
from anona.integrations.langchain import AnonaMemory
from langchain.agents import create_agent

bridge = MemoryBridge(
    api_key="anona_live_...",
    space_id="support-bot",
    user_id="customer-42",   # this user's memories only
)

agent = create_agent(model="gpt-4o", middleware=[AnonaMemory(bridge=bridge)])
```

The context block is fetched once per turn and injected as a system message
before every model call in that turn, reused across each step of a
tool-calling loop rather than re-fetched. If you also pass `system_prompt` to
`create_agent`, both survive: your instructions first, the memory block after.
The completed turn as a whole, not each intermediate step, is stored once the
agent run finishes. Nothing else changes.

<Note>
  That per-turn reuse is implemented as a declared field on LangGraph's own
  graph state, which is exactly what a checkpointer persists. If you compile
  `create_agent(..., checkpointer=...)` with a durable checkpointer (SQLite,
  Postgres, ...), the retrieved memory text is written into *your* checkpoint
  store verbatim (once per checkpoint saved during that run), not just held
  in process memory. That copy lives outside Anona's storage and retention:
  deleting or expiring a memory on Anona's side does not remove it from
  checkpoints already written. Expected given what the cache is for, not a
  bug, just worth knowing if your own checkpoint store needs to honor a
  retention or deletion policy.
</Note>

## Retrieval chains

```python theme={null}
from anona.integrations.langchain import AnonaRetriever

retriever = AnonaRetriever(bridge=bridge)
docs = await retriever.ainvoke("what did we decide about pricing?")
```

`AnonaRetriever` is async-only: call `ainvoke()` (shown above), not `invoke()`.
The sync form raises `NotImplementedError` rather than silently blocking an
event loop. It is the one place across any of these six adapters that raises into
the caller instead of failing open. Every other failure mode on this page
(Anona unreachable, no matches, ...) still returns normally with an empty
result.

## Scoping

`user_id`, `agent_id` and `session_id` on the bridge isolate memory inside one
space. A memory written under `user_id="customer-42"` is only ever returned to
a query carrying the same `user_id`.

## Turning off writes

```python theme={null}
AnonaMemory(bridge=bridge, record=False)
```

Keeps recall, skips storing new turns.

## Why not a LangGraph Store?

A `BaseStore` is namespaced key-value: `get(namespace, key)` returns one exact
record. Anona is search: you ask a question and get ranked, deduplicated,
token-budgeted context. A partial Store implementation would break LangGraph's
built-in memory tools at runtime, so we ship the two shapes that fit instead.

## Failure behaviour

If Anona is unreachable, your agent keeps running without memory. Memory
failures are logged, never raised.
