When do you need sessions?
Sessions add a lifecycle and persistence layer on top of anyAgent-protocol
brain - the default PlaybookAgent and the legacy DialogMachine alike. A
bare agent holds its conversation state in memory for the lifetime of the
instance. This works perfectly for:
- Voice calls (one machine per call, ephemeral)
- CLI testing (single conversation, short-lived)
- Simple single-user demos
SessionWorker when:
- Multi-user: multiple concurrent conversations hit the same process
- Multi-worker: requests are load-balanced across FastAPI workers or pods
- Long-lived chat: conversations span hours or days and must survive restarts
The Agent Protocol
SessionWorker works with any brain that implements this Protocol:
PlaybookAgent, the legacy DialogMachine, LLMAgent,
and LangChainAgent all implement this.
SessionWorker
Theagent_factory returns a fresh agent per session. Point DialogMachine at
a playbook and you get the default engine; the factory is identical whatever the
artifact:
acquire does:
- Loads or creates the session for
session_id - Acquires a per-session lock (serialises concurrent requests for the same id)
- Yields a
SessionHandle - On exit: persists state to the store and releases the lock
session_ids run fully in parallel.
FastAPI multi-user example
Session stores
Switch stores by changing one line - the rest of the code stays the same:
Lock backends
Other agent brains
When you want sessions and persistence but no checkpoint or flow opinion at all - a raw chat brain:pip install superdialog[langchain]):
Conversation state
Internally, each session stores aChatContext - LiveKit-aligned message
history:
- Playbook engine - the source of truth is the event-sourced log
(
agent.event_log);ConversationState.fold(log, playbook)derives the checkpoint, slots, and outcome. Persistevent_log.to_jsonl()and restore viaload_event_logfor full fidelity. - Legacy DialogMachine -
FlowState(current node, slot values). Present only when the brain is aDialogMachine;Noneotherwise.
Voice (ephemeral) pattern
For voice calls where each call is a fresh conversation and no persistence is needed:NullSessionStore keeps the single-call lifecycle of a bare agent while still giving you the multiplexing and locking of SessionWorker.