state object that persists between execute() calls, enabling stateful workflows without interference from other agents.
What is a Session?
A session is an isolated execution environment with:- Persistent state:
stateobject that survives across multipleexecute()calls - Shared browser context: All sessions see the same
context.pages()(browser tabs) - Independent execution: Code runs in a separate VM sandbox per session
Why Use Sessions?
Multi-agent isolation: When multiple AI agents automate the browser simultaneously, sessions prevent them from overwriting each other’s state.
Session Management
Create a New Session
-s to all subsequent commands.
List All Sessions
state objects.
Reset a Session
State Persistence
Source:playwriter/src/skill.md (context variables section)
The state object is session-isolated but persists between execute() calls within the same session:
- Playwright
PageandFrameobjects - Extracted data (arrays, objects, strings)
- Event listeners (but clean them up with
state.page.removeAllListeners()when done) - CDP sessions (
state.cdp = await getCDPSession({ page: state.page }))
- Browser-side functions (use
page.evaluate()on each call instead) - Element handles (they become stale - use locators or re-query on each call)
Pages are Shared, State is Not
All sessions see the samecontext.pages() array (all browser tabs with Playwriter enabled). If another agent navigates or closes a page you’re using, you’ll be affected.
Best practice: Create your own page and store it in state:
Multi-Agent Workflows
Scenario: Two agents automate different tasks on the same browser simultaneously. Agent A (session 1): Scraping product data from e-commerce siteSession State Implementation
Source:playwriter/src/relay-state.ts
Sessions are managed by the relay server using a Zustand store:
state object in the execution sandbox.
Session Lifecycle
- Create:
playwriter session newassigns a unique ID - Execute: Multiple
playwriter -s <id> -e '...'calls persist state - Reset:
playwriter session reset <id>clears connection but keeps session ID - Implicit cleanup: Sessions expire when the relay server restarts (state is lost)
Sessions do NOT persist across relay server restarts. If you restart the server, all session state is lost and you must create new sessions.
When to Use Sessions
Use sessions when:- Multiple agents automate the browser simultaneously
- You need to persist data between multiple automation steps
- You want to isolate page navigation from other agents
- You’re running a single one-off command (
playwriter -e 'await page.goto(...)'works fine) - You’re controlling a single tab that no other agent touches
Related
- Architecture - How relay server manages sessions
- State Management - Relay state structure