Skip to content

support telemetry for cua agent #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2025

Conversation

seanmcguire12
Copy link
Member

@seanmcguire12 seanmcguire12 commented Apr 22, 2025

why

  • need to track tokens on cua agents

what changed

  • added token usage and inference time to stagehand.metrics for cua agent

test plan

  • tested locally by cross referencing with token metrics on openai dashboard

Copy link

changeset-bot bot commented Apr 22, 2025

🦋 Changeset detected

Latest commit: 822db17

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@browserbasehq/stagehand Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@seanmcguire12 seanmcguire12 changed the title Sean/stg 270 support token tracking for cua agent support token telemetry for cua agent Apr 22, 2025
@seanmcguire12 seanmcguire12 changed the title support token telemetry for cua agent support telemetry for cua agent Apr 22, 2025
@seanmcguire12 seanmcguire12 marked this pull request as ready for review April 24, 2025 00:20
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Added telemetry tracking capabilities for Computer Use Assistant (CUA) agents, implementing token usage and inference time metrics across the Stagehand system.

  • Added usage property in AgentResult interface to track input/output tokens and inference time
  • Extended StagehandMetrics with agentPromptTokens, agentCompletionTokens, and agentInferenceTimeMs
  • Implemented token tracking in AnthropicCUAClient and OpenAICUAClient with proper error handling
  • Added AGENT to StagehandFunctionName enum and updated updateMetrics method in lib/index.ts
  • Modified StagehandAgentHandler to pass Stagehand instance for metrics access

6 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +449 to +453
const startTime = Date.now();
// Create the message using the Anthropic Messages API
// @ts-expect-error - The Anthropic SDK types are stricter than what we need
const response = await this.client.beta.messages.create(requestParams);
const endTime = Date.now();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider wrapping API call in try/catch to ensure inference time is always measured correctly, even if API call fails

Suggested change
const startTime = Date.now();
// Create the message using the Anthropic Messages API
// @ts-expect-error - The Anthropic SDK types are stricter than what we need
const response = await this.client.beta.messages.create(requestParams);
const endTime = Date.now();
const startTime = Date.now();
let response;
try {
// Create the message using the Anthropic Messages API
// @ts-expect-error - The Anthropic SDK types are stricter than what we need
response = await this.client.beta.messages.create(requestParams);
} finally {
const endTime = Date.now();

Copy link
Collaborator

@miguelg719 miguelg719 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally with a single agent and two agent instances

@seanmcguire12 seanmcguire12 merged commit b8cc164 into main Apr 24, 2025
13 checks passed
@github-actions github-actions bot mentioned this pull request Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants