State Machines for AI Agents With David Khourshid

Guest
David Khourshid
Founder
@
Stately

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.

Resources

Insights

Learn more about AI

cover

Nov 17Talk

Efficient On-Device LLMs in React Native Done Right

Artur Morys-Magiera will cover efficient on-device LLMs in React Native, including runtimes, model selection, quantization, and platform constraints.

cover

Nov 17Talk

On-Device AI After WWDC: What Changed for React Native

Mike Grabowski will explain what changed for system-provided on-device AI after WWDC and what React Native developers can build with it.

cover

Nov 16Talk

Beyond Coding Agents: Making ADLC Work Across Platforms

Mike Grabowski will explain how to close the full agentic development lifecycle across mobile, web, and native platforms.

Building the React Native Software Factory — cover

Nov 11Talk

Building the React Native Software Factory

Join us at React Foundation Meetup London on November 11 to explore how AI agents build and debug React Native apps, from one terminal to the cloud.

Building a Software Factory: Write Specs an Agent Can Build From — cover

Nov 6Article

Building a Software Factory: Write Specs an Agent Can Build From

Learn how to research requirements, resolve open decisions, and write testable specs that AI coding agents can implement without the planning conversation.

 Large-Scale Migrations With Coding Agents: Planning, Verification, and Rollback — cover

Nov 4Article

Large-Scale Migrations With Coding Agents: Planning, Verification, and Rollback

Plan codebase migrations with coding agents using reviewable requirements, verification checks, staged rollouts, and defined rollback criteria.

Quantization Impact on LLM Quality: A Case Study of Apex — cover

Nov 4Talk

Quantization Impact on LLM Quality: A Case Study of Apex

Artur Morys-Magiera and Lech Kalinowski share findings from 344 benchmark runs with Apex, our Gemma-based LLM, examining the trade-offs between model size, decode speed, and quality.

Conversational Banking: From Customer Questions to Authorized Actions — cover

Nov 2Article

Conversational Banking: From Customer Questions to Authorized Actions

Explore conversational banking architecture through a card-dispute example, covering tool access, authorization, execution, observability, and human handoff.

Building a Software Factory: The Night My Agents Waited for Me — cover

Oct 28Article

Building a Software Factory: The Night My Agents Waited for Me

Learn how permissions, review limits, and missing product decisions stalled two overnight coding agent runs, and how their maintainer revised the workflow.