Skip to content
TheQuantaSpace
← Research
Agents1 min read

Agents need state machines, not longer prompts

When an LLM agent misbehaves, the reflex is to add another paragraph to the system prompt. A better fix is to make the agent's control flow explicit, so the model chooses between moves instead of inventing them.

Most agent failures in production are not intelligence failures. They are control-flow failures: the agent retries forever, skips a confirmation step, or calls a tool in a state where it makes no sense.

The usual fix is to append rules to the prompt: “Never call refund before lookup_order.” That works until the prompt is 4,000 tokens long and the rules start contradicting each other.

Make the states explicit

A more durable pattern is to model the workflow as a small state machine and let the model pick a transition only from the ones that are valid right now.

type State = 'triage' | 'lookup' | 'propose' | 'confirm' | 'done';

const transitions: Record<State, State[]> = {
  triage: ['lookup', 'done'],
  lookup: ['propose', 'triage'],
  propose: ['confirm', 'lookup'],
  confirm: ['done', 'propose'],
  done: [],
};

function toolsFor(state: State) {
  // Only expose the tools that make sense in this state.
  return registry.filter((t) => t.states.includes(state));
}

Each turn, the agent sees the current state, the tools allowed in it, and the legal next states. The model still does the hard part (understanding the user and choosing what to do), but it can’t wander off the map.

What you get for free

  • Testability. Transitions are plain data, so you can unit-test them without calling a model.
  • Observability. Every trace becomes a path through a graph, so “stuck in lookup for 9 turns” is a query, not a mystery.
  • Smaller prompts. Rules that lived in prose now live in code, and the prompt shrinks back to intent.

When not to bother

Open-ended research agents with no fixed procedure gain little from this. The pattern pays off when there is a process (support flows, onboarding, ops runbooks) where some steps must happen before others.

Prompts describe intent. Code enforces invariants. Use each for what it’s good at.

  • #llm
  • #agents
  • #architecture