Skip to content

Enhance Gemini usage tracking to collect comprehensive token data #1752

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 10 commits into
base: main
Choose a base branch
from

Conversation

kiqaps
Copy link
Contributor

@kiqaps kiqaps commented May 18, 2025

When using Gemini, I noticed that some tokens (such as reasoning tokens and usage by modality) were not being collected. I made an adjustment so that all of these are included within the Usage details.

Since details is a dict from str to int, I couldn't simply throw them in there, so I created the dict in what I thought was the most intuitive way, but I'm not sure if it's the best approach.

I also had to add line breaks in this CLI test to make it pass on my PC (not sure why), but after pushing I saw that it broke the tests, so I removed them again :D

Now, every token documented here: https://ai.google.dev/api/generate-content#UsageMetadata is collected.

Also, when streaming, I'm retrieving usage data only from the last chunk and this should fix #1736

@amiyapatanaik
Copy link

Thanks @kiqaps, much appreciated.

Copy link
Contributor

@DouweM DouweM left a comment

Choose a reason for hiding this comment

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

@kiqaps Thanks for picking this up! Just one suggestion.

@kiqaps kiqaps requested a review from DouweM May 20, 2025 10:48
@Kludex
Copy link
Member

Kludex commented May 21, 2025

@kiqaps Would you mind checking the google.py as well? (after rebase)

@kiqaps
Copy link
Contributor Author

kiqaps commented May 21, 2025

@kiqaps Would you mind checking the google.py as well? (after rebase)

yeah, sure, no problem

@kiqaps
Copy link
Contributor Author

kiqaps commented May 21, 2025

ok, its done... but i have done a more intrusive implementation to keep a single "token parser" for both providers (via genai or http), will you guys maintain both or http will be deprecated?

if you prefer, I can keep the parsing in both providers

@DouweM DouweM assigned Kludex and unassigned DouweM May 22, 2025
@DouweM DouweM requested a review from Kludex May 22, 2025 20:01
@Kludex
Copy link
Member

Kludex commented May 23, 2025

The idea is to move forward with the google-genai implementation, and at some point deprecate the httpx one.

So I think it's better to have duplicated logic and not have a common file. Would you mind changing it? Sorry if this was not clear before.

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.

Incorrect usage calculation for gemini models in stream mode
4 participants