All posts

agent-browser: handing an AI agent a browser

A terminal window connected by a thin line to a browser window showing a small tree of elements labelled @e1, @e2, and @e3

Give an AI coding agent a web page to build and the code shows up quickly. The hard part comes after. Whether the button actually works, or whether the layout holds up on a phone, only becomes clear when someone opens a browser and looks. If that someone is always a person, you lose half the point of using an agent.

There are a few ways to let the agent check its own work. Feeding it screenshots is slow and burns tokens. Asking it to write Playwright scripts tends to end with it inventing CSS selectors that don’t match. We use agent-browser from Vercel Labs.

What it is

It’s a command-line tool written in Rust, under the Apache-2.0 license. The first command starts a daemon in the background, and that daemon drives Chrome through the Chrome DevTools Protocol. After that you run short commands like agent-browser click ... one at a time, and the browser stays open in between. That matches how coding agents work anyway: run a command, read the output, decide what to do next.

Installing it:

npm install -g agent-browser   # or: brew install agent-browser
agent-browser install          # downloads Chrome for Testing, first time only

Snapshots and refs

This is the part we like most. Instead of taking a picture of the page, snapshot returns the accessibility tree as text and gives each element a short reference like @e1:

Page: Example - Log in
URL: https://example.com/login

@e1 [heading] "Log in"
@e2 [form]
  @e3 [input type="email"] placeholder="Email"
  @e4 [input type="password"] placeholder="Password"
  @e5 [button type="submit"] "Continue"

The agent uses those references directly:

agent-browser open https://example.com/login
agent-browser snapshot -i              # interactive elements only
agent-browser fill @e3 "me@example.com"
agent-browser click @e5
agent-browser wait --text "Welcome"    # wait for the result, not for a number of seconds
agent-browser snapshot -i              # the page changed, so read it again

A few lines of text cost far fewer tokens than a screenshot, and the agent never has to guess coordinates or make up selectors. The accessibility tree is what screen readers use, so if the agent can’t find an element, that’s often an accessibility bug too. You can also skip the snapshot and find things by role and name, as in find role button click --name "Submit".

Other features

That loop covers most of it, but a few other features have become part of our routine.

Adding --session <name> starts a separate browser, so several agents can work at once without stealing each other’s tabs. read <url> pulls a documentation page as clean text without launching Chrome. diff snapshot compares the page before and after an action, and console and errors show what the page logged. set viewport and set device change the screen size, and you can emulate dark mode or going offline. When a person needs to see the result, there’s screenshot, pdf, and record start.

It can also mock network responses, save login state, and drive Electron apps or Safari in the iOS Simulator. The CLI ships with usage docs that match the installed version (agent-browser skills get core). Having the agent read those first cuts down on it calling commands that don’t exist any more. There’s an MCP server mode as well.

How we used it on this site

Our goal is to build fast together with AI and still build well, and agent-browser is one of the tools that keeps the “well” part honest. We built this site with an agent too. Whenever it changed a page, it opened the local preview, took a snapshot, clicked through what it had changed, and only then handed it back to us.

On the day we split the site into English and Korean, the agent checked the header at 375px and found that the new language link pushed the menu over far enough that “Work” broke into “Wo” and “rk”. It fixed the CSS, reopened the page at the same width, confirmed the menu now dropped below the logo, and then reported back. We never saw the bug ourselves.

A few habits we picked up along the way. Every task gets its own session name, because the default session is shared by everything running on the machine. We only take screenshots for things the DOM can’t show, like a canvas, or when a person really needs to look. Instead of sleep 3, the agent waits for something specific, like wait --text or wait @e4; when runs were flaky, the wait was almost always the cause. And we don’t trust what’s written on the page. A page can contain text meant to trick an agent, so we limit where it can go with --allowed-domains and keep passwords and other secrets out of the conversation.

Worth a try

If you use an AI agent for anything with a web UI, we’d recommend giving it a go. Once the agent stops at “I checked it” instead of “I changed it”, there’s a lot less you need to watch over.

The project moves fast, though. Pin the version if you use it in CI, and trust the docs bundled with the CLI over this post. We wrote this against version 0.43.0.

Sources