Skip to main content

Home › All stories

How to record and replay a browser agent's session

The short answerRecord a browser agent's session with a Playwright trace for debugging (actions, DOM snapshots, screenshots, network, console), add a video or an rrweb DOM replay for people who watch it, and save a HAR file to replay the network later. Tie each recording to the agent's step log.

By · Published · Updated · 5 min read

To record and replay a browser agent's session, capture a Playwright trace for debugging, add a video or a DOM replay for people who need to watch, and keep a HAR file if you want to replay the network. Then link each recording to the agent's own log of what it decided at each step.

Why record a browser agent's session?

Record a browser agent's session because its result alone does not tell you why it succeeded or failed. An agent's run has two halves: what the model decided, and what the browser showed and did. A recording of the browser half answers questions the model log cannot:

  • Did the page load fully before the agent clicked?
  • Was the element the agent chose the one the model described?
  • Did a pop-up, a slow API or a login prompt get in the way?
  • Did the agent visit anything it should not have?

Benchmark authors keep the same evidence. Mind2Web stores, for each task, an MHTML copy, a DOM snapshot, a screenshot, a HAR of "all network traffic for replay" and the Playwright trace (Mind2Web repository, read 2026-11-24).

What are the ways to record a browser session?

There are four common ways to record a browser session, and they answer different questions.

Table: Ways to record a browser agent session

MethodWhat it capturesBest forSize
Playwright traceEach action, DOM snapshots, screenshots, network, consoleDebugging a failed stepMedium
VideoThe viewport as a moving picturePeople watching a runLarge
DOM replay (rrweb)The page structure and every change, plus input eventsWatching and inspecting, with maskingSmall to medium
HAR fileEvery network request and responseReplaying or mocking the networkSmall to large

Step 1: How do I record a Playwright trace?

Record a Playwright trace by starting tracing on the browser context before the agent acts and stopping it to a file at the end.

await context.tracing.start({ screenshots: true, snapshots: true });
// the agent runs its steps here
await context.tracing.stop({ path: 'trace.zip' });

What the trace holds, from Playwright's documentation (Playwright trace viewer, read 2026-11-24):

  • "Playwright captures a set of complete DOM snapshots for each action."
  • "each trace records a screencast and renders it as a film strip."
  • "The Network tab shows you all the network requests that were made during your test."
  • Console logs "from the browser as well as from your test".

The screenshots option builds the timeline preview, and snapshots: true "is a shortcut for { dom: true }" (Playwright Tracing API, read 2026-11-24). Open the result with npx playwright show-trace trace.zip.

For long agent runs, use trace chunks (tracing.startChunk() and stopChunk()) to write one file per task or per group of steps, so a single file does not grow without limit.

Step 2: How do I record a video of the session?

Record a video by setting recordVideo when you create the context. Playwright "Enables video recording for all pages into recordVideo.dir directory" (Playwright Browser API, read 2026-11-24). Two details matter (Playwright videos, read 2026-11-24):

  1. "Videos are saved upon browser context closure", so close the context before collecting the file.
  2. "The video size defaults to the viewport size scaled down to fit 800x800." Set the size explicitly if people need to read small text.

For a live view rather than a file, Chromium's DevTools Protocol has Page.startScreencast, which "Starts sending each frame using the screencastFrame event" as compressed images. It is marked experimental (DevTools Protocol Page domain, read 2026-11-24).

Step 3: How do I record a DOM replay with rrweb?

Record a DOM replay by running rrweb's recorder in each page of the session and sending its events to storage. rrweb describes itself as "'record and replay the web', which is a tool for recording and replaying users' interactions on the web". Its recorder "builds on an initial snapshot to record all HTML state changes (mutations) and user interactions", and its player "rebuilds the inital snapshot and replays the list or stream of subsequent events" (rrweb repository, read 2026-11-24).

For an agent, inject the recorder with context.addInitScript() so it runs on every page, and set its privacy options (rrweb guide, read 2026-11-24):

  • maskAllInputs: true to "mask all input content as *".
  • maskInputOptions already masks password fields by default.
  • blockClass (default rr-block) or blockSelector for areas that must not be recorded.

Note that the page itself can see an injected recorder, so keep it to sessions you control.

Step 4: How do I record and replay the network with HAR?

Record the network as a HAR file when you want to replay a session against the same responses. Playwright explains: "A HAR file is an HTTP Archive file that contains a record of all the network requests that are made when a page is loaded" (Playwright mock APIs, read 2026-11-24).

  1. Record with context.routeFromHAR('session.har', { update: true }), or with the recordHar context option.
  2. Replay by calling routeFromHAR again without update. Requests that match are served from the file.

Replaying the network makes a failed run repeatable even after the live site changes. Benchmarks use the same idea: WebArena-Verified scores runs from the agent's response plus a network trace, so evaluation works "without requiring live web environments using network trace replay" (WebArena-Verified repository, read 2026-11-24).

Link recordings to decisions by giving every step one ID that appears in both the agent's log and the browser recording.

  1. Create a run ID for each task and put it in the file names of the trace, video, replay and HAR.
  2. Number each agent step. Log the model's input summary, its chosen action and the result under that number.
  3. Before each action, add the step number to the trace, for example with a named trace group, so the trace viewer shows it.
  4. Store the model log and the recordings together, and keep both for the same period.

With this, a reviewer can open step 14 in the model log, see why the agent chose to click, and jump to step 14 in the trace to see what the page actually looked like.

How should recordings be stored and kept?

Store recordings as sensitive data, with access controls and a deletion date. Traces and HAR files contain network requests, including cookies and authorisation headers, and videos show everything on screen. Start recording after login, mask inputs, filter secrets from logs, and delete recordings once they are no longer needed for debugging or audit. Our guide to keeping credentials out of a browser agent covers the masking steps.

What we read for this article

Frequently asked

What is the difference between a Playwright trace and a video?

A video is a moving picture of the viewport. A trace stores each action with DOM snapshots, screenshots, network requests and console messages, so you can inspect the page at any step, not only watch it.

How do I open a Playwright trace?

Run npx playwright show-trace trace.zip, or load the file in the Playwright trace viewer web page, which opens it locally in the browser.

What is DOM-based session replay?

A recording of the page's structure and every change to it, which a player rebuilds as a live page. rrweb is the common open-source tool. It is smaller than video and lets you mask inputs and block elements at capture time.

Do recordings contain sensitive data?

Often. Traces and HAR files include network requests, which can carry cookies and tokens, and videos show whatever was on screen. Start recording after login, mask inputs, and store recordings with access controls and a deletion date.