LangChain
Give a LangChain agent a memory of each of your users. Middleware puts what is known in front of the model and saves what happened when the agent is done; or a recall tool lets the agent look things up itself. Both read the user from the agent's runtime context, so one agent serves every user.
With the package
langchain-geniffy is one piece of middleware, ready made. Before every call the model gets the user's
briefing: where things stand, what is due, the rules that apply, what happened, and what is
known that bears on their message. When the agent has its answer, the run is saved, tool calls included, into one
memory for the whole conversation.
pip install langchain langchain-anthropic langchain-geniffyuv add langchain langchain-anthropic langchain-geniffyfrom dataclasses import dataclass
from langchain.agents import create_agent
from langchain_geniffy import GeniffyMemory
from langgraph.checkpoint.memory import InMemorySaver
@dataclass
class User:
id: str
agent = create_agent(
"anthropic:claude-opus-5-5",
system_prompt="You are a helpful assistant.",
middleware=[GeniffyMemory(space=lambda runtime: f"user_{runtime.context.id}")],
context_schema=User,
checkpointer=InMemorySaver(),
)Run it with the conversation as the thread, {"configurable": {"thread_id": chat.id}}, and the user from your
own sign-in. The briefing goes in after your own system prompt, and in a run with tools, Geniffy is asked once,
not once per step. Each run sends only what is new; where saving got to is kept with the thread, so nothing is sent
twice, even after a restart. project= keeps the briefing to one project. geniffy_tools(space=...) gives the
agent recall and remember instead, and GeniffyRetriever returns the matching memories as Documents for a
chain. The package is open source: Geniffy/langchain-geniffy.
The rest of this page builds a simpler version with the geniffy SDK itself, for when you want to place the
memory yourself: what bears on each message, and each exchange saved.
Install
pip install langchain langchain-anthropic geniffyuv add langchain langchain-anthropic geniffySet ANTHROPIC_API_KEY, and GENIFFY_API_KEY from API keys in the Geniffy app. Any chat model
LangChain supports works the same way.
Remember each user
from dataclasses import dataclass
from geniffy import Geniffy
from langchain.agents import AgentState, create_agent
from langchain.agents.middleware import ModelRequest, after_agent, dynamic_prompt
from langgraph.runtime import Runtime
geniffy = Geniffy() # reads GENIFFY_API_KEY
@dataclass
class User:
id: str
@dynamic_prompt
def with_memory(request: ModelRequest) -> str:
mem = geniffy.space(f"user_{request.runtime.context.id}")
asked = next(m for m in reversed(request.messages) if m.type == "human")
context = mem.context(asked.text) # what is known that bears on the message
return f"You are a helpful assistant.\n\n<memory>\n{context}\n</memory>"
@after_agent
def save_exchange(state: AgentState, runtime: Runtime[User]) -> None:
asked = next(m for m in reversed(state["messages"]) if m.type == "human")
answer = state["messages"][-1]
geniffy.space(f"user_{runtime.context.id}").memories.add(messages=[
{"role": "user", "content": asked.text},
{"role": "assistant", "content": answer.text},
])
agent = create_agent(
"anthropic:claude-opus-5-5",
middleware=[with_memory, save_exchange],
context_schema=User,
)Pass the user with each call, from your own sign-in:
result = agent.invoke(
{"messages": [{"role": "user", "content": "Who signs the Lumen renewal?"}]},
context=User(id=user.id),
)
print(result["messages"][-1].text)with_memory runs before each call to the model, so the model sees what is known about this user, each line
with where it came from. When nothing is known, the block says so in one sentence, so the model says it
doesn't know instead of guessing. save_exchange runs once, when the agent has its answer.
Let the agent look things up
To let the agent decide when to look something up, give it a recall tool. The tool reads the user from the
same runtime context, so the model never sees or chooses whose memory it reads.
from dataclasses import dataclass
from geniffy import Geniffy
from langchain.agents import AgentState, create_agent
from langchain.agents.middleware import after_agent
from langchain.tools import ToolRuntime, tool
from langgraph.runtime import Runtime
geniffy = Geniffy()
INSTRUCTIONS = ("You are a helpful assistant. Use recall before answering anything "
"that depends on what the user said before.")
@dataclass
class User:
id: str
@tool
def recall(query: str, runtime: ToolRuntime[User]) -> str:
"""Look up what is known about the user, with where it came from."""
return geniffy.space(f"user_{runtime.context.id}").context(query)
@after_agent
def save_exchange(state: AgentState, runtime: Runtime[User]) -> None:
asked = next(m for m in reversed(state["messages"]) if m.type == "human")
answer = state["messages"][-1]
geniffy.space(f"user_{runtime.context.id}").memories.add(messages=[
{"role": "user", "content": asked.text},
{"role": "assistant", "content": answer.text},
])
agent = create_agent(
"anthropic:claude-opus-5-5",
tools=[recall],
system_prompt=INSTRUCTIONS,
middleware=[save_exchange],
context_schema=User,
)The tool answers with context(), so the agent reads the same lines, with their sources, that the system
prompt would hold, and the same sentence when nothing is known.
Memory across conversations
A LangChain checkpointer keeps one conversation going. Geniffy is what the agent knows about the user across all of them: what they said last month, in another app, or in a file you added. Use both.