Skip to content

fix: treat usage_metadata.total_token_count of vertex ai as total #1208

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

Conversation

gion-pop
Copy link
Contributor

@gion-pop gion-pop commented Jun 6, 2025

Currently, langfuse.langchain.CallbackHandler counts total token count of Vertex AI response as "Other usage".
This PR fixes it.

ref. https://ai.google.dev/gemini-api/docs/tokens?lang=python#count-tokens


Important

Fixes langfuse.langchain.CallbackHandler to correctly map usage_metadata.total_token_count to total in _parse_usage_model().

  • Behavior:
    • Fixes langfuse.langchain.CallbackHandler to correctly treat usage_metadata.total_token_count as total in _parse_usage_model().
  • Files:
    • Updates CallbackHandler.py to include the mapping for total_token_count to total.

This description was created by Ellipsis for db9ee41. You can customize this summary. It will automatically update as commits are pushed.

Greptile Summary

Disclaimer: Experimental PR review

Fixes token usage tracking for Google Vertex AI by correctly mapping total_token_count in the Langfuse CallbackHandler, ensuring accurate usage metrics instead of incorrectly categorizing them as 'Other usage'.

  • Maps Vertex AI's usage_metadata.total_token_count to the standardized total field in langfuse/langchain/CallbackHandler.py
  • Improves accuracy of token usage reporting for Vertex AI LLM interactions
  • Aligns implementation with Google's official token counting documentation

💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!

@CLAassistant
Copy link

CLAassistant commented Jun 6, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@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.

LGTM

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

Copy link
Contributor

@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.

LGTM

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

@gion-pop gion-pop changed the title fix(langchain): treat usage_metadata.total_token_count of vertex ai as total fix: treat usage_metadata.total_token_count of vertex ai as total Jun 6, 2025
Copy link
Contributor

@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.

LGTM

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

@hassiebp hassiebp merged commit 864588e into langfuse:main Jun 6, 2025
1 check passed
@hassiebp
Copy link
Contributor

hassiebp commented Jun 6, 2025

Thanks for your contribution @gion-pop !

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.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy