Skip to content

State Management

Strategies for maintaining continuity of reasoning across complex agent workflows

Why State Management Matters

In complex workflows, the key is managing the continuity of reasoning — having the AI remember prior steps and logically move on to the next one.

    flowchart LR
    A["Step 1<br/>Data Collection"] -->|"Save Result"| S["State Store<br/>State Store"]
    S -->|"Load State"| B["Step 2<br/>Analysis"]
    B -->|"Save Result"| S
    S -->|"Load State"| C["Step 3<br/>Report Writing"]

    style S fill:#7C3AED,stroke:#6D28D9,color:#fff
    style A fill:#2563EB,stroke:#1D4ED8,color:#fff
    style B fill:#EA580C,stroke:#C2410C,color:#fff
    style C fill:#16A34A,stroke:#15803D,color:#fff
  

Types of State

State typeDurationStorageExample use
In-memory stateDuring executionPython dictA single execution session
Session stateFor the conversationRedisMulti-turn conversation
Persistent stateLong-termPostgreSQLUser preferences, history
CheckpointUntil the task completesFile/DBLong-running workflows

LangGraph State Management Example

from langgraph.graph import StateGraph
from typing import TypedDict, List

class AgentState(TypedDict):
    messages: List[str]
    current_step: str
    collected_data: dict
    analysis_result: str
    is_complete: bool

def research_node(state: AgentState) -> AgentState:
    # Read the previous state and return a new state
    data = collect_data(state["messages"][-1])
    return {
        **state,
        "collected_data": data,
        "current_step": "analysis"
    }

graph = StateGraph(AgentState)
graph.add_node("research", research_node)
graph.add_node("analysis", analysis_node)
graph.add_node("report", report_node)

Checkpointing Strategy

So that a long-running workflow doesn’t have to restart from scratch after a mid-run failure:

# Save a checkpoint after each step completes
checkpoint = {
    "step": "data_collection",
    "status": "completed",
    "result": collected_data,
    "timestamp": datetime.now().isoformat()
}
db.save_checkpoint(workflow_id, checkpoint)

# On restart, resume from the last checkpoint
last_checkpoint = db.load_checkpoint(workflow_id)
if last_checkpoint["step"] == "data_collection":
    skip_to_analysis(last_checkpoint["result"])