AI Hints
Home›LangGraph Tutorial›Long-Term Memory
LangGraph · Lesson 75 of 120

Namespaces

Organising memories.

⏱ 2 min read▶ Video at 1:20:30🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

A namespace is a tuple, like a folder path. It keeps memories organised, and separate. Most apps put the user's ID in the namespace, so each customer's memories live in their own folder.

Namespaces
From the video: Namespaces
  • A tuple, like a path
  • Organised and separate
  • User ID inside

One folder per customer

We save memories for Sam and Priya, each under their own namespace. A third namespace holds shop wide settings. Searching one namespace only returns what's inside it. Run it. Sam sees only Sam's memories, and Priya only hers.

namespaces.pypython
from langgraph.store.memory import InMemoryStore

store = InMemoryStore()
store.put(("memories", "sam"), "1", {"fact": "loves sci-fi"})
store.put(("memories", "sam"), "2", {"fact": "prefers paperbacks"})
store.put(("memories", "priya"), "1", {"fact": "reads poetry"})
store.put(("shop", "settings"), "story_time", {"day": "Saturday"})

for user in ["sam", "priya"]:
    facts = [item.value["fact"] for item in store.search(("memories", user))]
    print(user, "->", facts)
Output
sam -> ['loves sci-fi', 'prefers paperbacks']
priya -> ['reads poetry']

Namespace design

Put the user ID early in the namespace, so it's easy to keep users apart, and delete one user's data on request. Use a second part for the kind of memory, like preferences, or facts. And keep keys short and stable.

keyvalue
User IDkeeps people apart
Kindpreferences, facts
Deleteone user's folder
Keysshort, stable

Summary

Namespaces are tuples that organise memories, usually one per user. Searching a namespace returns only its items. Next, using the store inside graph nodes.

Key takeaways
  • Tuple namespaces
  • One per user
  • Search one folder
Next up: The Store in Nodes — read and write