State Management and Persistence
Based automatically persists variables across conversation turns, allowing you to maintain context throughout a multi-turn conversation. Understanding how state works is essential for building stateful agents.The state Dictionary
The state dictionary is the primary way to store and retrieve data that should persist across conversation turns. It’s automatically saved and restored between interactions.
Usage:
Variable Persistence Example
Here’s a complete example showing how variables persist across conversation turns:Variables defined at the top level of your Based code (like
state = {}) are automatically persisted across conversation turns. The session maintains the complete execution state, so your agent can pick up right where it left off.Pre-populated State for Voice Calls
When your Based agent is deployed for voice, thestate dictionary is automatically pre-populated with call metadata. This gives you immediate access to caller information without needing to ask for it.
Inbound Voice Calls
For inbound calls,state is pre-populated with Twilio call data:
Outbound Voice Calls
For outbound calls,state includes all the same call metadata plus an outbound_data field containing the data that triggered the call:
outbound_data to personalize outbound calls:
The
outbound_data field contains whatever data you pass when initiating the outbound call through the API or trigger. This allows you to include any context needed for the conversation, such as customer details, appointment information, order numbers, etc.