Module 9 Lesson 3: Stateless vs Stateful Agents
Choosing your memory model. When to build agents that remember the past vs agents that treat every event as new.
Stateless vs. Stateful: The Memory Trade-off
One of the deepest technical decisions you will make as an agentic engineer is how to handle State. In the world of StrandAgents and event-driven systems, this choice determines how you scale.
1. The Stateless Agent
A stateless agent treats every event as if it is the "first time" it has ever seen the user.
- How it works: Every event must contain all the context needed to solve it.
- Pros: Incredibly easy to scale (Serverless). Low latency. No database lock issues.
- Cons: If the conversation is 20 messages long, the "Event" becomes massive because you have to include all 20 messages every time.
2. The Stateful Agent
A stateful agent (like LangGraph) remembers the session.
- How it works: The agent receives an "ID." It looks up the user's history in a database before thinking.
- Pros: Cleaner events. Better for long-running user relationships.
- Cons: Slower (DB lookup latency). Harder to scale (you have to worry about "Race conditions" if two events hit the same state at once).
3. Comparison Matrix
| Feature | Stateless | Stateful |
|---|---|---|
| Storage | None (Transient) | Database (Persistent) |
| Payload Size | Large (Contains history) | Small (Contains ID) |
| Complexity | Low | High |
| Ideal For | Classification, Translation, Summarization | Personal Assistants, RPG Bots, Coding Agents |
4. Visualizing the Payload Shift
Stateless Event Payload:
{
"event": "new_message",
"history": [
{"user": "Hi"},
{"bot": "Hello, how can I help?"}
],
"new_input": "What's the weather?"
}
Stateful Event Payload:
{
"event": "new_message",
"thread_id": "session_8892",
"new_input": "What's the weather?"
}
5. The "Hybrid" Approach (Strand Pattern)
StrandAgents often use a hybrid:
- The Dispatcher is stateless (handles 10,000 requests/sec).
- It routes to a Worker which is stateful (loads the context for one specific user).
This gives you the reliability of a stateful system with the scale of a stateless one.
Key Takeaways
- Stateless is for high-speed, independent tasks.
- Stateful is for multi-turn conversations and long-running workflows.
- Context window limits are the primary reason to move from stateless to stateful as conversations grow.
- Threading and Locking are the primary challenges of stateful multi-agent systems.