Skip to main content

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:
Each primitive serves a distinct purpose and has different lifecycle characteristics.

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_id for data isolation

When to Create a Session

Session State

The state object is a JSON dictionary that represents your agent’s current working memory:
Key Insight: State is ephemeral (tied to the session TTL). For long-term knowledge, use 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

Why Track Turns?

  1. Debugging: Replay exact conversation history to reproduce bugs
  2. Auditing: Compliance and trust (who said what, when)
  3. Analytics: Measure agent performance (success rate, tool usage)
  4. 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

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


Key Takeaway: Sessions are containers, Turns are logs, Memory is knowledge. Master these three primitives and you can build agents that never forget.