Skip to content

What is an agent state machine and how does state persist across multi-step execution?

Beginner · 2 min read · AI Agents & MCP

Open in study mode to save this question and track progress

Short answer: An agent state machine tracks execution state variables (conversation messages, tool outputs, active task steps, intermediate variables) across multi-turn agentic loops; state persistence serializes state graphs to external datastores (e.g. Postgres or Redis) between turns.

Detail

Stateless APIs forget past actions between turns. Agent frameworks (like LangGraph) maintain explicit state graphs.

State (Step N): {messages: [...], user_id: "u123", current_step: "data_fetching"}
                          │
                          ▼
             [Execute Tool Call / Next Turn]
                          │
                          ▼
State (Step N+1): {messages: [..., ToolResult], user_id: "u123", current_step: "summarization"}
                          │
                          ▼
            Serialize & Persist to Postgres / Redis

Key State Components

  1. Message Log: Conversation thread history (system, user, assistant, tool).
  2. Custom State Variables: Intermediate key-value outputs produced during multi-step tool calls.
  3. Execution Pointers: Current active node or step in a workflow graph.

Example

Python concept representing serializable agent state:

python
import json
from dataclasses import dataclass, asdict, field

@dataclass
class AgentState:
    session_id: str
    messages: list[dict] = field(default_factory=list)
    scratchpad: dict = field(default_factory=dict)
    step_count: int = 0

    def serialize(self) -> str:
        return json.dumps(asdict(self))

state = AgentState(session_id="sess_42")
state.messages.append({"role": "user", "content": "Fetch report"})
state.scratchpad["retrieved_id"] = 991
print("Serialized State Payload:\n", state.serialize())

Interview tips

  • Highlight thread checkpointing: resuming long-running agent workflows after server crashes or human approvals.
  • Contrast in-memory agent execution with database-backed persistent agent state graphs.

Related Concepts


⬅ Back to AI Agents and MCP · All topics