Sessions, Turns & Memory
StateBase is built on three fundamental primitives that work together to give your AI agents reliable state management and memory. Understanding how these interact is essential to building production-ready agents.The Mental Model
Think of StateBase like a conversation database:1. Sessions: The Container
A Session is an isolated container for a single conversation or task. It’s the top-level unit of state in StateBase.Characteristics
- Immutable ID: Once created, a session ID never changes
- Mutable State: The session’s internal state can be updated throughout its lifecycle
- TTL-based: Sessions automatically expire after a configurable time (default: 24 hours)
- User-scoped: Each session belongs to a specific
user_idfor data isolation
When to Create a Session
Session State
Thestate object is a JSON dictionary that represents your agent’s current working memory:
2. Turns: The Interaction Log
A Turn represents a single round-trip interaction between the user and your agent.Anatomy of a Turn
Turn Structure
| Field | Type | Purpose |
|---|---|---|
input | Object | User’s message or trigger |
output | Object | Agent’s response |
reasoning | String | Why the agent made this decision (for debugging) |
metadata | Object | Custom tracking data (tool calls, latency, etc.) |
state_before | Object | Session state snapshot before this turn |
state_after | Object | Session state snapshot after this turn |
Why Track Turns?
- Debugging: Replay exact conversation history to reproduce bugs
- Auditing: Compliance and trust (who said what, when)
- Analytics: Measure agent performance (success rate, tool usage)
- Rollback: Revert to a previous turn if the agent goes off-track
3. Memory: Long-Term Knowledge
Memory is how your agent remembers facts across sessions. Unlike state (which is session-scoped), memories are global or user-scoped.Types of Memory
Memory vs State
| Aspect | State | Memory |
|---|---|---|
| Scope | Single session | Cross-session |
| Lifecycle | Ephemeral (TTL) | Permanent |
| Structure | Nested JSON | Flat text + embeddings |
| Access | Direct read | Semantic search |
| Use Case | Working memory | Long-term knowledge |
Semantic Search
Memories are automatically embedded and indexed for vector similarity search:How They Work Together
Here’s a real-world example of all three primitives in action:Best Practices
✅ Do This
- One session per conversation thread
- Log every turn (even errors—they’re valuable for debugging)
- Update state incrementally as the conversation progresses
- Extract memories when you learn something important about the user
- Use semantic search to retrieve relevant memories at the start of each session
❌ Avoid This
- Don’t create a new session for every message (loses context)
- Don’t store long-term facts in state (they’ll expire with the session)
- Don’t skip turn logging (you’ll regret it when debugging production issues)
- Don’t overload memory with trivial facts (focus on high-signal information)
Common Patterns
Pattern 1: Context Injection
Pattern 2: Progressive State Building
Pattern 3: Memory Consolidation
Next Steps
- Checkpoints & Rollbacks: Learn how to create save points and revert state
- Replay & Audit: Understand how to debug and replay conversations
- Quickstart: Build your first stateful agent in 2 minutes
Key Takeaway: Sessions are containers, Turns are logs, Memory is knowledge. Master these three primitives and you can build agents that never forget.