Session States
Sessions transition through these states:active- Session is running and ready to accept operationsclosed- Session has been terminated normallyerror- Session encountered an error and terminated unexpectedlytimed_out- Session exceeded its timeout duration
Creating a Session
Create a new session using the context manager pattern (recommended):Getting Session Details
Retrieve current information about your session:Unique identifier for the session
Current status:
active, closed, error, or timed_outSession creation timestamp
Session closure timestamp (None if still active)
Last operation timestamp
Session timeout in minutes
Total session duration
Listing Sessions
Query all your active sessions:Stopping a Session
Sessions are automatically stopped when using the context manager, but you can also stop them manually:Complete Lifecycle Example
Here’s a complete example demonstrating best practices for session management:Connecting to Existing Sessions
You can reconnect to an existing session using its session_id:Error Handling
The context manager automatically handles cleanup, even when errors occur:Best Practices
Follow these patterns to build reliable, cost-effective automation:1. Always Use Context Managers
Thewith statement guarantees cleanup:
context_manager.py
2. Set Appropriate Timeouts
Match timeout to task duration with a buffer for delays:- Quick tasks: 3-5 minutes (default is 3)
- Data scraping: 10-15 minutes
- Long workflows: 20-30 minutes
timeout_example.py
3. Monitor Session State
Check session status before long-running operations:monitor_state.py
4. Handle Cleanup Gracefully
Even with context managers, handle interrupts:handle_interrupts.py
Session Lifecycle Diagram
Next Steps
Session Configuration
Configure sessions with advanced options
Recordings
Record and replay session activity
Live View
Watch sessions in real-time
Cookies
Persist authentication across sessions

