0
0
Agentic AIml~5 mins

LangGraph for stateful agents in Agentic AI

Choose your learning style9 modes available
Introduction

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.

When building a chatbot that needs to remember previous conversations.
When creating a virtual assistant that adapts based on user history.
When designing a game AI that learns from past moves.
When developing a recommendation system that considers user preferences over time.
When managing complex workflows where past steps affect future actions.
Syntax
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.

Examples
Adds a starting state and an action leading to a happy state.
Agentic AI
lang_graph = LangGraph()
lang_graph.add_node('start', {'mood': 'neutral'})
lang_graph.add_edge('start', 'happy_state', 'say_hello')
Shows that querying a state not added returns None.
Agentic AI
empty_graph = LangGraph()
print(empty_graph.get_state('unknown'))  # None
A graph with one node and no edges returns empty list for next actions.
Agentic AI
single_node_graph = LangGraph()
single_node_graph.add_node('only_state', {'count': 1})
print(single_node_graph.next_actions('only_state'))  # []
Updates the state data for an existing node.
Agentic AI
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'}
Sample Model

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.

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, [])


# 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'))
OutputSuccess
Important Notes

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.

Summary

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.