> ## Documentation Index
> Fetch the complete documentation index at: https://tbd-6fc993ce-mintlify-add-deploy-button-docs-27400.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Stagehand

[Stagehand](https://github.com/browserbase/stagehand) is an open source AI browser automation framework. It lets developers choose what to write in code vs. natural language. By integrating with Kernel, you can run Stagehand automations with cloud-hosted browsers.

<Note>
  This guide is compatible with Stagehand SDK v3. If you're using an earlier version, please refer to the [Stagehand migration guide](https://docs.stagehand.dev/v3/migrations/v2) or upgrade to v3.
</Note>

## Adding Kernel to existing Stagehand implementations

If you already have a Stagehand (v3) implementation, you can easily switch to using Kernel's cloud browsers by updating your browser configuration.

### 1. Install the Kernel SDK

```bash theme={null}
npm install @onkernel/sdk
```

### 2. Initialize Kernel and create a browser

Import the libraries and create a cloud browser session:

```typescript theme={null}
import { Stagehand } from "@browserbasehq/stagehand";
import Kernel from '@onkernel/sdk';
import { z } from "zod";

const kernel = new Kernel();

const kernelBrowser = await kernel.browsers.create({ stealth: true });

console.log("Live view url: ", kernelBrowser.browser_live_view_url);
```

### 3. Update your browser configuration

Replace your existing browser setup to use Kernel's CDP URL:

```typescript theme={null}
const stagehand = new Stagehand({
  env: "LOCAL",
  localBrowserLaunchOptions: {
    cdpUrl: kernelBrowser.cdp_ws_url,
  },
  model: "openai/gpt-4.1",
  apiKey: process.env.OPENAI_API_KEY,
  verbose: 1,
  domSettleTimeout: 30_000
});

await stagehand.init();
```

### 4. Use your Stagehand automation

Use Stagehand's page methods with the Kernel-powered browser:

```typescript theme={null}
const page = stagehand.context.pages()[0];
await page.goto("https://onkernel.com");
await stagehand.act("Click on Blog in the navbar");
await stagehand.act("Click on the newest blog post");
const output = await stagehand.extract(
  "Extract a summary of the blog post",
  z.object({ summary: z.string() })
);

console.log("Newest blog post summary: ", output.summary);

// Clean up
await stagehand.close();
await kernel.browsers.deleteByID(kernelBrowser.session_id);
```

## Quick setup with our Stagehand example app

Alternatively, you can use our Kernel app template that includes a pre-configured Stagehand integration:

```bash theme={null}
kernel create --name my-stagehand-app --language typescript --template stagehand
```

Then follow the [Quickstart guide](/quickstart/) to deploy and run your Stagehand automation on Kernel's infrastructure.

## Benefits of using Kernel with Stagehand

* **No local browser management**: Run automations without installing or maintaining browsers locally
* **Scalability**: Launch multiple browser sessions in parallel
* **Stealth mode**: Built-in anti-detection features for web scraping
* **Session state**: Maintain browser state across runs via [Profiles](/browsers/profiles)
* **Live view**: Debug your automations with real-time browser viewing

## Next steps

* Check out [live view](/browsers/live-view) for debugging your automations
* Learn about [stealth mode](/browsers/bot-detection/stealth) for avoiding detection
* Learn how to properly [terminate browser sessions](/browsers/termination)
* Learn how to [deploy](/apps/deploy) your Stagehand app to Kernel
