Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Expose usage counts in OpenAI streamed responses (Fixes #2003)#2016

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Nihhaar0002 wants to merge6 commits intoHeyPuter:main
base:main
Choose a base branch
Loading
fromNihhaar0002:feature-stream-usage

Conversation

@Nihhaar0002
Copy link
Contributor

Fixes#2003

This PR adds token usage exposure for streamed OpenAI responses:

  • Enablesstream_options.include_usage whenstream: true
  • Allows downstream handlers to surface live usage counts
  • Matches existing Claude streaming usage behavior

Claude implementation is already complete — this PR finishes OpenAI support.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign ourContributor License Agreement before we can accept your contribution.
1 out of2 committers have signed the CLA.

✅ Nihhaar0002
❌ Nihhaar Saini


Nihhaar Saini seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, pleaseadd the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let usrecheck it.

Copy link
Collaborator

@SalazareoSalazareo left a comment

Choose a reason for hiding this comment

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

Need to run test on this before merge but generally looks good to me

Copy link
Collaborator

@SalazareoSalazareo left a comment

Choose a reason for hiding this comment

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

some changes required there.

I'm also wondering if this should just be implemented as part of the puterai stream instead of here in claude. But might be too specific to the model.

@ProgrammerIn-wonderland since you've been mostly working on the ai stuff, what do you think

constinit_chat_stream=async({ chatStream})=>{
constcompletion=awaitanthropic.messages.stream(sdk_params);
constusageSum={};
construnningUsage={
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
construnningUsage={
construnningUsage=this.usageFormatterUtil({});

should match actual claude usages to make it more visible


// Each emitted content block now carries an incremental usage object
// ({ input_tokens, output_tokens, total_tokens }) for live metering.
constgetUsage=()=>({
Copy link
Collaborator

Choose a reason for hiding this comment

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

can just spread op the data to copy it

{...runningUsage}

constpayload={
type:'text',
text,
usage:getUsage(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
usage:getUsage(),
usage:{ ...runningUsage},

input:JSON.parse(buffer),
...(block.contentBlock?.text ?{} :{text:''}),
type:'tool_use',
usage:getUsage(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
usage:getUsage(),
usage:{ ...runningUsage},

...block.contentBlock,
input:JSON.parse(buffer),
...(block.contentBlock?.text ?{} :{text:''}),
type:'tool_use',
Copy link
Collaborator

Choose a reason for hiding this comment

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

This needs to go at top of the block, as stream block.contentBlock might override it, to match existing method

if(!usageSum[key])usageSum[key]=0;
usageSum[key]+=meteredData[key];
});
runningUsage.input_tokens+=meteredData.input_tokens||0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
runningUsage.input_tokens+=meteredData.input_tokens||0;
for(constusageTypeinrunningUsage){
runningUsage[usageType]+=meteredData[usageType];
}

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@SalazareoSalazareoSalazareo requested changes

Requested changes must be addressed to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Expose usage counts when streaming ai responses

3 participants

@Nihhaar0002@CLAassistant@Salazareo

[8]ページ先頭

©2009-2025 Movatter.jp