← Blog

BLOG

Run Browser Remotely: A Practical Guide for AI Agents

Learn how to run browser sessions remotely for AI agents: hosted Chromium, CDP access, Playwright wiring, and the production criteria that matter.

9 min readRemote Browser
Ivory computer towers connected across terracotta platforms in a quiet desert landscape
The infrastructure behind your browserREMOTE BROWSER / ILLUSTRATION

To run browser sessions for an AI agent, you need a real Chromium instance that the agent can drive over a stable protocol, observe while it works, and tear down cleanly when the task ends. The most reliable way to do that is to run the browser on hosted infrastructure and connect to it over the Chrome DevTools Protocol (CDP), rather than launching Chrome on the same machine as your agent loop. This guide covers what "run browser" actually means in an agent context, how the connection works, and the production criteria that separate a demo from a system you can leave running.

If you are still deciding between local and hosted execution, the remote browser overview is a good starting point. This article assumes you have chosen to run the browser remotely and focuses on doing it correctly.

What "run browser" means for an agent

A browser is not a single process. When you run browser automation, several things have to be true at once:

  • A real rendering engine executes JavaScript, loads CSS, and runs the page the way a human would see it.
  • A control channel lets your code or agent issue navigation, input, and evaluation commands.
  • A session boundary isolates one task's cookies, storage, and tabs from another's.
  • An observation path lets you watch what is happening, either live or through logs and recordings.
  • A lifecycle starts the browser, keeps it alive for the duration of the task, and releases it afterward.

When people say "run browser" in the context of AI agents, they usually mean all five. A headless Chrome binary launched from a shell script satisfies the first point and part of the second, but it does not give you isolation, observability, or a clean lifecycle. That gap is why hosted runtimes exist.

The distinction matters because agent workloads are long-lived and unpredictable. An agent may open a dozen tabs, fill a multi-step form, hit a CAPTCHA, retry, and then need to resume from a saved login on the next run. A browser you launched locally for a one-off test will not survive that pattern.

Why run the browser remotely instead of locally

Running Chromium locally works until it doesn't. The failure modes are well known to anyone who has shipped browser automation:

ConcernLocal browserHosted remote browser
Environment parityDepends on the developer's OS and Chrome versionConsistent Chromium image per session
ScalingLimited by the host machine's CPU and memorySessions provisioned on demand
Session isolationManual; easy to leak cookies between runsIsolated per session by default
ObservabilityScreenshots and logs you wire yourselfLive viewer plus CDP and recordings
PersistenceLocal profile directories you managePersistent profiles managed by the runtime
Network identityYour machine's IPConfigurable proxy settings
CleanupYou kill the process; orphans happenSession lifecycle handled by the API

The table is not an argument that local is always wrong. For a quick script that runs once, local Chrome is fine. The trade-off flips when you run browser sessions on a schedule, in parallel, or as part of an agent that a user is waiting on.

Hosted runtimes also solve a subtler problem: version drift. If your agent's prompts and tool definitions assume a particular Chromium behavior, you want the same build every time. A hosted runtime pins the browser image so a Chrome auto-update on someone's laptop does not silently change how your agent behaves.

How the connection works: CDP over WebSocket

The Chrome DevTools Protocol is the interface Chromium exposes for external control. Playwright, Puppeteer, Selenium (via a bridge), and most agent frameworks ultimately speak CDP or a layer on top of it. When you run browser sessions remotely, you connect to a WebSocket endpoint that the runtime exposes and drive the browser through that channel.

The Chrome DevTools Protocol documentation is the authoritative reference for the domains and commands available. In practice you rarely call CDP directly; your automation library does. But understanding that CDP is the transport explains why the same hosted session can be driven by Playwright, Puppeteer, or an agent framework without re-provisioning the browser.

A minimal Playwright connection looks like this:

import { chromium, Browser, BrowserContext, Page } from "playwright";

interface SessionInfo {
  cdpUrl: string;
  sessionId: string;
}

async function runBrowserTask(session: SessionInfo): Promise<void> {
  // Connect to the hosted Chromium instance over CDP.
  const browser: Browser = await chromium.connectOverCDP(session.cdpUrl, {
    timeout: 30_000,
  });

  try {
    // Reuse the default context so persistent profile state is available.
    const context: BrowserContext = browser.contexts()[0];
    const page: Page = context.pages()[0] ?? (await context.newPage());

    await page.goto("https://example.com", { waitUntil: "domcontentloaded" });

    // Expose a stable signal to the agent loop rather than raw HTML.
    const title = await page.title();
    const hasLoginForm = await page.locator("form[action*='login']").count();

    console.log(JSON.stringify({ title, hasLoginForm, sessionId: session.sessionId }));

    // Keep the session open for follow-up steps; close only when the task ends.
  } finally {
    // Disconnect without killing the remote browser if the task continues.
    await browser.close();
  }
}

Two details in that snippet matter more than they look. First, connectOverCDP attaches to an existing browser rather than launching one, which is the whole point of a remote runtime. Second, browser.close() on a CDP connection disconnects your client; whether the remote session terminates depends on the runtime's lifecycle policy. Check the documentation for how your runtime handles disconnect versus explicit session termination, because getting this wrong is a common source of leaked sessions and surprise usage.

Production criteria for running browser sessions

Once the connection works, the questions shift from "can I drive it" to "can I operate it." These are the criteria worth evaluating before you commit.

Session isolation and profiles

Every task should start from a known state unless you deliberately want continuity. Isolated sessions prevent one agent's cookies from leaking into another's. Persistent profiles are the opposite tool: they let an agent resume a logged-in state across runs. A good runtime supports both and lets you choose per task. If you are running a workflow that logs in once and then performs many actions, a persistent profile saves you from re-authenticating on every step.

Observability

When an agent fails, you need to see why. A live viewer that streams the browser's screen is the fastest debugging tool available, and it doubles as a way to let a human intervene mid-task. Recordings and CDP-level logs cover the cases where you were not watching. If your runtime only gives you a final screenshot, you will spend a lot of time guessing.

Network configuration

Many real tasks depend on where the request originates. Configurable proxy settings let you route sessions through a specific region or network identity. Be careful with claims here: "stealth" means different things to different vendors, and configurable browser settings are not the same as guaranteed evasion. Evaluate what you actually need rather than what the marketing page promises.

Usage controls and cost

Browser sessions consume compute for as long as they run. A session left open by a crashed agent keeps billing. Look for usage controls, idle timeouts, and clear metering. Pricing models vary, so check /pricing for current rates rather than relying on figures quoted in older articles.

Concurrency and scheduling

If you run browser sessions in parallel, the runtime needs to provision them without queueing behind a fixed pool. Ask how concurrency is bounded and what happens when you exceed it. A runtime that silently throttles is worse than one that returns a clear error.

Common mistakes when you run browser remotely

A few patterns show up repeatedly in production incidents:

  • Not closing sessions on error paths. Wrap task logic in try/finally and terminate the session explicitly. Relying on garbage collection or process exit does not work when the browser lives on someone else's server.
  • Treating the CDP connection as the session. Disconnecting your Playwright client is not the same as ending the browser. Decide which one your task needs.
  • Ignoring page load semantics. waitUntil: "load" can hang on pages with long-polling resources. Prefer domcontentloaded plus explicit locator waits.
  • Assuming a fixed viewport. Agent frameworks often assume a desktop viewport. Set it explicitly if the target site is responsive.
  • Skipping the live viewer during development. Watching the browser while you build the agent catches selector and timing bugs far faster than reading stack traces.

When a hosted runtime is the right call

Run browser sessions on hosted infrastructure when any of the following is true:

  • The agent runs on a schedule or on behalf of users, so reliability matters more than setup speed.
  • You need to run many sessions in parallel and do not want to manage a fleet of Chrome containers.
  • You need persistent profiles, proxy configuration, or live observation without building each from scratch.
  • You want a consistent Chromium version across environments.

Stay local when you are prototyping a single script, when the task is short and one-off, or when you have strict data-residency requirements that a hosted runtime cannot meet. Those are legitimate reasons, and a good runtime should not pretend otherwise.

For teams moving from a local prototype to something durable, the practical path is to keep the agent logic identical and swap the browser launch for a CDP connection. That is the smallest possible change and it preserves everything you have already tested. The remote browser online guide walks through that migration in more detail, and remote control browser covers the control-plane side once you have multiple sessions to manage.

A checklist before you ship

Before you run browser sessions in production, confirm each of these:

  1. Connection: your client connects over CDP with a timeout and retry policy.
  2. Lifecycle: sessions are terminated explicitly on success and on failure.
  3. Isolation: each task gets a fresh session unless continuity is intentional.
  4. Observation: you can watch a live session and retrieve logs after the fact.
  5. Network: proxy and region settings are configured per task where needed.
  6. Cost: you know what a session costs and have idle timeouts in place.
  7. Versioning: the Chromium build is pinned so behavior does not drift.

If you can check all seven, you have moved past "it runs on my machine" and into something you can operate. That is the real meaning of "run browser" for an AI agent: not a single successful launch, but a repeatable, observable, and bounded execution environment. Start with the documentation to wire up your first hosted session, and keep the checklist handy as you scale.