State Machines Explained

State Machines: From a Light Switch to Modern AI

A note on tone

i write my own thoughts and what i understand, if you are reading this i recommend you to watch or read more standard blogs to get full gist of state machines

Start with the word state

State is where you are right now, and it decides where you can go next.

A light switch has two states: ON and OFF. When it is ON, the only move is to turn OFF. When it is OFF, the only move is to turn ON.

A state machine is just that, written down properly:

  1. A list of possible states
  2. A starting state
  3. A set of events (inputs)
  4. Transitions: "in state A, if event X happens, go to state B"
The one-line idea

The machine only needs to remember which state it is in. It doesn't need the whole history. The state is a summary of the past that is just enough to decide the future.

That is what I meant in my old note when I said state represents everything defined in its space. The current state carries all that matters.


A slightly bigger example: a traffic light

Red can never jump straight to Yellow. The rules of the machine forbid it. That is the real power: illegal situations are impossible by design.

In code, a state machine is often just a lookup table:

transitions = {
    ("red", "timer"): "green",
    ("green", "timer"): "yellow",
    ("yellow", "timer"): "red",
}

state = "red"
for _ in range(4):
    state = transitions[(state, "timer")]
    print(state)   # green, yellow, red, green

The journey through history

Here is the whole trip in one picture, and then we will walk through it slowly.

1930s: Turing machines

Alan Turing (1936) described a machine with a finite set of states plus an endless tape to read and write. The states are the "brain" and the tape is memory. Every computer you use is, in principle, this idea.

McCulloch and Pitts (1943) modeled a neuron as a simple on/off unit. Networks of them behave like finite state machines. This is the first bridge between state machines and neural networks. Keep it in mind.

1950s: Formal theory

Why the stack matters

A plain state machine can't count unlimited things. It cannot check that brackets like ((((...)))) match to any depth. Add a stack and it can. More memory gives more power.

1960s to 1980s: Everyday tools

1990s to 2000s: Games and hardware

2010s: The web

Think of fetching data for a page. It is always in exactly one of four states:

function reducer(state, event) {
  switch (state.status) {
    case "idle":
      if (event === "FETCH") return { status: "loading" };
      break;
    case "loading":
      if (event === "SUCCESS") return { status: "done" };
      if (event === "FAIL") return { status: "error" };
      break;
    case "error":
      if (event === "RETRY") return { status: "loading" };
      break;
  }
  return state; // ignore events that aren't allowed here
}

The turn: state machines meet neural networks

A neural network on its own is not a state machine. A plain feed-forward net takes an input and gives an output, with no memory. But the moment we want it to handle sequences (text, speech, time), it needs state. The story of modern AI is largely the story of how to carry state well.

Step 1: Recurrent Neural Networks (RNNs)

An RNN reads one item at a time and keeps a hidden state ht:

ht=f(Whht−1+Wxxt)

Compare with the state machine rule: new_state = transition(old_state, input). It is the same shape. The difference is that a classic machine has a handful of named states, while an RNN's state is a big vector of numbers, so it has a huge continuous space of states. The transition function is learned, not hand-written.

Step 2: LSTM and GRU

Plain RNNs forget quickly. LSTM (1997) and GRU added gates: small learned switches deciding what to keep, what to erase, and what to write. Gates work like guarded transitions: "only update the state if this condition holds."

Step 3: Attention and Transformers

Transformers (2017) dropped the step-by-step hidden state and let every token look at every other token. That was faster to train, but the state idea did not disappear:

Being honest

Saying "a transformer is a state machine" is a stretch. The accurate claim is that generation is a stateful loop, and the cache is the state.

Step 4: The return of state: State Space Models

Because the cache grows without limit, researchers went back to a fixed-size state. State Space Models (S4, then Mamba) use an update like:

ht=Aht−1+Bxt,yt=Cht

This comes straight from control theory, a cousin of state machines from the 1960s. The state stays the same size no matter how long the input is. It is fast for long sequences, and the older idea is back in cutting-edge models.

Step 5: AI agents are explicit state machines

Modern agent systems make the state machine visible:

Frameworks like LangGraph model an agent as nodes (states) and edges (transitions), with a shared state object passed along. The model chooses the next transition, and the state machine keeps it from wandering into nonsense.

Also: Reinforcement learning

An RL agent lives in an environment with states, takes actions, and moves to new states for rewards. This is a Markov Decision Process, a state machine where transitions are chosen and rewarded. It trained game-playing AIs, and it fine-tunes chat models today.


The same question, a bigger state

Era Idea Where the state lives
1936 Turing machine A few states plus a tape
1943 Artificial neuron On/off units
1950s Finite automata, regex Named states
1980s Statecharts, protocols Nested named states
2010s React, Redux, promises A status value in an object
1990s to 2010s RNN, LSTM A learned vector ht
2017 Transformer decoding The growing KV cache
2020s Mamba and SSMs A fixed-size learned state
2020s Agents Explicit graph of states

Why it keeps coming back

Key takeaway

Any system that handles things over time must answer one question: what do I need to remember, and how do I update it? That is the definition of a state machine. A light switch answers it with one bit. A language model answers it with millions of numbers. The question is unchanged.