mirror of
https://github.com/langgenius/dify-official-plugins.git
synced 2026-07-21 17:45:23 -04:00
Incorrect Token Counting for Gemini #481
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @chaosyaan on GitHub (Jul 25, 2025).
Self Checks
Dify version
1.7.0
Plugin version
0.2.9
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
Problem Description:
Currently, the code accumulates completion_tokens for every chunk when processing Gemini's candidates_token_count:
https://github.com/langgenius/dify-official-plugins/blob/2974592dd89078c264404d93cd190859d5110987/models/gemini/models/llm/llm.py#L492C1-L497C1
However, Gemini's candidates token count is already incrementally increasing with each chunk. The current implementation causes duplicate counting, leading to an explosion of token counts.
Expected Behavior:
The completion_tokens should only be calculated once at the end of the process, using the final value of candidates_token_count instead of accumulating it for every chunk.
✔️ Error log
No response
@chaosyaan commented on GitHub (Jul 25, 2025):
Here's the the PR link: https://github.com/langgenius/dify-official-plugins/pull/1351
@dosubot[bot] commented on GitHub (Aug 19, 2025):
Hi, @chaosyaan. I'm Dosu, and I'm helping the dify-official-plugins team manage their backlog and am marking this issue as stale.
Issue Summary:
Next Steps:
Thank you for your understanding and contribution!