State Management Patterns

State management is the architectural discipline of capturing, evolving, and persisting the "current truth" of a system. As systems transition from simple CRUD to complex, long-running processes—particularly in AI-driven or distributed environments—state management moves from a localized implementation detail to a core architectural concern.

1. Frontend Reactive State (React/Web)

In the frontend, state management focuses on synchronizing the UI with underlying data while maintaining performance through unidirectional data flow.

2. Agentic State-Machine Designs

When building Agentic Workflows, state is not just data—it is a progression of intent. AI agents require structured state machines to prevent infinite loops and ensure task completion.

The "Plan-Act-Reflect" Loop

A common agentic pattern uses a Finite State Machine (FSM) to govern the agent's behavior:

StateActionNext State (Success)Next State (Fail)
PlanningLLM generates a task listExecutingPlanning (Re-plan)
ExecutingTool use / Code executionReflectingExecuting (Retry)
ReflectingLLM evaluates the resultPlanning (Next Task)Executing (Correction)
FinishedFinal answer returned--

Managing Agent Memory

Agent state typically consists of:

  1. Short-term (Conversation History): The raw log of messages, often managed via sliding-window truncation.
  2. Working Memory (Scratchpad): Structured data extracted during the task (e.g., "extracted_user_id: 42").
  3. Task Graph: A representation of what has been done and what remains (DAG).

3. Event-Driven Transitions and FSMs

In backend systems, state is often moved via Event-Driven Transitions. Instead of imperatively setting status = 'SHIPPED', the system emits a SHIPPING_LABELED event, and the state machine transitions the aggregate.

Advantages of Formal State Machines

Example State Machine Configuration (JSON-based):

{
  "id": "order_fulfillment",
  "initial": "unpaid",
  "states": {
    "unpaid": {
      "on": { "PAYMENT_RECEIVED": "paid" }
    },
    "paid": {
      "on": { 
        "INVENTORY_RESERVED": "ready_to_ship",
        "CANCELLED": "refund_pending"
      }
    },
    "ready_to_ship": {
      "on": { "SHIPPING_LABEL_GENERATED": "shipped" }
    }
  }
}

4. Durable Workflows (Temporal Pattern)

For processes that last hours, days, or months, state must survive process restarts and server failures. Durable Execution patterns (pioneered by Temporal) ensure that the state of a function—including its local variables and stack—is persisted.

5. Decision Matrix: Which Pattern to Use?

NeedPatternRecommended Tooling
Simple UI SyncReactive HookuseState / Zustand
Long-running TransactionSaga PatternSagaPattern
Complex AI ReasoningFSM / Task GraphLangGraph / XState
Distributed ReliabilityDurable WorkflowTemporal / Azure Durable Functions
Perfect Audit TrailEvent SourcingEventSourcing

Further Reading