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
Start learning this pattern below
Jump into concepts and practice - no test required
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.
Practice
Solution
Step 1: Understand LangGraph structure
LangGraph uses nodes to represent states and edges to represent actions connecting those states.Step 2: Identify the purpose of this structure
This structure helps agents remember past states and decide next actions based on memory.Final Answer:
To store states as nodes and actions as edges for memory -> Option AQuick Check:
LangGraph = state nodes + action edges [OK]
- Confusing LangGraph with model training
- Thinking LangGraph generates random actions
- Assuming LangGraph only visualizes data
Solution
Step 1: Identify method to add nodes
Adding a new state means adding a node, so the method should be add_node.Step 2: Check options for adding nodes
Only langgraph.add_node(new_state) uses add_node(new_state), which correctly adds a state node.Final Answer:
langgraph.add_node(new_state) -> Option CQuick Check:
Add state = add_node() method [OK]
- Using add_edge() to add states
- Confusing remove_node() with adding
- Trying to update actions to add states
langgraph.add_node('S1')
langgraph.add_node('S2')
langgraph.add_edge('S1', 'S2', 'move')
print(langgraph.get_next_action('S1'))What will be the output?
Solution
Step 1: Understand the graph setup
Two states 'S1' and 'S2' are added, then an edge from 'S1' to 'S2' with action 'move'.Step 2: Check get_next_action('S1')
This method returns the action on the edge from 'S1' to its next state, which is 'move'.Final Answer:
'move' -> Option BQuick Check:
Edge action from S1 = 'move' [OK]
- Confusing action with next state
- Expecting None if not familiar with method
- Assuming method does not exist
langgraph.add_node('S1')
langgraph.add_node('S2')
langgraph.add_edge('S1', 'S2', 'jump')
langgraph.update_edge('S1', 'S2', 'run')Solution
Step 1: Check if update_edge method exists
LangGraph typically does not have update_edge; edges are removed and re-added to update.Step 2: Identify correct update approach
To change an action, remove the old edge and add a new edge with the new action.Final Answer:
update_edge method does not exist; should remove and add edge -> Option DQuick Check:
No update_edge method in LangGraph [OK]
- Assuming update_edge exists
- Trying to update nodes instead of edges
- Thinking action strings are invalid
Solution
Step 1: Understand loop avoidance in LangGraph
Storing visited states as nodes and adding edges only for new actions helps the agent remember paths and avoid loops.Step 2: Evaluate other options
Clearing the graph loses memory, duplicates confuse state identity, and external lists separate memory from LangGraph.Final Answer:
Store visited states as nodes and add edges only for new actions -> Option AQuick Check:
Memory in LangGraph = nodes + edges tracking [OK]
- Resetting graph loses memory
- Duplicating nodes breaks state tracking
- Using external lists splits memory logic
