Skip to content

Commit 9037430

Browse files
token usage for act (#636)
* token usage for act * changeset
1 parent 98d63e3 commit 9037430

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

Diff for: .changeset/dirty-garlics-wonder.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
fix token act metrics and inference logging being misplaced as observe metrics and inference logging

Diff for: lib/handlers/actHandler.ts

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export class StagehandActHandler {
229229
onlyVisible: false,
230230
drawOverlay: false,
231231
returnAction: true,
232+
fromAct: true,
232233
});
233234

234235
if (observeResults.length === 0) {

Diff for: lib/handlers/observeHandler.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class StagehandObserveHandler {
5757
returnAction,
5858
onlyVisible,
5959
drawOverlay,
60+
fromAct,
6061
}: {
6162
instruction: string;
6263
llmClient: LLMClient;
@@ -65,6 +66,7 @@ export class StagehandObserveHandler {
6566
returnAction?: boolean;
6667
onlyVisible?: boolean;
6768
drawOverlay?: boolean;
69+
fromAct?: boolean;
6870
}) {
6971
if (!instruction) {
7072
instruction = `Find elements that can be used for any future actions in the page. These may be navigation links, related pages, section/subsection links, buttons, or other interactive elements. Be comprehensive: if there are multiple elements that may be relevant for future actions, return all of them.`;
@@ -114,6 +116,7 @@ export class StagehandObserveHandler {
114116
isUsingAccessibilityTree: useAccessibilityTree,
115117
returnAction,
116118
logInferenceToFile: this.stagehand.logInferenceToFile,
119+
fromAct: fromAct,
117120
});
118121

119122
const {
@@ -123,7 +126,7 @@ export class StagehandObserveHandler {
123126
} = observationResponse;
124127

125128
this.stagehand.updateMetrics(
126-
StagehandFunctionName.OBSERVE,
129+
fromAct ? StagehandFunctionName.ACT : StagehandFunctionName.OBSERVE,
127130
prompt_tokens,
128131
completion_tokens,
129132
inference_time_ms,

Diff for: lib/inference.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ export async function observe({
349349
logger,
350350
returnAction = false,
351351
logInferenceToFile = false,
352+
fromAct,
352353
}: {
353354
instruction: string;
354355
domElements: string;
@@ -359,6 +360,7 @@ export async function observe({
359360
isUsingAccessibilityTree?: boolean;
360361
returnAction?: boolean;
361362
logInferenceToFile?: boolean;
363+
fromAct?: boolean;
362364
}) {
363365
const observeSchema = z.object({
364366
elements: z
@@ -407,15 +409,16 @@ export async function observe({
407409
buildObserveUserMessage(instruction, domElements, isUsingAccessibilityTree),
408410
];
409411

412+
const filePrefix = fromAct ? "act" : "observe";
410413
let callTimestamp = "";
411414
let callFile = "";
412415
if (logInferenceToFile) {
413416
const { fileName, timestamp } = writeTimestampedTxtFile(
414-
"observe_summary",
415-
"observe_call",
417+
`${filePrefix}_summary`,
418+
`${filePrefix}_call`,
416419
{
417420
requestId,
418-
modelCall: "observe",
421+
modelCall: filePrefix,
419422
messages,
420423
},
421424
);
@@ -450,18 +453,18 @@ export async function observe({
450453
let responseFile = "";
451454
if (logInferenceToFile) {
452455
const { fileName: responseFileName } = writeTimestampedTxtFile(
453-
"observe_summary",
454-
"observe_response",
456+
`${filePrefix}_summary`,
457+
`${filePrefix}_response`,
455458
{
456459
requestId,
457-
modelResponse: "observe",
460+
modelResponse: filePrefix,
458461
rawResponse: observeData,
459462
},
460463
);
461464
responseFile = responseFileName;
462465

463-
appendSummary("observe", {
464-
observe_inference_type: "observe",
466+
appendSummary(filePrefix, {
467+
[`${filePrefix}_inference_type`]: filePrefix,
465468
timestamp: callTimestamp,
466469
LLM_input_file: callFile,
467470
LLM_output_file: responseFile,

0 commit comments

Comments
 (0)