Incorrect Token Counting for Gemini #481

Closed
opened 2026-02-16 10:19:32 -05:00 by yindo · 2 comments
Owner

Originally created by @chaosyaan on GitHub (Jul 25, 2025).

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues Dify issues & Dify Official Plugins, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

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:

if chunk.usage_metadata:
    completion_tokens += (
        chunk.usage_metadata.candidates_token_count or 0
    )
Image

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

Originally created by @chaosyaan on GitHub (Jul 25, 2025). ### Self Checks - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [Dify issues](https://github.com/langgenius/dify/issues) & [Dify Official Plugins](https://github.com/langgenius/dify-official-plugins/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### 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: ```python if chunk.usage_metadata: completion_tokens += ( chunk.usage_metadata.candidates_token_count or 0 ) ``` <img width="1405" height="573" alt="Image" src="https://github.com/user-attachments/assets/c86ca9e6-8f5f-4068-a7a7-3150d69ec2f4" /> 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_
yindo added the bugstale labels 2026-02-16 10:19:32 -05:00
yindo closed this issue 2026-02-16 10:19:32 -05:00
Author
Owner

@chaosyaan commented on GitHub (Jul 25, 2025):

Here's the the PR link: https://github.com/langgenius/dify-official-plugins/pull/1351

@chaosyaan commented on GitHub (Jul 25, 2025): Here's the the PR link: https://github.com/langgenius/dify-official-plugins/pull/1351
Author
Owner

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

  • You reported that in Dify v1.7.0, Gemini's token counting was incorrectly summing candidates_token_count for each chunk, causing inflated totals.
  • You proposed a fix to compute completion_tokens once at the end using the final candidates_token_count.
  • You provided a PR addressing this issue for review.
  • The issue was resolved by implementing your suggested fix in the PR.

Next Steps:

  • Please confirm if this issue is still relevant with the latest version of dify-official-plugins.
  • If it is, you can keep the discussion open by commenting on this issue; otherwise, I will automatically close it in 5 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Aug 19, 2025): Hi, @chaosyaan. I'm [Dosu](https://dosu.dev), and I'm helping the dify-official-plugins team manage their backlog and am marking this issue as stale. **Issue Summary:** - You reported that in Dify v1.7.0, Gemini's token counting was incorrectly summing candidates_token_count for each chunk, causing inflated totals. - You proposed a fix to compute completion_tokens once at the end using the final candidates_token_count. - You provided a PR addressing this issue for review. - The issue was resolved by implementing your suggested fix in the PR. **Next Steps:** - Please confirm if this issue is still relevant with the latest version of dify-official-plugins. - If it is, you can keep the discussion open by commenting on this issue; otherwise, I will automatically close it in 5 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-official-plugins#481