Agent state machine

ProductionArchitecture and orchestrationPublished By Simon Budziak

An agent state machine is an explicit model of the states an AI agent may occupy and the events that move it between them. It makes waiting, approval, retry, recovery, completion, and failure visible in code, instead of hiding workflow control inside prompts or scattered conditional logic.

The pattern matters because model output is probabilistic while production control flow must remain inspectable. The OpenAI Agents SDK run lifecycle separates agent execution from application control, and durable workflow systems use the same core idea: store state, apply a known transition, and record the result.

An agent state machine showing planned, acting, waiting, completed, and failed states with controlled transitions

What states belong in an AI agent workflow?

The exact states should reflect the business process, not a generic framework. A useful agentic workflow might include planned, acting, waiting for input, waiting for approval, completed, failed, and cancelled. Each state needs a clear owner, permitted operations, and an exit condition. The state machine owns control flow; the model proposes content and actions.

Keep terminal states explicit. Completed should mean the required outcome was verified, not merely that a model produced a confident answer. Failed should preserve enough evidence for diagnosis. Cancelled should stop future work without being confused with a technical error.

How do transitions work in production?

An event causes a transition only when its guard conditions pass. A tool result may move acting to completed, while a high-impact action moves acting to waiting for approval. A timeout may route the run to retry or failure. LangGraph is one framework that represents stateful agent workflows as graphs, but the operating principle is independent of any library. Every transition should be deterministic even when the model output is not.

Store the transition event, previous state, next state, workflow version, and relevant decision evidence. That record makes it possible to explain why a run advanced, paused, or stopped.

How should state survive pauses and failures?

Persist durable state outside the model context. An agent checkpoint should capture the minimum information needed to resume safely, including completed effects and pending approvals. After a process crash, the orchestrator reads the checkpoint and continues from a known state instead of reconstructing truth from chat history. A restart must not silently repeat an external action.

Pair state persistence with idempotent tools and explicit retry limits. If the previous result is uncertain, route the workflow into reconciliation or agent recovery rather than guessing that the action failed.

When is a state machine worth the added structure?

Use one when work spans multiple tools, people, time periods, or failure paths. It is especially valuable for approvals, long-running jobs, transactions, and processes that must be audited. A single read-only lookup may be simpler as a direct function call. Explicit states earn their cost when an operator needs to know what can happen next.

Start with the smallest set of meaningful states and add a state only when it changes allowed behavior, ownership, or recovery. That keeps the diagram and runtime aligned instead of turning the state model into documentation that nobody can operate.

Frequently asked questions

What is an agent state machine used for?

Use it when a workflow can pause, branch, wait for approval, retry, or recover. It makes every allowed transition and terminal outcome explicit.

Does every AI agent need a state machine?

No. A short, read-only task may not, but multi-step production workflows benefit from explicit states and persisted transitions.

How is an agent state machine different from a prompt?

A prompt influences model behavior. A state machine deterministically controls which stage runs next and which transitions are allowed.

Summarize this page with

Train your team to build this