Agentic AI · Intermediate level

Intermediate level · Module 6: Agent State Management · Lesson 2 of 3

Agents as State Machines

Learners model the agent as a state machine with named statuses and allowed transitions, so that impossible or unsafe sequences are prevented by design.

In this lesson you will

  • Define statuses and allowed transitions
  • Enforce transitions in code

Key concepts

state machinestatus (planning, searching, drafting, reviewing, done, handed_over)allowed transitionsterminal statesinvalid transition errors

The lesson

A state machine names the statuses an agent can be in and which moves are allowed: planning → searching → drafting → reviewing → done, with exits to handed_over or failed from any step.

Enforcing transitions in code (a dictionary of allowed next statuses, checked on every change) prevents unsafe sequences, such as 'done' without 'reviewing'.

Drawing the state machine gives a precise diagram for reviews and is the direct preparation for graph-based orchestration at the Advanced level.

Step by step

  1. Draw the project agent's state machine.
  2. Implement allowed transitions as a dictionary.
  3. Raise an error on any invalid transition.
  4. Test three valid and two invalid sequences.

Practice activity

Implement and test the project agent's state machine.

Expected result: A state machine that allows only the designed sequences.

Check your understanding

  1. What is a terminal state?

    Show answer

    A state with no further transitions, such as done, handed_over or failed.

  2. Why enforce transitions in code?

    Show answer

    To prevent unsafe or impossible sequences, such as finishing without review.

Further learning

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.