Bryn Shanahan

← Back to blog

Giving your Agent a Spine: Part 1

agentic

Using state machines to give structure and observability to your agentic experiences

The Problem

I truly believe that agentic experiences can be a big win for teams trying to give end users a more powerful and customised experience.

It's really hard to build agentic experiences that take the users through a complex flow without the experience falling apart. I've had experiences building experiences where an agent will randomly handoff to another agent, forget what it was doing, or get stuck in loops repeating the same action over and over.

When building AI agents that need to perform multi-step tasks, it's easy for the agent to get lost, repeat steps, or make inconsistent decisions. This leads to a frustrating user experience.

One way to address this is to give your agent a "spine" - a structured framework that guides its behaviour and limits it's available actions at any given time.

State Machines

First of there are a couple of really interesting ways that people are looking into fixing this issue. Mastra is a really cool project that aims to provide a framework for building agentic experiences around predefined flows. But I decided to explore a more general purpose approach using state machines.

If you haven't heard of state machines before, they are a way to model the behaviour of a system by defining a set of states and the transitions between them. At any given time, the system is in one state, and can only transition to certain other states based on defined rules.

For example, consider a state machine for a traffic light:

Workspace Files
Loading...

Chat Session
you are in charge of operating a single traffic light, your job is to say "red", "yellow" and "green" in the correct order