To reset browser state between AI agent sessions, start each session from nothing and add back only what the task needs. In practice that means a new browser context, or a new browser in a new container, for every task, plus a saved login state file when the task must be signed in.
Why does browser state need resetting between agent sessions?
Browser state needs resetting because anything left over changes what the next session sees and can leak data between tasks. Left-over state causes three kinds of trouble for agents:
- Wrong results. A cart, a draft or a filter from the last task is still there, and the agent works from it.
- Data leaks. One user's cookies or form history reach a session run for another user.
- Unrepeatable runs. The same task gives different results depending on what ran before. Benchmarks treat this seriously: WebArena's instructions say to "reset the environment to the initial state" after a full evaluation (WebArena repository, read 2026-11-24).
What state does a browser keep?
A browser keeps far more than cookies. A full reset has to cover all of these:
Table: Where a browser keeps state, and what resets it
| State | Where it lives | Reset by a new context | Reset by a new browser and profile |
|---|---|---|---|
| Cookies | Profile | Yes | Yes |
| Local storage and session storage | Profile, per origin | Yes | Yes |
| IndexedDB | Profile, per origin | Yes | Yes |
| Service workers and their caches | Profile, per origin | Yes | Yes |
| HTTP cache | Profile | Yes | Yes |
| Permissions granted to sites | Profile | Yes | Yes |
| Downloaded files | File system | No | Only if the file system is new too |
| Browser-wide settings and extensions | Browser launch | No | Yes |
The last two rows explain why a container per session is the strongest reset. A new context resets the web state, but files on disk and the browser process itself survive it.
Option 1: How do I reset state with a new browser context?
Create a new browser context for each session and close it at the end. Playwright describes contexts as "equivalent to incognito-like profiles", "fast and cheap to create" and "completely isolated, even when running in a single browser" (Playwright browser contexts, read 2026-11-24). It also says "Non-persistent browser contexts don't write any browsing data to disk" (Playwright BrowserContext API, read 2026-11-24).
Puppeteer has the same idea. browser.createBrowserContext() "Creates a new browser context" that "won't share cookies/cache with other browser contexts" (Puppeteer createBrowserContext, read 2026-11-24).
The steps:
- Launch one browser per worker.
- For each agent task, call
browser.newContext()(Playwright) orbrowser.createBrowserContext()(Puppeteer). - Open pages only inside that context.
- At the end of the task, close the context. Its cookies, storage and cache go with it.
This is quick and works well when every task belongs to the same customer. When tasks belong to different customers, a shared browser process is a shared failure point. Use option 2.
Option 2: How do I reset state with a new browser in a new container?
Start a new container with a new browser for each session and delete it at the end. This resets the web state, the file system, the browser process and its settings in one step. It is the reset that research environments use for full desktops: OSWorld restores each virtual machine from a snapshot, noting that "The snapshot feature also enables efficient reset of the virtual environment" (OSWorld paper, section 2.2, read 2026-11-24).
The cost is start-up time. A browser container takes longer to start than a context, so many teams keep a small pool of fresh containers ready and never return a used one to the pool.
How do I keep an agent logged in after a reset?
Keep an agent logged in by saving the login state once and loading it into each new context.
- Sign in once, by script or by hand, in a clean context.
- Save the state:
context.storageState({ path: 'state.json' }). Playwright's storage state "contains current cookies, local storage snapshot" and more (Playwright BrowserContext API, read 2026-11-24). - For each task, create the context with
browser.newContext({ storageState: 'state.json' }). Playwright says this option "can be used to initialize context with logged-in information" (Playwright Browser API, read 2026-11-24).
Three details from Playwright's authentication guide matter here (Playwright authentication, read 2026-11-24):
- IndexedDB "is not included in the snapshot by default". Set
indexedDB: truewhen saving if the site keeps its login there. - "Playwright does not provide API to persist session storage". Sites that keep login in session storage need a small script to save and restore it.
- "The browser state file may contain sensitive cookies and headers that could be used to impersonate you or your test account." Treat the file as a secret. Our guide to keeping credentials out of a browser agent covers how.
What about persistent profiles?
A persistent profile keeps state on disk between sessions, so use one only when a task needs it. Playwright's launchPersistentContext "Launches browser that uses persistent storage located at userDataDir and returns the only context" (Playwright BrowserType API, read 2026-11-24). The same page notes that passing an empty string creates a temporary directory, which gives a persistent-style launch with a throwaway profile.
Two rules keep persistent profiles safe:
- One identity per profile. Never let two customers, or two unrelated agents, share a profile.
- Never the real user's profile. Playwright says "automating the default Chrome user profile is not supported", and since Chrome 136 the remote debugging switches "must now be accompanied by the
--user-data-dirswitch to point to a non-standard directory" (Chrome remote debugging changes, published March 17, 2025, read 2026-11-24).
How do I check that a reset worked?
Check a reset by testing for state before the agent starts.
- In a new session, read
document.cookie,localStorage.lengthand the list of IndexedDB databases on the target site. They should be empty, apart from the login state you loaded. - List the download directory. It should be empty.
- Run the same task twice in a row from a fresh start. The two runs should begin from identical pages.
- Log the context or container ID with each task, so a result can always be traced to the session that produced it.
What we read for this article
- Playwright browser contexts, read 2026-11-24
- Playwright BrowserContext API, read 2026-11-24
- Playwright Browser API, read 2026-11-24
- Playwright BrowserType API, read 2026-11-24
- Playwright authentication, read 2026-11-24
- Puppeteer createBrowserContext, read 2026-11-24
- Chrome remote debugging changes, read 2026-11-24
- WebArena repository, read 2026-11-24
- OSWorld paper (arXiv 2404.07972), read 2026-11-24
Frequently asked
What is a browser context?
A browser context is an isolated, incognito-like session inside one browser. Each context has its own cookies, local storage and session storage. Playwright describes contexts as fast and cheap to create and completely isolated, even when they run in a single browser.
Is clearing cookies enough to reset a browser?
No. Local storage, session storage, IndexedDB, service workers, the HTTP cache and downloaded files can all carry state between sessions. A new context or a new browser resets them together.
How do I keep an agent logged in without reusing a profile?
Save the login once with storageState, which captures cookies, local storage and optionally IndexedDB, and load that file into each new context. Store the file as a secret, because it can be used to impersonate the account.
When should I use a persistent profile?
When a task genuinely needs long-lived browser state, such as an extension's settings or a site that ties trust to a device. Give each persistent profile to one agent identity only, and reset it on a schedule.