Skip to content

Commit e176d50

Browse files
committed
docs: update Stagehand documentation with AI Gateway and custom model sections
1 parent e1ecee5 commit e176d50

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

src/content/docs/browser-rendering/platform/stagehand.mdx

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,33 @@ import {
1717

1818
[Stagehand](https://www.stagehand.dev/) is an open-source, AI-powered browser automation library. Rather than dictating exact steps or specifying selectors, Stagehand enables developers to build more reliably and flexibly by combining code with natural-language instructions powered by AI. This makes your agents more resilient to website changes and easier to maintain.
1919

20+
This guide shows you how to deploy a Worker that uses Stagehand, Browser Rendering, and [Workers AI](/workers-ai/) to automate a web task.
21+
2022
## Use Stagehand in a Worker
2123

22-
In this example, you will search for a movie on https://www.themoviedb.org/ and return the movie's title, year, and director, as well as a screenshot of the webpage.
24+
In this example, you will use Stagehand to search for a movie on [The Movie Database](https://www.themoviedb.org/), extract its details (title, year, and director), and return the information along with a screenshot of the webpage.
25+
26+
If you want to skip the steps and get started quickly, select **Deploy to Cloudflare** below.
27+
28+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/playwright-mcp/tree/main/cloudflare/example)
2329

2430
After you deploy, you can interact with the Worker using this URL pattern:
2531
```
2632
https://<your-worker>.workers.dev
2733
```
2834

29-
If you want to skip the steps and get started quickly, select **Deploy to Cloudflare** below.
35+
#### 1. Set up your project
3036

31-
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/playwright-mcp/tree/main/cloudflare/example)
37+
???DON'T THEY NEED TO INSRALL BROWSERBASE STAGEHAND TOO?
3238

33-
1. Install dependencies:
39+
Install the necessary dependencies:
3440
```bash
3541
npm ci
3642
```
3743

38-
2. Include the [browser rendering](/browser-rendering/) binding in your wrangler configuration file:
44+
#### 2. Configure your Worker
45+
46+
Update your wrangler configuration file to include the bindings for Browser Rendering and [Workers AI](/workers-ai/):
3947
<WranglerConfig>
4048
```jsonc
4149
{
@@ -62,7 +70,7 @@ If you want to skip the steps and get started quickly, select **Deploy to Cloudf
6270
```
6371
</WranglerConfig>
6472

65-
3. Edit the code:
73+
#### 3. Write the Worker code
6674

6775
```ts title="src/index.ts"
6876

@@ -140,34 +148,56 @@ If you want to skip the steps and get started quickly, select **Deploy to Cloudf
140148
},
141149
};
142150
```
143-
4. Build the project:
151+
#### 4. Build the project
152+
144153
```bash
145154
npm run build
146155
```
147-
5. Deploy to Cloudflare Workers:
156+
#### 5. Deploy to Cloudflare Workers
157+
158+
After you deploy, you can interact with the Worker using this URL pattern:
159+
```
160+
https://<your-worker>.workers.dev
161+
```
162+
148163
```bash
149164
npm run deploy
150165
```
151166

152-
## Use a custom model
167+
## Use Cloudflare AI Gateway
168+
169+
[AI Gateway](/ai-gateway/) is a service that adds observability to your AI applications. By routing your requests through AI Gateway, you can monitor and debug your AI applications.
170+
171+
You can use AI Gateway with both Workers AI and third-party models.
153172

154-
Instead of using a model from [Workers AI](/workers-ai/), you can use a custom model for which you supply your own credentials.
173+
To use AI Gateway, first create a gateway in the [Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/workers/ai-gateway), then add the `baseURL` as shown below.
155174

156-
In this example, you will configure Stagehand to use [OpenAI](https://openai.com/). You will need an OpenAI API key. Cloudflare recommends storing your API key with [secrets](/workers/configuration/secrets/).
175+
???I SWITCHED THE ORDER AROUND. WE SHOULD FIRST SHOW AI GATEWAY WHILE STILL USING WORKERS AI
157176

158177
```typescript
159178
const stagehand = new Stagehand({
160179
env: "LOCAL",
161180
localBrowserLaunchOptions: { cdpUrl: endpointURLString("BROWSER") },
162181
modelName: "openai/gpt-4.1",
163182
modelClientOptions: {
164-
apiKey: env.OPENAI_API_KEY,
183+
baseURL: `https://gateway.ai.cloudflare.com/v1/${env.CLOUDFLARE_ACCOUNT_ID}/${env.AI_GATEWAY_ID}/openai`,
165184
},
166185
});
167186
```
168187

169-
## Use Cloudflare AI Gateway
188+
## Use a custom model
189+
190+
You can configure Stagehand to use models from third-party providers, including OpenAI or Anthropic, by providing your own credentials.
170191

192+
???DOES THIS ONLY WORK WITH CERTAIN PROVIDERS? ONLY MODELS THAT AIGATEWAY SUPPORTS?
193+
194+
In this example, you will configure Stagehand to use [OpenAI](https://openai.com/). You will need an OpenAI API key. Cloudflare recommends storing your API key as a [secret](/workers/configuration/secrets/).
195+
196+
```bash
197+
npx wrangler secret put OPENAI_API_KEY
198+
```
199+
200+
Then, configure Stagehand with your provider, model, and API key.
171201

172202
```typescript
173203
const stagehand = new Stagehand({
@@ -176,7 +206,6 @@ const stagehand = new Stagehand({
176206
modelName: "openai/gpt-4.1",
177207
modelClientOptions: {
178208
apiKey: env.OPENAI_API_KEY,
179-
baseURL: `https://gateway.ai.cloudflare.com/v1/${env.CLOUDFLARE_ACCOUNT_ID}/${env.AI_GATEWAY_ID}/openai`,
180209
},
181210
});
182211
```

0 commit comments

Comments
 (0)