Observability for Stagehand
Overview
Stagehand is a relatively low-level AI-native framework for building browser agents in JavaScript.
Stagehand is designed to be a seamless extension of Playwright, and in particular, its Page API.
Thanks to Laminar’s native integration with Playwright, you can trace your Stagehand agents with ease.
Usage
Initialize Laminar
First, we need to initialize Laminar and pass all the LLM modules that you use in your Stagehand agents.
Import the LLM provider SDK and pass it to instrumentModules
, depending
on the model that you use with Stagehand.
For example, if you use OpenAI with Stagehand, you can do the following:
Using Stagehand
You can now use the Stagehand API as usual.
Finalizing Stagehand traces
In order to finalize Stagehand traces, make sure to call stagehand.close()
and Laminar.flush()
,
especially if you are using Laminar and Stagehand in standalone scripts or edge runtimes.
Example result
An example trace for page.act('go to Laminar pricing page')
Troubleshooting
I can’t see LLM calls, token counts, and costs in my trace
It is likely that you’ve started off with npx create-browser-app
, which
creates the package as an ESM module.
Solution
Remove the "type": "module"
from your package.json
file.
You can also explicitly set the type to commonjs
in your package.json
file.
For further reading, check the section on this issue in our Troubleshooting guide.