Skip to main content
Stagehand is an open-source SDK for browser agents from Browserbase. StagehandMiddleware adds its run, snapshot, and screenshot tools to a LangChain or Deep Agents agent. Pass a configured Stagehand client to the middleware: no MCP server, custom tool definitions, or browser system prompt is needed. For use cases, browser scope, and a sandbox testing setup, see Browser use.
langchain-stagehand requires Python 3.11 or later, LangChain 1.4.7 or later, and Stagehand 4.3.0 or later.

Tools

These are the two input shapes for run:
Use an ID from the latest snapshot response instead of the illustrative 1-42 value. Take a new snapshot after navigation or when IDs become stale.

Setup

Install the middleware, Deep Agents, and a model integration:
Set the model-provider credential and your Browserbase API key:

Use with a deep agent

Create a Browserbase browser, attach a Stagehand client, and pass the middleware to create_deep_agent:
Use ainvoke or astream: the browser tools are asynchronous. The middleware includes browser guidance in tool descriptions and leaves your system prompt unchanged. These tools use Stagehand’s Playwright-shaped facade and do not require a separate Stagehand model.

Use with LangChain

Keep the same browser setup and replace create_deep_agent with create_agent:

Choose the browser

Your application configures the browser and Stagehand client directly, rather than through MCP server environment variables.
  • Browserbase: Use browserbase.launch(api_key=...) as in the example above. The browser runs remotely, so it cannot reach localhost on your machine or in a sandbox.
  • Local Chrome: Use local_browser.launch(headless=True) to run without a visible window. Chrome shares the application’s localhost and requires an installed Google Chrome or Chromium browser.
To use local Chrome, replace the browser launch and client setup with:
Keep the same application-level try/finally cleanup. All Stagehand client settings remain available through Stagehand.create(...). To run Chrome next to your app with a LangSmith sandbox backend, see Test an app with Chrome in a LangSmith sandbox.

Manage browser lifetime

Your application owns the client and browser. The middleware reuses the supplied client across runs and does not close or recreate it. Keep the browser open to retain tabs and logins, then call await browser.close() when finished. If the browser disconnects, supply a new configured client. Choose whether to reuse login state within a thread or across one authenticated user’s conversations. The middleware does not implement scope selection, session storage, or browser checkpointing. Give concurrent agents separate clients and browsers. For the tradeoffs and user-isolation requirements, see Choose a browser scope. The screenshot tool returns images directly to the model. Screenshots saved by run code with a path go to the current working directory, not a Deep Agents backend. Backend artifact saving is not supported yet.

Require approval before actions

snapshot and screenshot only read the page. run navigates, clicks, types, and submits forms. To review each run call before it executes, keep the browser setup above and add interrupt_on and a checkpointer to the agent:
Pass config to ainvoke and keep the browser open while reviewing and resuming interrupts. Each interrupt lists the pending run arguments. For the approval and resume loop, see Human-in-the-loop.

Security

run executes model-authored JavaScript inside the Stagehand browser extension. That code can use any account the session is signed in to and any site the browser can reach. Use Browserbase as the isolation boundary for untrusted tasks. The Playwright-shaped API is not a general-purpose security sandbox. See Stagehand’s security boundary. Treat page content as untrusted input. Keep credentials in your application environment, not in prompts or browser tool inputs. Do not share personal browser sessions across unrelated users.

Browser use guide

Use cases, browser scope, and a sandbox testing setup.

Middleware source and examples

Native LangChain middleware, client lifecycle, and browser tools.

Stagehand Deep Agents docs

Upstream MCP alternative, including use with JavaScript agents.

Browserbase provider page

Browserbase setup, plus Search and Fetch tools.