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

When to Build a Custom AI Model? RAG vs. Fine-Tuning

Nov 27Article

When to Build a Custom AI Model? RAG vs. Fine-Tuning

Start with a base model. Add RAG when answers depend on current information, and test fine-tuning when a specific failure persists even with the right context. A React Native chatbot that picks up a policy update in SQLite and a support assistant that mishandles a customer’s change of request show when to use each approach or combine them.

How an AI-SDLC Audit Helps You Decide What to Automate First

Nov 26Article

How an AI-SDLC Audit Helps You Decide What to Automate First

An AI-SDLC audit will help your team decide where automation is worth the investment before you commit to a build. Follow a deployment lookup that starts with a testing ticket, from a specific problem statement to an agent design and a proposed pilot. See what the three audit deliverables give your team and how to evaluate the workflow afterward.

Building a Software Factory: AI Reviewers That Don’t Share the Writer’s Blind Spots

Nov 19Article

Building a Software Factory: AI Reviewers That Don’t Share the Writer’s Blind Spots

A reviewer produced 155 confirmed findings and five rounds of work. Szymon Chmal explains how that experience shaped Simlock’s review process, from specialist agents and verified findings to targeted rechecks and historical replays.

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.

Nov 12Live Stream

Mobile Development, Plugins & AI-Powered Workflows

Join Kewin Wereszczyński and Hanno Gödecke on November 12 for live demos and a discussion of the Mobile Dev plugin and AI-powered mobile workflows.

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.

Build, Launch, and Maintain: The Business Case for a Software Factory

Nov 10Article

Build, Launch, and Maintain: The Business Case for a Software Factory

Explore an anonymized software factory business case, its delivery workflow, and the staffing assumptions behind a projected 65% reduction in total costs.