Minimal example
Use these values on a trusted local machine or server. Keep project credentials out of browser code.
python
import os
from typing import TypedDict
from langchain_openai import ChatOpenAI
from langgraph.graph import START, END, StateGraph
from langchain_memorae import MeMoraeMemory
# Set MEMORAE_API_KEY, MEMORAE_SUBJECT_ID, OPENAI_API_KEY and
# MEMORAE_BASE_URL=https://memora-singapore.onrender.com on your server.
memory = MeMoraeMemory(
api_key=os.environ["MEMORAE_API_KEY"],
base_url=os.environ["MEMORAE_BASE_URL"],
subject_id=os.environ["MEMORAE_SUBJECT_ID"],
namespace=os.getenv("MEMORAE_NAMESPACE", "default"),
)
llm = ChatOpenAI()
class State(TypedDict):
input: str
history: str
output: str
def recall(state: State) -> dict:
return memory.load_memory_variables({"input": state["input"]})
def respond(state: State) -> dict:
reply = llm.invoke([
("system", "Relevant user memories:\n" + state["history"]),
("human", state["input"]),
]).content
return {"output": reply}
def save(state: State) -> dict:
memory.save_context({"input": state["input"]}, {"output": state["output"]})
return {}
builder = StateGraph(State)
for name, node in (("recall", recall), ("respond", respond), ("save", save)):
builder.add_node(name, node)
builder.add_edge(START, "recall")
builder.add_edge("recall", "respond")
builder.add_edge("respond", "save")
builder.add_edge("save", END)
graph = builder.compile()
print(graph.invoke({"input": "What do you remember about me?"})["output"])Check that it works
- Invoke the graph with an authenticated user’s stable subject ID configured on the memory instance.
- Confirm the recall node can return an empty history when there is no relevant memory.
- After the save node runs, allow asynchronous processing before checking retrieval.
- Inspect source citations in the dashboard or returned retrieval documents.