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 UserEvery 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
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
| Method | Description |
|---|---|
session.add_node(node) | Registers a node in the graph. |
session.add_edge(parent, child) | Connect two nodes explicitly. |
session.start() | Locks the graph, validates no cycles exist, and starts the event loop. |
session.wait_until_complete() | Keeps the process alive until the WebSocket disconnects. |
session.context | A dict-like object for storing session-scoped state. |

