Answer
Stateless vs Stateful in AI Agents
State management is a critical design decision when building AI agents. Understanding stateless vs stateful architecture determines how your agent handles context, memory, and multi-turn conversations.
Core Definitions
| Stateless | Stateful | |
|---|---|---|
| Memory | No memory between calls | Retains memory across calls |
| Context | Each call is independent | Context accumulates over time |
| Complexity | Simple | More complex |
| Scalability | Easy to scale (no shared state) | Harder to scale |
| Use case | Single-turn tasks | Multi-turn conversations, long tasks |
Stateless Agent
python# Stateless: each call has no memory of previous calls def stateless_agent(user_message: str) -> str: response = client.messages.create( model="claude-opus-4-6", messages=[ {"role": "user", "content": user_message} ] ) return response.content[0].text # Call 1 reply1 = stateless_agent("My name is Alice") # Call 2 — agent has NO idea Alice mentioned her name reply2 = stateless_agent("What is my name?") # Will say "I don't know"
Stateful Agent
python# Stateful: messages accumulate in history class StatefulAgent: def __init__(self): self.history = [] # Maintains state def chat(self, user_message: str) -> str: self.history.append({"role": "user", "content": user_message}) response = client.messages.create( model="claude-opus-4-6", messages=self.history ) assistant_msg = response.content[0].text self.history.append({"role": "assistant", "content": assistant_msg}) return assistant_msg agent = StatefulAgent() agent.chat("My name is Alice") # Stored in history agent.chat("What is my name?") # Returns "Alice" — context retained
LangGraph Stateful Agents
LangGraph manages state explicitly via a
text
Statepythonfrom langgraph.graph import StateGraph from typing import TypedDict, Annotated from langgraph.graph.message import add_messages class State(TypedDict): messages: Annotated[list, add_messages] # State persisted across nodes current_task: str completed_steps: list graph = StateGraph(State) # State flows through all nodes — each node reads and updates it
Types of State in Agents
| State Type | Storage | Duration |
|---|---|---|
| In-memory | Python list/dict | Current session only |
| Database | PostgreSQL, Redis | Persistent across sessions |
| Vector store | Pinecone, Chroma | Semantic memory |
| File system | JSON, SQLite | Simple persistence |
When to Use Each
| Use Stateless | Use Stateful |
|---|---|
| One-shot tasks ("summarize this text") | Chatbots with conversation history |
| Parallel/distributed agents | Long-running tasks with checkpoints |
| API microservices | User personalization |
| Simple transformations | Agents that learn from past interactions |
Key Challenge: State Management at Scale
Stateful agents are more powerful but require careful design:
- Where to store state (in-memory vs external DB)
- How to handle concurrency (multiple users)
- When to expire/summarize old state (token limits)
- How to recover from failures (checkpointing)