LangGraph helps agents remember and use past information to make better decisions. It keeps track of what happened before so the agent can act smarter over time.
LangGraph for stateful agents in Agentic AI
class LangGraph: def __init__(self): self.nodes = {} self.edges = {} def add_node(self, node_id, state): self.nodes[node_id] = state def add_edge(self, from_node, to_node, action): if from_node not in self.edges: self.edges[from_node] = [] self.edges[from_node].append((to_node, action)) def get_state(self, node_id): return self.nodes.get(node_id, None) def update_state(self, node_id, new_state): if node_id in self.nodes: self.nodes[node_id] = new_state def next_actions(self, node_id): return self.edges.get(node_id, [])
This class stores states as nodes and actions as edges connecting them.
It allows updating states and querying possible next actions from a state.
lang_graph = LangGraph() lang_graph.add_node('start', {'mood': 'neutral'}) lang_graph.add_edge('start', 'happy_state', 'say_hello')
empty_graph = LangGraph() print(empty_graph.get_state('unknown')) # None
single_node_graph = LangGraph() single_node_graph.add_node('only_state', {'count': 1}) print(single_node_graph.next_actions('only_state')) # []
lang_graph = LangGraph() lang_graph.add_node('start', {'mood': 'neutral'}) lang_graph.update_state('start', {'mood': 'happy'}) print(lang_graph.get_state('start')) # {'mood': 'happy'}
This program creates a LangGraph, adds a starting state, connects it to two possible next states with actions, and updates the state to show how the agent's memory changes.
class LangGraph: def __init__(self): self.nodes = {} self.edges = {} def add_node(self, node_id, state): self.nodes[node_id] = state def add_edge(self, from_node, to_node, action): if from_node not in self.edges: self.edges[from_node] = [] self.edges[from_node].append((to_node, action)) def get_state(self, node_id): return self.nodes.get(node_id, None) def update_state(self, node_id, new_state): if node_id in self.nodes: self.nodes[node_id] = new_state def next_actions(self, node_id): return self.edges.get(node_id, []) # Create LangGraph instance lang_graph = LangGraph() # Add initial state node lang_graph.add_node('start', {'mood': 'neutral', 'step': 0}) # Add edges representing actions leading to new states lang_graph.add_edge('start', 'happy_state', 'say_hello') lang_graph.add_edge('start', 'sad_state', 'ignore') # Print initial state print('Initial state:', lang_graph.get_state('start')) # Show possible actions from 'start' print('Possible actions from start:', lang_graph.next_actions('start')) # Update state after action lang_graph.update_state('start', {'mood': 'happy', 'step': 1}) print('Updated state:', lang_graph.get_state('start'))
Time complexity for adding nodes or edges is O(1).
Space complexity grows with number of states and actions stored.
Common mistake: forgetting to check if a node exists before updating its state.
Use LangGraph when you need to track how an agent's state changes over time with actions.
LangGraph stores states as nodes and actions as edges to keep agent memory.
It helps agents remember past states and decide next actions.
Updating and querying states is simple and efficient.