Skip to content

Fix token usage tracking for streaming providers #3909

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

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

Conversation

jajimajp
Copy link

@jajimajp jajimajp commented Aug 7, 2025

Problem

When using streaming providers with the goose CLI, the context token count remains at 0 and doesn't update properly:

Context: ○○○○○○○○○○ 0% (0/200000 tokens)
( O)> Hello

(...snip...)

Context: ○○○○○○○○○○ 0% (0/200000 tokens)
( O)>

Root Cause

The current implementation overwrites usage metrics with each streaming event instead of properly accumulating them. When streaming APIs send a final event with None values, it resets the usage count to 0.

Solution

Modified the update_session_metrics method to:

  • Properly track cumulative token usage across streaming events
  • Handle None values gracefully without resetting existing counts

@jajimajp jajimajp force-pushed the fix-usage-for-streaming-providers branch from 29d5be6 to 0ac0658 Compare August 7, 2025 05:46
Signed-off-by: jajimajp <soichi-yajima@c-fo.com>
@jajimajp jajimajp force-pushed the fix-usage-for-streaming-providers branch from 0ac0658 to a38ea0b Compare August 7, 2025 05:55
@michaelneale
Copy link
Collaborator

thanks @jajimajp - what is odd, I use anthropic/databricks and didn't see it show zero, can you confirm what provider you were using and model etc?
(good catch if there are some cases, but I wonder how it worked at all)

@michaelneale michaelneale self-assigned this Aug 7, 2025
@jajimajp
Copy link
Author

jajimajp commented Aug 7, 2025

thanks @jajimajp - what is odd, I use anthropic/databricks and didn't see it show zero, can you confirm what provider you were using and model etc? (good catch if there are some cases, but I wonder how it worked at all)

I am using an OPENAI-compatible proxy with the model sonnet-4. Therefore, the provider is OPENAI. This change worked well in my environment, but let me verify again whether the same issue occurs with the regular OPENAI API.

@michaelneale
Copy link
Collaborator

also if you can run cargo fmt and clippy etc.

@michaelneale
Copy link
Collaborator

I hand tested this with databricks, openai direct, and anthropic, seems ok, but @katzdave sorry to hassle again - but you ok with this? I am just not sure how it works. I am wary that it works without with openai direct, but with a proxy, was different (sounds like 3rd party proxy may be returning incorrect values?)

@katzdave
Copy link
Collaborator

Can we just add a test that verifies it does the the same thing for the non-streaming case. Generally makes sense to me though on the streaming side.

I can't remember my exact setup but I was seeing some kind of 0 issue a couple weeks ago; I think with openai on databricks.

@michaelneale
Copy link
Collaborator

@jajimajp able to add any tests?

@jajimajp
Copy link
Author

@michaelneale @katzdave

Thanks for the feedback. I've added a test case to verify the non-streaming behavior as requested.

Additionally, I manually verified that non-streaming works correctly with my OpenAI provider by temporarily making the following change:

--- a/crates/goose/src/providers/openai.rs
+++ b/crates/goose/src/providers/openai.rs
@@ -206,7 +206,7 @@ impl Provider for OpenAiProvider {
  }
 
  fn supports_streaming(&self) -> bool {
-     true
+     false
  }
 
  async fn stream(

It seems to be working correctly. Let me know if there's anything else.

@jajimajp jajimajp force-pushed the fix-usage-for-streaming-providers branch from 0cb1c06 to c5bb6bb Compare August 13, 2025 03:42
@jajimajp
Copy link
Author

force pushed to sign previous commits

Signed-off-by: jajimajp <soichi-yajima@c-fo.com>
Signed-off-by: jajimajp <soichi-yajima@c-fo.com>
Signed-off-by: jajimajp <soichi-yajima@c-fo.com>
@jajimajp jajimajp force-pushed the fix-usage-for-streaming-providers branch from c5bb6bb to 8c8caac Compare August 13, 2025 06:01
@zanesq
Copy link
Collaborator

zanesq commented Aug 22, 2025

Still in progress?

@jajimajp
Copy link
Author

Still in progress?

No, I am waiting for comments on this: #3909 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants