Skip to main content

Local sessions

For local sessions, point localBrowser.launch() at a user data directory. Stagehand creates the directory if it does not exist and leaves it in place after the browser shuts down, so the next run starts from the same cookies and local storage:
When no user data directory is set, Stagehand launches Chrome against a temporary profile and deletes it on shutdown, so cookies and local storage do not carry over between runs. A directory you pass yourself is kept either way, so the setup above needs nothing extra.
The preserve flag governs only that generated temporary profile, never a directory you passed yourself:
preserveUserDataDir keeps the generated temporary directory after shutdown when you turn it on.

Browserbase sessions

For Browserbase sessions, use contexts to persist browser data:
A persisted profile carries cookies, tokens, and site data across runs. Treat the directory or context ID as a credential: keep it out of version control and scope it to a single automation.