← Back to blog

Graphs, State Machines, and Agda

I just talk to the agent. The question is what to talk about.

Igor Plotnikov7 min read

The AI crowd on X has moved on to graphs. Since July, "graph engineering" is the buzzword. Karpathy's name gets attached to it a lot, although as far as I can tell he never used the phrase.

Two ideas hide under the one name. One is memory. The agent keeps linked notes, like Karpathy's LLM wiki, so it doesn't start from scratch every session. The other is control, where you replace one agent going round in a loop with a graph of agents and the steps between them.

Then there is Eric Raymond, who just talks to the agent. He thinks the thing of the day mostly compensates for models that aren't smart enough yet, so it may be a temporary crutch.

I just talk to the agent too. For a developer that's largely enough. A product is different, because it runs when I'm not there.

The question is what to talk to the agent about.

I talk about states. A graph describes a system by its steps. A state machine describes it by the situations it can be in and what is allowed from each. The two are equivalent, or isomorphic. What differs is the picture in people's heads, and that matters a great deal.

I've argued the other side before.

At Cisco I was on the graph side

When we built the SSL VPN at Cisco, we were firmly against state machines. We used fibers. In a fiber the state is in a big part the code and the stack. Fibers gave us an illusion of linear code where the algorithm was clear. A state machine breaks the same algorithm into a multitude of callbacks, each reacting to one event, so you just can't see it anymore. That choice was fundamental to the success of the product.

A fiber is a state machine turned inside out. The state still exists, it just lives on the stack. We were choosing a style.

Linear code is a graph of steps written as text. I think agent graphs come from the same instinct. It worked well for us, so I won't call it wrong.

Same system, different questions

A graph makes you ask "what happens next?" Every new situation adds a box and an arrow, and special cases pile up at the edges.

With states the question becomes "where can we be, and what must never happen?" A new situation is a new state, and you have to say what happens in it.

Memory belongs to the state. In our meeting engine the state is the phase plus the turn count, who has spoken, the open questions and the settled positions. Rules can refer to all of it. A discussion can't be declared finished unless the meeting type allows that ending. Karpathy's wiki is another kind of memory, notes the model reads and no rule checks.

A well crafted graph can do all this. Give it loops, a record of state and a rule on every arrow, and you have a state machine. LangGraph's main class is called StateGraph, and the StateFlow paper modeled agents this way in 2024. People who build with these tools know it. My argument is not with that, but with the perception of the message on X.

That message draws boxes left to right, maybe a fork, no going back. Agents go back constantly. They debate, retry, wait, reopen. In the simple picture the loop gets cut short ("retry three times") or hidden inside a box ("the agent works on it here"), where nobody looks.

What changes with an LLM

We chose fibers because you could read the code and understand the algorithm. With an LLM picking the next step, the code is a loop: ask the model, do what it says, ask again. It reads top to bottom and tells you little. The simple graph usually hides that loop in a box. I want the loop, with rules around it.

At the keyboard I don't need them. I keep track and steer. A product has nobody steering, so the steering has to be coded up.

Tool-using agents already work this way. The model asks, plain code does the work, and makes decisions. We do the same for the flow of a meeting. The coordinator, an LLM, proposes the next speaker or an ending. Code checks that the answer is readable, that the speaker is in the meeting, and that the discussion isn't ending before a minimum number of turns. A refused proposal changes nothing. The coordinator gets one more try, then a fixed fallback applies. What an AI board member says in its turn is up to the model.

Is this one more temporary crutch? Where a box exists because the model couldn't manage a step alone, Raymond is right, and smarter models will probably remove it. The product's own states stay. Someone picks the speaker, a board member speaks, the meeting waits for the human or is paused, whether the members are cheap models or geniuses.

The trap is slicing too fine, which rebuilds the rigid rule systems LLMs were supposed to free us from. Our engine has 24 phases, from collecting company data to presenting slides.

Easier to prove

A state machine is also easier to prove things about. Or so I think.

Take chess. Nobody can list every game, yet we know a bishop never leaves its color, because no single bishop move does. Check the move and you've covered every game, including the unplayed ones.

Same here. Start from a good position, make sure each allowed move keeps the rules, and no sequence can break them. You check a short list of moves, not an endless number of paths. That matters with an LLM, which will find the sequences you didn't think of.

A graph can be proved the same way. But to prove anything you need the rules, and a drawing of steps trivializes them. It tempts you to simplify. A state machine design starts with the rules.

For our coordinator we proved that whatever the model answers, the resulting decision names a speaker who is in the meeting, never ends the discussion early, and costs at most two model calls.

Why Agda

We use Agda, a programming language that is also a proof checker. The spec is more than 600 files and covers the meeting engine plus the boring parts like accounts, billing and sign-in. When spec and code disagree, the spec wins. Valya wrote about it in The Equation and AI.

I don't write it by hand. I tell the agent what I want, and it writes a skeleton in English, then the Agda, then the code. Formal specs used to be too expensive to write and keep current, and the relentless agent removes most of that. Reading is still on us. The agent will happily loosen the spec to get a failing test to pass, so no spec change goes in without a person deciding and recording why.

We also compile the spec into a program, the oracle. A runner generates commands, feeds them to both the Go code and the oracle, and compares the answers, refusals included. Nobody writes expected results by hand, so the tests aren't limited to cases somebody imagined. A disagreement gets shrunk to the smallest failing case and saved for replay. One hour-long run last month covered 3,817 generated cases and about 476,000 steps with no disagreement. Earlier runs had some. We break the code on purpose to confirm the tests notice. Part of the browser code is checked the same way.

That makes two separate checks. Coordinator rules watch the LLM while a meeting runs, and the oracle tests our code before it ships.

Agda also demands completeness. Every function must handle every case. Add a phase, say a recess, and the spec won't compile until somebody decides what happens in a recess. (You can write a catch-all, but then you did it on purpose.) In Go the gap would be a quiet default that surfaces when a customer hits it, unless a linter gets there first. LLMs tend to wander into such gaps.

What it doesn't solve

The rules say which moves are allowed. Nothing in them says a meeting has to finish. The coordinator can pick one more speaker, then another, and every pick is allowed, so a discussion could go on forever. A graph with no loops doesn't have this problem, because it runs out of boxes. We added a limit on the number of turns. When a meeting hits it, it stops and waits for the human.

Nor does a proof say anything about quality. A meeting can follow every rule, end on time and be useless.

The proofs are about the spec. What customers run is the Go code. The oracle tests compare the two on thousands of generated cases, and passing them means no difference was found. It doesn't mean there is none.

All of this is too much for a small agent. A loop and a few tests will do there. The spec earns its keep in a product that runs unattended and has sufficiently complex rules it must never break.

For my own work I still just talk to the agent. For the product I do the same, but what we talk about is states and rules, and the agent writes them down as a spec the compiler checks.