Skip to main content
You can attach a CDP client to a live V4 browser and record its network events. This is a live observer, not a download of everything that happened during a run. V4 run events describe agent activity; they are not the browser’s HTTP request log.

Find the live browser

For an agent run, watch its ordered run events for browser.ready or browser.reattached. The event’s data.browser_session_id identifies the browser. You can also list the session’s browsers with GET /api/v4/browsers?agentSessionId=SESSION_ID. A session can use more than one browser over time, so do not assume the first browser lasts for every follow-up. Use an API key with browser read access in the same project to retrieve the active browser’s cdpUrl. BROWSER_SESSION_ID is a browser ID, not a run ID or agent session ID.
For a standalone V4 browser, you can use cdpUrl from the create response instead. A stopped browser has no live CDP URL. Check the HTTP status and browser state if discovery fails; do not create a replacement browser and mistake it for the agent’s browser.
Treat the CDP URL as a credential. Anyone who can connect can control the browser and read its session. Keep it out of logs, tickets, source control, and shared shell history.

Record metadata from existing pages

Install Playwright with pip install playwright. No local browser download is needed because this example connects to an existing browser. Save this as capture_network.py, then run python capture_network.py. It records for 30 seconds and creates network.jsonl with owner-only permissions. It refuses to overwrite an existing file. Start it before the activity you want to observe.
Each request is correlated by (page, requestId). A response row gives the HTTP status; a finished row gives transferred bytes. Keep the event sequence: redirects can produce more than one request event with the same request ID. Empty output means no matching events were observed, not that the run made no requests. The example does not navigate, intercept requests, change the cache, or stop the browser. It drops URL paths, queries, credentials, headers, cookies, and request and response bodies. HTTP origins retain non-default ports; malformed and non-HTTP URLs have a null origin. Origins can still be sensitive; review the file before sharing it. Keep a bounded capture duration and a retention policy.

Coverage and lifecycle

  • Capture starts only after Network.enable for each attached page. Earlier traffic is not reconstructed, and attaching after browser.ready can miss startup requests.
  • This small example attaches only to pages present at startup. Popups, new tabs, workers, service workers, and some cross-process frames need separate target handling. It is not complete browser-wide capture.
  • Reattach if V4 provisions a new browser. A logger attached to the old browser cannot observe the replacement.
  • JSONL events are not HAR. A HAR exporter needs its own request, redirect, timing, and body handling. Network.getResponseBody is a separate, optional CDP call; bodies may be unavailable and can contain secrets or customer data.
  • Detaching an observer does not stop a browser or end its billing. Leave an agent-owned browser under the agent’s lifecycle. For a standalone browser you created, stop it through the browser API when finished.
See the CDP Network reference and Playwright CDP sessions for event fields and target-specific behavior.