Skip to main content
Connect Claude to a browser with StagehandBrowser. The Anthropic SDK runs the model’s tool loop, and Stagehand handles browser interactions such as navigation, screenshots, and form input.

Prerequisites

  • An Anthropic API key and access to a model that supports the browser toolset.
  • Google Chrome for the local example, or a Browserbase API key for a hosted session.
  • Node.js 22.18 or later and pnpm for TypeScript, or Python 3.11 or later and uv for Python.

Install the integration

Clone the Claude Toolset repository:
Install the Anthropic SDK and Stagehand from npm:
Import the driver from the stagehand-claude-sdk npm package.

Run a browser task

Export your Anthropic API key:
Replace the repository’s example.ts or example.py with the matching example below. Both launch headless Chrome, ask Claude to read the heading on example.com, and print the model’s messages.
example.ts
Run from the repository’s typescript directory:
Look for a response identifying the heading as Example Domain. Register the browser instance directly in tools; the Anthropic SDK dispatches its browser tool calls. Keep the same instance throughout the loop so tabs and page state remain available. The tool runner doesn’t close the browser. Use finally in TypeScript or async with browser in Python to close it when the task finishes or fails.

Use Browserbase

Export your Browserbase API key:
Replace the StagehandBrowser.launch call with the matching factory below. Keep the tool loop and cleanup code from the previous example.
This creates a Browserbase session, so you don’t need local Chrome. The toolset doesn’t support downloads in Browserbase sessions.

Domain restrictions

Set allowedDomains and blockedDomains in TypeScript, or allowed_domains and blocked_domains in Python, to restrict HTTP and HTTPS requests. Entries match exact hosts by default; use a leading *. pattern such as *.example.com for subdomains. Blocked entries take precedence.
Domain restrictions don’t cover WebSocket handshakes. A page on an allowed host can open a WebSocket connection to another host. Don’t treat these lists as complete network isolation.

Troubleshooting

For additional configuration and examples, see the Claude Toolset repository.