Skip to content

Commit fd909e0

Browse files
committed
Enhance Stagehand error handling and token tracking
- Improved error handling in StagehandAdapter to parse and surface structured error messages from stdout before falling back to stderr. - Added hints for common errors, specifically when Stagehand is not installed. - Updated bridge.mjs to accumulate token usage during chat completion calls, providing better insights into token consumption across the run.
1 parent c14583c commit fd909e0

3 files changed

Lines changed: 716 additions & 15 deletions

File tree

‎adapters/stagehand/bridge.mjs‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ async function main() {
5959
await stagehand.init();
6060
const page = stagehand.page;
6161

62+
// Stagehand 1.x doesn't expose aggregated token usage publicly, but every
63+
// LLMClient subclass populates response.usage on each createChatCompletion
64+
// call. Wrap the method to accumulate tokens across the run.
65+
let tokens_in = 0;
66+
let tokens_out = 0;
67+
if (stagehand.llmClient && typeof stagehand.llmClient.createChatCompletion === "function") {
68+
const originalCreate = stagehand.llmClient.createChatCompletion.bind(stagehand.llmClient);
69+
stagehand.llmClient.createChatCompletion = async (opts) => {
70+
const result = await originalCreate(opts);
71+
const usage = result && (result.usage || (result.data && result.data.usage));
72+
if (usage) {
73+
tokens_in += Number(usage.prompt_tokens || usage.input_tokens || 0) || 0;
74+
tokens_out += Number(usage.completion_tokens || usage.output_tokens || 0) || 0;
75+
}
76+
return result;
77+
};
78+
}
79+
6280
// Pre-auth via API (so the agent doesn't have to sign in)
6381
if (req.credentials) {
6482
try {
@@ -78,17 +96,13 @@ async function main() {
7896
await page.goto(req.base_url + "/");
7997

8098
const steps = [];
81-
let tokens_in = 0;
82-
let tokens_out = 0;
8399

84100
try {
85101
const result = await stagehand.act({ action: req.goal });
86102
steps.push({
87103
type: "stagehand_act",
88104
action: { goal: req.goal, result: String(result).slice(0, 500) },
89105
url: page.url(),
90-
tokens_in: 0,
91-
tokens_out: 0,
92106
});
93107
} catch (err) {
94108
steps.push({
@@ -102,9 +116,7 @@ async function main() {
102116
await stagehand.close();
103117
} catch {}
104118

105-
process.stdout.write(
106-
JSON.stringify({ steps, tokens_in, tokens_out }) + "\n",
107-
);
119+
process.stdout.write(JSON.stringify({ steps, tokens_in, tokens_out }) + "\n");
108120
}
109121

110122
main().catch((err) => {

0 commit comments

Comments
 (0)