---
title: "State Machines for AI Agents With David Khourshid"
description: "David Khourshid explains state machines for AI agents, human approval steps, tracing, and handling invalid model outputs in agent workflows."
url: https://www.callstack.com/podcasts/state-machines-for-ai-agents-with-david-khourshid
updated: 2026-10-24T08:39:43.828Z
---

# State Machines for AI Agents With David Khourshid

David Khourshid explains state machines for AI agents, human approval steps, tracing, and handling invalid model outputs in agent workflows.

_Guest: David Khourshid, Founder @ Stately · Host: Luke Farrell, Senior Software Engineer @ Callstack · Published 2026-10-12 · Duration 11:17 · Category: AI_

Listen: [YouTube](https://youtu.be/_KojGuXmbGk)

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](https://youtu.be/1rMgw0Q5MgY?si=n4yfy7ITqS56unn2)." This studio interview follows up on deterministic workflows, agent behavior, and the tooling around state machines.

#### Resources

- [Stately](https://stately.ai/)
- [Stately and XState documentation](https://stately.ai/docs)
- [XState on GitHub](https://github.com/statelyai/xstate)
- [David Khourshid on X](https://x.com/davidkpiano)
- [Agent Conf](https://www.agent.sh/)

Insights

## Learn more about AI

Here's everything we published recently on this topic.

[View all](https://www.callstack.com/insights)

- [Efficient On-Device LLMs in React Native Done Right](https://www.callstack.com/events/efficient-on-device-llms-in-react-native-done-right) (Nov 17 · Talk): Artur Morys-Magiera will cover efficient on-device LLMs in React Native, including runtimes, model selection, quantization, and platform constraints.
- [On-Device AI After WWDC: What Changed for React Native](https://www.callstack.com/events/on-device-ai-after-wwdc-what-changed-for-react-native) (Nov 17 · Talk): Mike Grabowski will explain what changed for system-provided on-device AI after WWDC and what React Native developers can build with it.
- [Beyond Coding Agents: Making ADLC Work Across Platforms](https://www.callstack.com/events/beyond-coding-agents-making-adlc-work-across-platforms) (Nov 16 · Talk): Mike Grabowski will explain how to close the full agentic development lifecycle across mobile, web, and native platforms.
- [Building the React Native Software Factory](https://www.callstack.com/events/building-the-react-native-software-factory) (Nov 11 · Talk): 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](https://www.callstack.com/blog/building-a-software-factory-write-specs-an-agent-can-build-from) (Nov 6 · Article): 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](https://www.callstack.com/blog/large-scale-migrations-with-coding-agents-planning-verification-and-rollback) (Nov 4 · Article): 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](https://www.callstack.com/events/quantization-impact-on-llm-quality-a-case-study-of-apex-a-gemma-based-model) (Nov 4 · Talk): 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](https://www.callstack.com/blog/conversational-banking-from-customer-questions-to-authorized-actions) (Nov 2 · Article): 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](https://www.callstack.com/blog/building-a-software-factory-the-night-my-agents-waited-for-me) (Oct 28 · Article): Learn how permissions, review limits, and missing product decisions stalled two overnight coding agent runs, and how their maintainer revised the workflow.
