AI agent instructions
AI agent instructions
Session States
Sessions transition through these states:authenticating- Session was started with Managed Auth connections and login is still in progressactive- Session is running and ready to accept operationsclosed- Session has been terminated normallyerror- Session encountered an error and terminated unexpectedlytimed_out- Session hit its idle timeout or maximum duration
Creating a Session
Create a new session using the context manager pattern (recommended):Getting Session Details
Retrieve current information about your session:string
Unique identifier for the session
string
Current status:
authenticating, active, closed, error, or timed_outdatetime
Session creation timestamp
datetime | None
Session closure timestamp (None if still active)
datetime
Last operation timestamp
integer
Idle timeout in minutes. The session closes after this long without any operation.
integer
Maximum session lifetime in minutes, regardless of activity
string | None
Why the session closed:
manual, idle_timeout, max_duration, error, or unknown (None while active)timedelta
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
Python’swith statement and TypeScript’s use() callback guarantee cleanup:
2. Set Appropriate Timeouts
Sessions have two limits:idle_timeout_minutes(default 3, max 30) - closes the session after this long without any operation. Each operation resets it.max_duration_minutes(default 15, max 1440) - closes the session after this long, regardless of activity.
max_duration_minutes first for long workflows. Set the idle timeout to cover the longest pause between operations, for example while an agent thinks or a page loads.
3. Monitor Session State
Check session status before long-running operations: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