0
0
Agentic AIml~5 mins

State graphs and transitions in Agentic AI

Choose your learning style9 modes available
Introduction

State graphs help us understand how a system moves from one situation to another. They show all possible steps and choices clearly.

To model how a robot decides what to do next based on its current position.
To represent different stages in a game and how players move between them.
To track the steps in a customer support chatbot conversation.
To plan actions for an AI agent in a smart home system.
To visualize how a machine learning model changes states during training.
Syntax
Agentic AI
class State:
    def __init__(self, name):
        self.name = name
        self.transitions = {}  # Maps action to next State

    def add_transition(self, action, next_state):
        self.transitions[action] = next_state

    def next_state(self, action):
        return self.transitions.get(action, None)

# Example usage:
# state1 = State('Start')
# state2 = State('Middle')
# state1.add_transition('go', state2)
# next = state1.next_state('go')
# print(next.name)  # Output: Middle

Each state has a name and a list of possible transitions.

Transitions connect one state to another based on an action or event.

Examples
This shows a state with no transitions yet.
Agentic AI
empty_state = State('Empty')
print(empty_state.transitions)  # Output: {}
A state with one transition named 'move'.
Agentic AI
single_transition_state = State('Single')
single_transition_state.add_transition('move', State('Next'))
print(list(single_transition_state.transitions.keys()))  # Output: ['move']
States connected in a chain with transitions.
Agentic AI
start = State('Start')
middle = State('Middle')
end = State('End')
start.add_transition('go', middle)
middle.add_transition('finish', end)
print(start.next_state('go').name)  # Output: Middle
print(middle.next_state('finish').name)  # Output: End
A state that transitions back to itself, showing a loop.
Agentic AI
state = State('Loop')
state.add_transition('repeat', state)
print(state.next_state('repeat').name)  # Output: Loop
Sample Model

This program creates a simple state graph with three states and transitions. It shows moving through states by actions.

Agentic AI
class State:
    def __init__(self, name):
        self.name = name
        self.transitions = {}

    def add_transition(self, action, next_state):
        self.transitions[action] = next_state

    def next_state(self, action):
        return self.transitions.get(action, None)

# Create states
start_state = State('Start')
middle_state = State('Middle')
end_state = State('End')

# Add transitions
start_state.add_transition('go', middle_state)
middle_state.add_transition('finish', end_state)
middle_state.add_transition('repeat', middle_state)  # loop

# Print initial state
print(f"Current state: {start_state.name}")

# Move from start to middle
current_state = start_state.next_state('go')
print(f"After 'go' action, state: {current_state.name}")

# Repeat in middle state
current_state = current_state.next_state('repeat')
print(f"After 'repeat' action, state: {current_state.name}")

# Finish to end state
current_state = current_state.next_state('finish')
print(f"After 'finish' action, state: {current_state.name}")
OutputSuccess
Important Notes

Time complexity to find the next state is O(1) because transitions are stored in a dictionary.

Space complexity depends on the number of states and transitions stored.

A common mistake is forgetting to add transitions, which leads to None when moving states.

Use state graphs when you want to clearly map all possible moves and decisions in a system.

Summary

State graphs show how a system moves between different situations.

Each state has transitions triggered by actions or events.

They help plan and understand AI agent behavior step-by-step.