---
title: "How to give a browser agent a persistent profile safely"
description: "Give a browser agent a persistent profile only when a saved login file is not enough. Use one profile per agent identity in its own directory, never a person's profile, one browser at a time, no saved passwords, the folder treated as a secret, and a known-good copy you restore when it drifts."
url: "https://www.aiagentsandbox.io/persistent-browser-profile-for-ai-agents/"
site: "AIAgentSandbox.io"
author: "AIAgentSandbox editors"
published: "2026-11-26"
updated: "2026-11-26"
---

# How to give a browser agent a persistent profile safely

AIAgentSandbox editors. Published 2026-11-26, updated 2026-11-26.

Give a browser agent a persistent profile only when a saved login file is not enough. Use one profile per agent identity in its own directory, never a person's profile, one browser at a time, no saved passwords, the folder treated as a secret, and a known-good copy you restore when it drifts.

To give a browser agent a persistent profile safely, treat the profile as a credential that belongs to one agent identity. Keep it in its own directory, open it in one browser at a time, store nothing in it the task does not need, and keep a known-good copy you can restore. This article is about the cases where persistence is needed; for the usual case, a fresh session per task, see [resetting browser state between sessions](https://www.aiagentsandbox.io/reset-browser-state-between-agent-sessions/).

## What is a persistent browser profile?

A persistent browser profile is a user data directory that the browser reuses between runs. Chromium's documentation says "The user data directory contains profile data such as history, bookmarks, and cookies, as well as other per-installation local state", and that "Each profile is a subdirectory (often `Default`) within the user data directory" ([Chromium, User Data Directory](https://chromium.googlesource.com/chromium/src/+/main/docs/user_data_dir.md), read 2026-11-26).

Playwright opens one with `launchPersistentContext`, which "Launches browser that uses persistent storage located at userDataDir and returns the only context". Its `userDataDir` is the "Path to a User Data Directory, which stores browser session data like cookies and local storage" ([Playwright BrowserType API](https://playwright.dev/docs/api/class-browsertype), read 2026-11-26).

## When does a browser agent need a persistent profile?

A browser agent needs a persistent profile only when a saved login file cannot carry the state the task depends on. A storage state file holds cookies and local storage, and that covers most logins.

Table: Saved login file compared with persistent profile

| Need | Saved login file (storageState) | Persistent profile |
|---|---|---|
| Stay signed in to a site | Yes | Yes |
| IndexedDB | Only if saved with `indexedDB: true` | Yes |
| Session storage | No | Kept while the browser runs |
| Installed extensions and their settings | No | Yes |
| Site permissions granted earlier | No | Yes |
| Starts clean apart from the login | Yes | No, everything accumulates |

If every row you need says Yes in the first column, use the file. The [Playwright authentication guide](https://playwright.dev/docs/auth) describes the file and its limits (read 2026-11-26).

## Step 1: Which account and directory should the profile use?

The profile should belong to one agent identity, signed in to an account made for the agent, in a directory nothing else uses.

1. **One identity per profile.** Never let two customers, two tasks of different trust, or two agents share a profile.
2. **An account made for the agent**, with the least rights the task needs. See [keeping credentials out of a browser agent](https://www.aiagentsandbox.io/keep-credentials-out-of-a-browser-agent/).
3. **Never a person's profile.** Playwright warns that "automating the default Chrome user profile is not supported" and that pointing at Chrome's main "User Data" directory "may result in pages not loading or the browser exiting" (Playwright BrowserType API, read 2026-11-26). Chrome made the same choice for security: the remote debugging switches "will no longer be respected if attempting to debug the default Chrome data directory", and "A non-standard data directory uses a different encryption key" ([Chrome remote debugging changes](https://developer.chrome.com/blog/remote-debugging-port), read 2026-11-26).
4. **A dedicated directory per identity**, for example `/profiles/<identity>`, readable only by the account the browser runs as (`chmod 700`).

## Step 2: How do I launch the agent's browser with the profile?

Launch the browser with the profile's directory and nothing that exposes it to other programs. These examples show the profile option of three common tools; the paths are examples.

Playwright (Node.js):

```js
import { chromium } from 'playwright';

const context = await chromium.launchPersistentContext('/profiles/billing-agent', {
  headless: true,
  serviceWorkers: 'block',
});
// ... the agent's task ...
await context.close();   // also closes the browser
```

Playwright MCP, with the option its README documents ([Playwright MCP README](https://github.com/microsoft/playwright-mcp), read 2026-11-26):

```bash
npx @playwright/mcp@0.0.86 --headless --user-data-dir=/profiles/billing-agent
```

Chrome DevTools MCP, whose default profile directory "is not cleared between runs" ([Chrome DevTools MCP advanced usage](https://github.com/ChromeDevTools/chrome-devtools-mcp/blob/main/docs/advanced-usage.md), read 2026-11-26):

```bash
npx chrome-devtools-mcp@1.14.0 --headless --userDataDir=/profiles/billing-agent --no-usage-statistics
```

Do not open a remote debugging port for a profile that holds a login unless the port is reachable only from inside the agent's sandbox. The Chrome DevTools MCP guide warns that with the port open, "Any application on your machine can connect to this port and control the browser."

## Step 3: How do I stop two browsers opening the same profile?

Stop two browsers opening the same profile with a lock in your own scheduler, because the browser's refusal is an error, not a queue.

- Playwright: "browsers do not allow launching multiple instances with the same User Data Directory" (Playwright BrowserType API).
- Playwright MCP: "A persistent profile can only be used by one browser instance at a time" (Playwright MCP README).
- Chrome DevTools MCP: "Only one browser can use it at a time" (advanced usage guide).

Take a lock named after the profile before launch, release it after the context closes, and make a second task for the same identity wait or fail clearly.

## Step 4: What should never be stored in the profile?

Never store passwords, payment details or a person's data in an agent's profile. Log in by code, then let the session cookies persist.

This matters most on Linux. Chromium's documentation says it can store passwords in GNOME Libsecret, KWallet or "plain text", that "Passwords stored in plain text are not encrypted", and that "Chromium will fall back to `basic` if a requested or autodetected store is not available" ([Chromium, Linux Password Storage](https://chromium.googlesource.com/chromium/src/+/main/docs/linux/password_storage.md), read 2026-11-26). A headless container usually has no desktop keyring, so assume anything saved there is readable by whoever can read the directory.

## Step 5: How do I protect the profile directory?

Protect the profile directory the way you protect a password, because cookies in it can sign in as the agent's account.

1. **Restrict file access** to the browser's user, and mount the directory only into that identity's sandbox.
2. **Encrypt it at rest** wherever it is stored between runs: the volume, the backup and any copy.
3. **Keep it out of logs, artifacts and images.** Do not bake a signed-in profile into a container image or attach it to a bug report.
4. **Limit where the browser can go** with a network allow-list, so an injected instruction cannot send the session elsewhere. See [restricting a browser agent's network access](https://www.aiagentsandbox.io/restrict-browser-agent-network-access/).

## Step 6: How do I keep the profile from drifting?

Keep the profile from drifting by restoring it from a known-good copy instead of letting every run add to it.

1. After signing in and setting up the profile once, close the browser and save a copy as the known-good version.
2. For each run, give the agent a fresh copy of the known-good version, never the copy another run used.
3. When the login expires, refresh it in the known-good version and save a new copy.
4. Compare a run's profile with the known-good one if a task starts behaving differently. New extensions, permissions or cookies for unexpected sites are signs of trouble.

Copy only a closed profile. Copying while a browser has it open can capture files mid-write.

## How do I check the profile setup is safe?

Check the setup with a short test before giving the profile to an agent.

1. Start a second browser on the same profile while the first is running. It should be refused, and your lock should have stopped it earlier.
2. Open the browser's saved passwords page in the profile. It should be empty.
3. From inside the sandbox, try to reach a site that is not on the allow-list. It should fail.
4. Start a run from the known-good copy and confirm the login works and nothing from the previous run is present.

## What we read for this article

- [Chromium, User Data Directory](https://chromium.googlesource.com/chromium/src/+/main/docs/user_data_dir.md), read 2026-11-26
- [Chromium, Linux Password Storage](https://chromium.googlesource.com/chromium/src/+/main/docs/linux/password_storage.md), read 2026-11-26
- [Playwright BrowserType API](https://playwright.dev/docs/api/class-browsertype), read 2026-11-26
- [Playwright authentication guide](https://playwright.dev/docs/auth), read 2026-11-26
- [Playwright MCP README](https://github.com/microsoft/playwright-mcp), read 2026-11-26
- [Chrome DevTools MCP advanced usage guide](https://github.com/ChromeDevTools/chrome-devtools-mcp/blob/main/docs/advanced-usage.md), read 2026-11-26
- [Chrome remote debugging changes](https://developer.chrome.com/blog/remote-debugging-port), read 2026-11-26

## FAQ

### What is a persistent browser profile?

It is a user data directory that the browser keeps on disk between runs, holding cookies, local storage, history, settings and extensions. Chromium's documentation describes the user data directory as containing profile data such as history, bookmarks and cookies.

### Can two agents use the same profile at once?

No. Playwright's documentation says browsers do not allow launching multiple instances with the same user data directory, and both Playwright MCP and Chrome DevTools MCP say a profile can only be used by one browser at a time.

### Can I point an agent at my own Chrome profile?

Do not. Playwright says automating the default Chrome user profile is not supported, and since Chrome 136 the remote debugging switches are ignored for the default data directory. It would also give the agent every site you are signed in to.

### Are passwords in a Linux browser profile encrypted?

Not always. Chromium on Linux picks a password store from the desktop environment and falls back to a plain-text store when none is available, which is common in a container. Keep passwords out of the agent's profile.
