# State machines in 2 minutes
**作者**: David K
**日期**: 2026-07-20T14:20:41.000Z
**来源**: [https://x.com/DavidKPiano/status/2079209887158989231](https://x.com/DavidKPiano/status/2079209887158989231)
---

First it was loops. Now it's graphs. Next month it'll be something else.
Here's the thing: we're constantly rediscovering decades-old software engineering patterns and repackaging them as innovations or whatever, instead of just applying what we've already known for a long time.
Two patterns I keep seeing rediscovered over and over are the actor model and state machines. I already wrote a 2-minute explanation of the actor model (https://x.com/DavidKPiano/status/2033132659795194367), so now take 2 minutes to understand state machines, because that's really all you need (and what I've been talking about incessantly for 10+ years).
A state machine is very simple. It's a thing that answers one question:
> Given the current state, when an event occurs, what is the next state?
Or as a function:
> (state, event) → nextState
That's it. And that function describes every program, application, and algorithm ever written, because it's a fundamental truth of computer science: given some state, something happens that transitions it to the next state. We don't usually *write* our programs this way, but it's the underlying "physics" of how all code works, whether we realize it or not.
One thing I love about state machines is that you can visualize them. A finite state machine has a finite set of states and a finite set of events that cause transitions between those states. It's basically the same (state, event) → nextState function, just visualized as a graph: states are nodes, transitions are edges.

A simple state machine; if you can read words and follow arrows, you already know how to read it
Making this explicit gives you two guarantees for free:
1. You will always be in exactly one state* (no more impossible states)
2. You can never transition anywhere except along a defined edge (no undefined behavior, by construction)
Ok, but what about loops and graphs?
Surprise... loops are graphs: directed, cyclic ones. A loop is also just about the most basic state machine there is: two states, "looping" and "done":

This is the silly thing you all hyped for weeks
And when we talk about "agent graphs," we're really describing how an agent moves through different "steps" to accomplish some task. But those steps aren't always sequential, and they aren't always acyclic either (sorry, DAG lovers): agents retry, backtrack, wait for humans, loop until verified. Map out an agent with realistically complex logic and it looks like this:

Contrived example, but you get the idea
That's a state machine. That's also a graph. State machines are graphs. The nodes and edges have precise meaning: which states exist (nodes), which events matter (edge labels), and where they lead (edges).
So loops, graphs, whatever comes next month: it's all the same underlying model. State machines and the actor model are the two concepts actually worth learning for building complex agent logic and multi-agent architectures. Nothing is new; don't fall for the hype. We're just reapplying well-known software patterns (and that's a very good thing IMO), just repackaged for whatever reason.
Also you don't need a library for this. A simple `switch` statement or an object/dict works in every programming language. But if you want to build more complex state machines and statecharts*, I created XState to help you: https://github.com/statelyai/xstate
Happy graphing! 🟡→🟢
*I'll talk about statecharts in the next article. They bring state machines to the next level and make them much more useful to work with for complex logic, but are still technically state machines.
## 相关链接
- [David K](https://x.com/DavidKPiano)
- [@DavidKPiano](https://x.com/DavidKPiano)
- [3.4K](https://x.com/DavidKPiano/status/2079209887158989231/analytics)
- [https://x.com/DavidKPiano/status/2033132659795194367](https://x.com/DavidKPiano/status/2033132659795194367)
- [https://github.com/statelyai/xstate](https://github.com/statelyai/xstate)
- [Upgrade to Premium](https://x.com/i/premium_sign_up)
- [10:20 PM · Jul 20, 2026](https://x.com/DavidKPiano/status/2079209887158989231)
- [3,421 Views](https://x.com/DavidKPiano/status/2079209887158989231/analytics)
- [View quotes](https://x.com/DavidKPiano/status/2079209887158989231/quotes)
---
*导出时间: 2026/7/20 23:36:32*
---
## 中文翻译
# 两分钟看懂状态机
**作者**: David K
**日期**: 2026-07-20T14:20:41.000Z
**来源**: [https://x.com/DavidKPiano/status/2079209887158989231](https://x.com/DavidKPiano/status/2079209887158989231)
---

先是循环。现在是图。下个月又不知道会变成什么。
问题在于:我们不断地重新发现几十年前的软件工程模式,把它们重新包装成创新或者别的什么东西,而不是直接应用我们早已熟知的成果。
我反复看到被重新发现的两个模式是 Actor 模型和状态机。我已经写了一篇 2 分钟的 Actor 模型解释(https://x.com/DavidKPiano/status/2033132659795194367),所以现在花 2 分钟了解一下状态机,因为这真的就是你所需要的全部(也是我这 10 多年来喋喋不休谈论的东西)。
状态机非常简单。它只回答一个问题:
> 给定当前状态,当一个事件发生时,下一个状态是什么?
或者作为一个函数:
> (state, event) → nextState
仅此而已。这个函数描述了所有编写的程序、应用和算法,因为这是计算机科学的一个基本真理:给定某种状态,发生某些事情将其转移到下一个状态。我们通常不这样*编写*程序,但这是所有代码运作的底层“物理机制”,无论我们是否意识到这一点。
我喜欢状态机的一点是,你可以将它们可视化。有限状态机具有有限的状态集合和有限的事件集合,这些事件导致状态之间的转换。它基本上就是同一个 (state, event) → nextState 函数,只是可视化为一张图:状态是节点,转换是边。

一个简单的状态机;如果你能识字并跟着箭头走,你就已经知道如何阅读它了
明确这一点会免费给你两个保证:
1. 你将始终处于一个确切的状态*(不再有不可能的状态)
2. 你只能沿着定义的边进行转换(从构造上避免了未定义的行为)
好吧,那循环和图呢?
惊喜……循环也是图:有向的、循环的图。循环也可以说是最基本的状态机:两个状态,“循环中”和“完成”:

这就是你们为此兴奋了好几个星期的傻东西
当我们谈论“智能体图”时,我们实际上是在描述一个智能体如何通过不同的“步骤”来完成任务。但这些步骤并不总是顺序的,也不总是非循环的(抱歉,DAG 爱好者们):智能体会重试、回溯、等待人类、循环直到验证通过。绘制一个具有现实复杂逻辑的智能体,它看起来像这样:

做作的例子,但你懂我的意思
这就是状态机。这也是一张图。状态机就是图。节点和边有精确的含义:哪些状态存在(节点),哪些事件重要(边标签),以及它们通向哪里(边)。
所以循环、图,以及下个月出现的任何新东西:都是同样的底层模型。状态机和 Actor 模型是构建复杂智能体逻辑和多智能体架构时真正值得学习的两个概念。没有什么是新的;不要被炒作忽悠。我们只是在重新应用众所周知的软件模式(我认为这是一件非常好的事情),只是出于某种原因重新包装了一下。
此外,你不需要为此引入一个库。一个简单的 `switch` 语句或对象/字典在任何编程语言中都行得通。但如果你想构建更复杂的状态机和状态图*,我创建了 XState 来帮助你:https://github.com/statelyai/xstate
绘图愉快!🟡→🟢
*我会在下一篇文章中谈论状态图。它们将状态机提升到了一个新的水平,使处理复杂逻辑变得更有用,但在技术上仍然是状态机。
## 相关链接
- [David K](https://x.com/DavidKPiano)
- [@DavidKPiano](https://x.com/DavidKPiano)
- [3.4K](https://x.com/DavidKPiano/status/2079209887158989231/analytics)
- [https://x.com/DavidKPiano/status/2033132659795194367](https://x.com/DavidKPiano/status/2033132659795194367)
- [https://github.com/statelyai/xstate](https://github.com/statelyai/xstate)
- [Upgrade to Premium](https://x.com/i/premium_sign_up)
- [10:20 PM · Jul 20, 2026](https://x.com/DavidKPiano/status/2079209887158989231)
- [3,421 Views](https://x.com/DavidKPiano/status/2079209887158989231/analytics)
- [View quotes](https://x.com/DavidKPiano/status/2079209887158989231/quotes)
---
*导出时间: 2026/7/20 23:36:32*