Concept #66Mediumextended-ai-concepts

What is stateless and stateful in AI agents?

#gen-ai#agents

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

StatelessStateful
MemoryNo memory between callsRetains memory across calls
ContextEach call is independentContext accumulates over time
ComplexitySimpleMore complex
ScalabilityEasy to scale (no shared state)Harder to scale
Use caseSingle-turn tasksMulti-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
State
schema:

python
from 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 TypeStorageDuration
In-memoryPython list/dictCurrent session only
DatabasePostgreSQL, RedisPersistent across sessions
Vector storePinecone, ChromaSemantic memory
File systemJSON, SQLiteSimple persistence

When to Use Each

Use StatelessUse Stateful
One-shot tasks ("summarize this text")Chatbots with conversation history
Parallel/distributed agentsLong-running tasks with checkpoints
API microservicesUser personalization
Simple transformationsAgents 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)