Memory

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Memory

Think about how important memory is in your daily life. Where did you put your keys? What’s your email password? What’s the name of that friend you haven’t seen in ten years?

Input/Output-Based Memory

The most basic form of preserving state is simply passing output from one node as input to another. While this might not be considered “memory” by most people, in a sense, you’re persisting the state of the previous node in the graph, if not any of the nodes before it. This is the style of persistence you use when you have a plain Graph object:

def function_1(input):
  return "output"

def function_2(input):
  return "output"

def function_3(input):
  return "output"

graph = Graph()
graph.add_node("node_1", function_1)
graph.add_node("node_2", function_2)
graph.add_node("node_3", function_3)
graph.add_edge("node_1", "node_2")
graph.add_edge("node_2", "node_3")

State-Based Memory

A more powerful version of memory is to define a State object and provide it to a StateGraph. This allows keeping a record of entire AI-human conversations through a message list. If that’s not enough, you can add as many properties to the State object as needed. This object is available to every node in the graph. The following is an abbreviated example:

class MyState(TypedDict):
  messages: Annotated[list, add]
  count: int

def function_1(state):
  return state

def function_2(state):
  return state

def function_3(state):
  return state

graph = StateGraph(MyState)
graph.add_node("node_1", function_1)
graph.add_node("node_2", function_2)
graph.add_node("node_3", function_3)
graph.add_edge("node_1", "node_2")
graph.add_edge("node_2", "node_3")

Checkpoints

The real power of LangGraph comes with checkpoints. Checkpoints remember the state at every step of the graph execution. This is like giving a human the superpower of a photographic memory. Plus, a time machine. Plus, the ability to change the past.

from langgraph.checkpoint.memory import MemorySaver

graph = StateGraph(State)
memory = MemorySaver()
app = graph.compile(checkpointer=memory)
memory = MemorySaver()
app = graph.compile(checkpointer=memory)

initial_input = {"messages": ["hello"]}
thread = {"configurable": {"thread_id": "1"}}
app.invoke(initial_input, thread)
thread = {"configurable": {"thread_id": "2"}}
app.invoke(initial_input, thread)
See forum comments
Download course materials from Github
Previous: Introduction Next: Memory Demo