Remembering Preferences
Across threads.
Here's the payoff. A customer tells the bot something in one conversation. Days later, in a brand new conversation, the bot still knows. That's long term memory, working across threads.

- Tell it once
- New conversation later
- It still knows
Memory across conversations
When a message starts with remember, the chat node saves the rest as a fact, under the user's namespace. Every reply includes the saved facts in the system prompt. We use two different thread IDs, so these are separate conversations. Run it. In the second conversation, the bot recommends a book that fits what Sam said in the first.
from langchain.chat_models import init_chat_model
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import MessagesState, StateGraph, START
from langgraph.runtime import Runtime
from langgraph.store.memory import InMemoryStore
from ctx import Ctx
model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
def chat(s: MessagesState, runtime: Runtime[Ctx]):
ns, text = ("memories", runtime.context.user_id), s["messages"][-1].text
if text.lower().startswith("remember"):
runtime.store.put(ns, str(len(runtime.store.search(ns))), {"fact": text[9:]})
facts = [m.value["fact"] for m in runtime.store.search(ns)]
return {"messages": [model.invoke([("system", f"Customer facts: {facts}. One sentence.")] + s["messages"])]}
b = StateGraph(MessagesState, context_schema=Ctx); b.add_node(chat); b.add_edge(START, "chat")
graph = b.compile(checkpointer=InMemorySaver(), store=InMemoryStore())
for thread, text in [("monday", "Remember: I love space adventures."), ("friday", "Suggest one book. Title only.")]:
out = graph.invoke({"messages": [{"role": "user", "content": text}]}, {"configurable": {"thread_id": thread}}, context=Ctx("sam"))
print(f"[{thread}] {text} -> {out['messages'][-1].text.strip()}")[monday] Remember: I love space adventures. -> I've got that stored in my memory banks, commander—ready to assist you with any thrilling space adventure you want to embark on! [friday] Suggest one book. Title only. -> *Project Hail Mary*
Also uses ctx.py
from dataclasses import dataclass
@dataclass
class Ctx:
user_id: strDeciding what to remember
Here we used a simple keyword. Real apps often let the model decide what's worth remembering, with a tool, or a background step after each conversation. Remember facts, not whole chats. And let customers see, and delete, what's stored.
| key | value |
|---|---|
| Simple | a keyword |
| Smarter | model decides |
| Store | facts, not chats |
| Respect | view and delete |
Summary
Facts saved in the store under the user's namespace are available in every future conversation. Next, finding memories by meaning.
Key takeaways- Save facts
- New thread, same user
- Facts in the prompt