Sessions
The Session (AgentSession) is the container that holds your active conversation. It creates a sandbox for every user connection, managing state, resources, and the event loop.
One Session = One User
Every time a user connects via WebSocket (or phone), a new AgentSession instance is spawned. This ensures total isolation—variables set in one session never leak to another.
Core Capabilities
The Session is your primary interface for controlling the agent’s environment.
1. Graph Construction
You don’t just add nodes; you build a graph. The session provides methods to define exactly how events flow.
2. Event Hooks
Power Feature: You can listen to events globally!
Use the @session.on_event decorator to inject logic without creating a dedicated node. This is perfect for simple side-effects like logging analytics or sending welcome messages.
3. Automatic Plumbing
The session automatically manages the entry and exit points of your graph.
- Root Node: Automatically created. Receives events from the client and forwards them to your nodes.
- Sink Node: Automatically created. Catches events from your nodes and sends them back to the client.
SDK Reference
Cycle Detection
When you call start(), the session runs a validation algorithm. If your graph contains a cycle (A → B → A), it will raise a ValueError immediately to prevent infinite loops.
Example: Complete Setup
A full example showing graph building, hooks, and lifecycle management.

