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 progressShort 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
- Message Log: Conversation thread history (
system,user,assistant,tool). - Custom State Variables: Intermediate key-value outputs produced during multi-step tool calls.
- 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
- [[How do DAG-based Multi-Agent Orchestrators prevent state deadlocks?]] (
#139): How do DAG-based Multi-Agent Orchestrators prevent state deadlocks? - [[How to design an enterprise-grade LLM Gateway with dynamic fallback, tenant rate limiting, and cost allocation?]] (
#169): How to design an enterprise-grade LLM Gateway with dynamic fallback, tenant rate limiting, and cost allocation? - [[How to design an autonomous multi-agent software engineering system in a staff-level interview?]] (
#200): How to design an autonomous multi-agent software engineering system in a staff-level interview?