Skip to content

Commit 0853068

Browse files
authored
Update stagehand.mdx
add code for ai gateway with workers ai and third party
1 parent ff3174e commit 0853068

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919

2020
This guide shows you how to deploy a Worker that uses Stagehand, Browser Rendering, and [Workers AI](/workers-ai/) to automate a web task.
2121

22-
## Use Stagehand in a Worker
22+
## Use Stagehand in a Worker with Workers AI
2323

2424
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.
2525

@@ -153,26 +153,25 @@ https://<your-worker>.workers.dev
153153
npm run deploy
154154
```
155155

156-
## Use Cloudflare AI Gateway
156+
## Use Cloudflare AI Gateway with Workers AI
157157

158158
[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.
159159

160-
You can use AI Gateway with both Workers AI and third-party models.
161-
162-
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.
160+
To use AI Gateway, first create a gateway in the [Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/workers/ai-gateway). In this example, we've named the gateway `stagehand-example-gateway`.
163161

164162
```typescript
165163
const stagehand = new Stagehand({
166-
env: "LOCAL",
167-
localBrowserLaunchOptions: { cdpUrl: endpointURLString(env.BROWSER) },
168-
modelName: "openai/gpt-4.1",
169-
modelClientOptions: {
170-
baseURL: `https://gateway.ai.cloudflare.com/v1/${env.CLOUDFLARE_ACCOUNT_ID}/${env.AI_GATEWAY_ID}/openai`,
171-
},
172-
});
164+
env: "LOCAL",
165+
localBrowserLaunchOptions: { cdpUrl },
166+
llmClient: new WorkersAIClient(env.AI, {
167+
gateway: {
168+
id: "stagehand-example-gateway"
169+
}
170+
}),
171+
});
173172
```
174173

175-
## Use third-party model
174+
## Use a third-party model
176175

177176
If you'd like to use a model outside of Workers AI, you can configure Stagehand to use models from supported [third-party providers](https://docs.stagehand.dev/configuration/models#supported-providers), including OpenAI and Anthropic, by providing your own credentials.
178177

@@ -195,5 +194,22 @@ const stagehand = new Stagehand({
195194
});
196195
```
197196

197+
## Use Cloudflare AI Gateway with a third-party model
198+
199+
[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.
200+
201+
To use AI Gateway, first create a gateway in the [Cloudflare dashboard](https://dash.cloudflare.com/?to=/:account/workers/ai-gateway). In this example, we are using [OpenAI with AI Gateway](https://developers.cloudflare.com/ai-gateway/usage/providers/openai/). Make sure to add the `baseURL` as shown below, with your own Account ID and Gateway ID.
202+
203+
```typescript
204+
const stagehand = new Stagehand({
205+
env: "LOCAL",
206+
localBrowserLaunchOptions: { cdpUrl: endpointURLString(env.BROWSER) },
207+
modelName: "openai/gpt-4.1",
208+
modelClientOptions: {
209+
baseURL: `https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/openai`,
210+
},
211+
});
212+
```
213+
198214
## Stagehand API
199215
For the full list of Stagehand methods and capabilities, refer to the official [Stagehand API documentation](https://docs.stagehand.dev/first-steps/introduction).

0 commit comments

Comments
 (0)