跪拜 Guibai
← Back to the summary

Stagehand: AI Browser Automation with Self-Healing Selectors and Deterministic Replay

This open-source AI browser automation framework Stagehand is amazing!

Hello everyone, I am Kuang Shi.

Today I'm sharing an open-source project: Stagehand, an AI browser automation framework from Browserbase.

image

Anyone doing Agent development will eventually face the problem: an Agent has to operate web pages, log into backends, fill forms, scrape data from pages. Doing this by hand with a mouse is too slow, and relying on hard-coded scripts is too fragile. Stagehand is designed for this.

What is Stagehand?

For this project, just remember a few key facts:

image

What it does is simple, summarized in one sentence: a browser SDK for AI Agents, where finding elements, clicking buttons, filling forms, and extracting data are all replaced with natural language instructions. The API follows Playwright's conventions: goto, click, locator, screenshot, all present.

The difference from Playwright is in positioning.

The official docs have a comparison table and a migration guide from Playwright, so existing test cases can be moved over.

act, observe, and extract: three actions

The core of the framework is three methods.

act: Execute actions, with instructions written in plain language. For example, "click the login button". The model finds the corresponding element on the page and clicks it. If the page changes, if the button moves or changes text, the next time it executes it will re-locate that element. This is what they call self-healing.

observe: Only look, don't act. Ask "what can be clicked on this page?" and it returns a set of real selectors. The login example in the README explains this well: first observe to find the email input and password input, get the selectors, then use locator to fill in credentials. Throughout this process, the password stays only in your own code and never enters the model's context.

extract: Extract data. Give it a zod-defined schema, say "extract all invoices from the table", and the returned data is directly validated against the schema. You'll know immediately if field names or types are wrong.

Browser sessions can also be persisted. When launching with localBrowser.launch, you can specify userDataDir, and cookies are stored locally so login state can be reused across scripts. After the first login, subsequent scripts start with that login state.

One inference, then deterministic replay

Anyone who has written UI automation has encountered the problem of brittle selectors. When a page changes, dozens of test cases fail at once, and you stay up late fixing selectors.

Stagehand's solution to this problem has two layers.

The first time act is executed, it uses model inference. After the model finds the target element, it returns an Action object with the parsed selector. Subsequent executions of the same action replay this Action directly. Replay is deterministic and doesn't call the model at all. You only pay for the first inference.

The second layer is self-healing. When a page change causes an action to fail, the framework detects that execution doesn't match, automatically refreshes the way the action is performed, and asks the model to re-find the target. If you run on Browserbase's cloud, there's also server-side caching, so the same instruction hits the cache and returns, costing zero tokens.

These two layers together implement a valuable idea in Agent engineering: compress the model's non-determinism into a single occurrence, and let deterministic code handle the rest.

Why it's fast and why it saves tokens

There's a design choice in the architecture worth highlighting. Stagehand's runtime runs as a browser extension beside the browser, so each Agent action doesn't need to go back and forth to a remote server. That's how they explain its speed advantage.

Token savings come from something else. Before feeding the model, the framework does a hybrid pruning on the accessibility tree. The model only gets the portion of the page context needed for the current action, no extra nodes. The homepage has a comparison image: for the same task, a Stagehand agent spends 7.4k tokens, while a Playwright agent spends 35.7k. This is official demo data, so the magnitude is worth referencing, but don't take the exact numbers seriously.

Quick start

Prerequisite: Chrome installed locally. The framework drives it via Chrome DevTools Protocol. Three languages to choose from.

# TypeScript
pnpm install @browserbasehq/stagehand 'zod@~4.4.3'

# Python
pip install stagehand

# Go
go get github.com/browserbase/stagehand/packages/sdk-go/[email protected]

One detail: the TypeScript side must pin zod to 4.4.x. Newer minor versions cause type errors when passing a schema to extract. The official docs specifically warn about this.

To write the shortest script following the official Quickstart: launch a local browser, create a Stagehand instance passing in the model and API key, then try the three operations.

import { localBrowser, Stagehand } from "@browserbasehq/stagehand";
import { z } from "zod/v4";

const browser = await localBrowser.launch();
const stagehand = await Stagehand.create({
  browser,
  model: { modelName: "openai/gpt-5.6-sol", apiKey: process.env.OPENAI_API_KEY },
});

const [page] = await browser.context.pages();
await page.goto("https://stagehand.dev");

const { data } = await stagehand.extract(
  "Extract the value proposition from the page.",
  z.object({ valueProposition: z.string() }),
);
console.log(data);

await stagehand.act("Click the 'Evals' button.");
await stagehand.close();
await browser.close();

Set OPENAI_API_KEY before running, then pnpm dlx tsx index.ts. A visible Chrome window will open, showing every step, which makes debugging convenient.

One design point to get used to: Stagehand doesn't read environment variables for you. The model key must be explicitly passed to Stagehand.create in your code.

After running locally, if you want to go to the cloud, replace localBrowser.launch with browserbase.launch, add a Browserbase key. Stealth proxies, session recording, and other capabilities are on the cloud side. Model Gateway will also pick the cheapest model for each action, and you don't even need to configure model keys.

For Claude Code users, an even easier way

You can also use it without writing code. The official team has released a browse command-line tool for coding agents like Claude Code, Cursor, and Codex. For local use, no LLM key or Browserbase account is required.

npm install -g browse
npx skills add browserbase/stagehand --skill browse

browse open https://example.com --local
browse snapshot
browse get title

Another path is the officially hosted MCP server, with four tools navigate, act, observe, and extract that plug directly into any MCP client. This requires a Browserbase key.

claude mcp add --transport http browserbase https://mcp.browserbase.com/mcp \
  --header "Authorization: Bearer $BROWSERBASE_API_KEY"

Points to watch out for

Model calls are a cost. act and extract each require inference, so the more you run scripts, the more you spend. Deterministic parts don't cost money: goto, locator, and replaying Actions don't call the model. If you can replay, replay.

Cloud features are tied to an account. Server-side caching, Model Gateway, stealth proxies, and so on only come with Browserbase. Local browsers can't use server-side caching, so to save those tokens you need to go to the cloud.

Action quality depends on the model. On complex pages, act might click the wrong thing. The official best practices cover ways to make actions more reliable. Before putting critical flows into production, use observe to inspect the page first, which is more reliable than directly using act.

Conclusion

Teams that already have Playwright tests running stably and have no Agent needs don't need to switch frameworks just to be trendy. But three things in this design are worth moving over.

Credentials never reach the model. observe returns real selectors, and sensitive data is filled in by the engineering layer using those selectors. The password never appears in the model's context from start to finish. This layering applies to any Agent doing account operations.

One inference, deterministic replay. The model's non-determinism is compressed into the first execution; everything after is local code speed. In LLM projects, the biggest cost and stability issues often come down to minimizing model calls.

Prune context before feeding. The accessibility tree is pruned before giving to the model, so the model only sees the parts it needs. In Agent projects, the big overhead often comes from giving the model too much context.

GitHub project address:

https://github.com/browserbase/stagehand