Skip to content
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

token usage tracking (Issue #268) #339

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

andrewgcodes
Copy link

why

being able to track input/output/total tokens when using stagehand can be helpful for controlling costs, picking which model to use, etc.

See Issue #268 (#268)

what changed

  • Added LLMUsageEntry interface to track tokens per function call
  • Implemented usage recording in OpenAI and Anthropic clients
  • Added recordUsage() and getUsage() methods to Stagehand class
  • Added getTotalTokensUsed() for aggregating total token usage

Works for act(), observe(), and extract()
12 files edited, added tests and and example (hackernews.ts) demonstrating how to log tokens used. Please see diff

test plan

i wrote a few test cases and it seems to work but someone on the browserbase team should definitely test as well before merging!

Copy link

changeset-bot bot commented Dec 27, 2024

⚠️ No Changeset found

Latest commit: 931020d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@kamath
Copy link
Contributor

kamath commented Dec 27, 2024

whoa!! thanks @andrewgcodes! i'll take a more detailed look at this in a bit, but thank you so much for taking the time to do this!

Copy link
Contributor

@kamath kamath left a comment

Choose a reason for hiding this comment

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

Looking more, this is a phenomenal PR, and I sincerely appreciate you adding test cases.

However, we're trying super hard to keep the Stagehand interface as minimal as possible to avoid adding unnecessary complexity to the end-user. Having a user write out the code I highlighted feels unnecessarily cumbersome.

I acknowledge the frustration around token usage being a black box, and it's something we're actively fixing before the holiday break ends. We as the Browserbase team need to do better about this.

I'm imagining an interface to act/extract/observe with something like the following:

const result = page.extract(extractOptions) # returns { ...extractResult, _stagehandTokenUsage: TokenUsageObject }

Let's talk more in #stagehand-discussion in the Stagehand public Slack (I see you're part of this already but linking here for posterity)

Comment on lines 58 to 64
const extractUsage = stagehand.getUsage();
const extractEntry = extractUsage.find(
(entry) => entry.functionName === "extract",
);
if (extractEntry) {
logTokenUsage("HN-ARTICLE-EXTRACT", extractEntry);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

this isn't really elegant; ideally the user should be able to have all of this in one line

devin-ai-integration bot and others added 3 commits December 27, 2024 02:43
…se _stagehandTokenUsage property"

This reverts commit a79b420.
…esponse objects via _stagehandTokenUsage property. new test cases and example
@andrewgcodes
Copy link
Author

andrewgcodes commented Dec 27, 2024 via email

@@ -53,6 +53,7 @@ export class LLMProvider {
getClient(
modelName: AvailableModel,
clientOptions?: ClientOptions,
stagehand?: any,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would definitely try to avoid using any here

@filip-michalsky
Copy link
Collaborator

this is amazing! looking for the discussion in Slack. Also I see some Devin in here @andrewgcodes :D

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.

4 participants