
David Khourshid, Founder of Stately and Creator of XState, joins us in the studio at Agent Conf 2026 in Warsaw to discuss using state machines to structure AI agent workflows.
We talk about keeping the workflow deterministic while allowing an agent flexibility within each step. David explains an email approval example, how traces can reveal where agents get stuck, and how guards handle unexpected outputs. He also shares how Stately uses AI to generate and update state machines.
A deterministic workflow around an unpredictable agent
David describes his approach as "determinism in the core and non-determinism in the edges." An agent can decide what to do within a step, while a state machine determines the flow between steps.
His example is an email drafter: the agent can prepare the message, but it must wait for the user's approval before sending it. David explains why he would encode that requirement in the workflow rather than rely on a prompt telling the agent not to send.
Mapping agent traces to state transitions
Knowing what an agent said does not always explain what it did. David describes mapping traces to transitions in a state machine so developers can inspect the path through a workflow.
That view can show which steps an agent repeats or gets stuck on, and which paths users take. We also discuss using a heat map to identify places where the model or workflow might need attention.
States, events, and the actor model
David explains that state machines can be implemented in any language, using something as simple as a switch statement or a function that determines the next state from the current state and an event.
He describes the execution loop: start in an initial state, receive events, determine the next state, execute effects, and repeat until the workflow is done. He also explains how the actor model and state machines work together: actors coordinate and communicate, while state machines define each agent's behavior.
Handling unexpected model outputs
We ask how a deterministic workflow can respond to a model whose output is unpredictable. David explains that a state machine can ignore events it does not recognize, and guards can require an output to pass a schema check.
Those checks make error handling part of the workflow. If an agent call fails to produce an acceptable result, the system can retry, move into an error state, or take a compensating action.
Using AI to generate and update state machines
David says Stately has been using AI to create state machines since 2023. Early models sometimes confused XState versions, produced syntax errors, or hallucinated details.
He describes a process that generated multiple candidates and used a validation loop to check the result and try again when it was invalid. He also discusses generating a machine, visualizing it, and asking AI to patch its transitions or logic. The process involves several steps between the request and the final output.
When a state machine is useful
David says a simple text-generation task may not need a state machine, particularly when the output has few constraints. More demanding workflows can be much larger: he recalls opening machines with around 200 states after users reported a slow visual editor.
He also discusses LangGraph as another way to define agent logic through a graph, and points viewers toward Stately's visual editor.
About the guest
David Khourshid is Founder of Stately and Creator of XState, a state machine library. At Agent Conf 2026, he presented "Goodbye Slop; Welcome Determinism." This studio interview follows up on deterministic workflows, agent behavior, and the tooling around state machines.











