message_tokens is 0 #12790

Closed
opened 2026-02-21 19:09:03 -05:00 by yindo · 4 comments
Owner

Originally created by @shaxiaozz on GitHub (Apr 11, 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 search for existing issues, 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.2.0

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

I checked the number of messages in the chat bot log and the token cost for all messages was 0.

Image

✔️ Expected Behavior

No response

Actual Behavior

No response

Originally created by @shaxiaozz on GitHub (Apr 11, 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 [search for existing issues](https://github.com/langgenius/dify/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.2.0 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce I checked the number of messages in the chat bot log and the token cost for all messages was 0. ![Image](https://github.com/user-attachments/assets/93a68247-7ce4-40f8-b78b-3bb2aef73d81) ### ✔️ Expected Behavior _No response_ ### ❌ Actual Behavior _No response_
yindo closed this issue 2026-02-21 19:09:03 -05:00
Author
Owner

@shaxiaozz commented on GitHub (Apr 11, 2025):

Supplement: All chat bots are displayed like this. At the same time, I checked the network request in F12 and found that the message_tokens in the data returned by the backend was 0. This problem should not be a problem with the front-end display.

Note: Even if message_tokens is 0, it will not affect the reply of llm. The llm I am currently using is "grok-2-1212"

@shaxiaozz commented on GitHub (Apr 11, 2025): Supplement: All chat bots are displayed like this. At the same time, I checked the network request in F12 and found that the message_tokens in the data returned by the backend was 0. This problem should not be a problem with the front-end display. Note: Even if message_tokens is 0, it will not affect the reply of llm. The llm I am currently using is "grok-2-1212"
Author
Owner

@dosubot[bot] commented on GitHub (Apr 11, 2025):

I found a similar issue that was previously reported: Token usage is none for ark provider. This issue was closed with a pull request that fixed the problem: fix: ark token usage is none [1].

For your current issue, it seems that the message_tokens being 0 might be related to the token calculation logic. In Dify, the token calculation logic for chat bots is implemented in the get_num_tokens method within the file api/core/model_runtime/model_providers/__base/large_language_model.py. This method calculates the number of tokens for given prompt messages. If the PLUGIN_BASED_TOKEN_COUNTING_ENABLED configuration is enabled, it uses the PluginModelManager to get the number of tokens. Otherwise, it returns 0 [2].

You might want to check if the PLUGIN_BASED_TOKEN_COUNTING_ENABLED configuration is set correctly in your setup. Additionally, ensure that the TokenManager class in api/libs/helper.py is functioning as expected, as it handles token usage and calculation [3].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

@dosubot[bot] commented on GitHub (Apr 11, 2025): <!-- Answer --> I found a similar issue that was previously reported: [Token usage is none for ark provider](https://github.com/langgenius/dify/issues/8349). This issue was closed with a pull request that fixed the problem: [fix: ark token usage is none](https://github.com/langgenius/dify/pull/8351) <sup>[[1]](https://github.com/langgenius/dify/issues/8349)</sup>. For your current issue, it seems that the `message_tokens` being 0 might be related to the token calculation logic. In Dify, the token calculation logic for chat bots is implemented in the `get_num_tokens` method within the file `api/core/model_runtime/model_providers/__base/large_language_model.py`. This method calculates the number of tokens for given prompt messages. If the `PLUGIN_BASED_TOKEN_COUNTING_ENABLED` configuration is enabled, it uses the `PluginModelManager` to get the number of tokens. Otherwise, it returns 0 <sup>[[2]](https://github.com/langgenius/dify/blob/main/api/core/model_runtime/model_providers/__base/large_language_model.py)</sup>. You might want to check if the `PLUGIN_BASED_TOKEN_COUNTING_ENABLED` configuration is set correctly in your setup. Additionally, ensure that the `TokenManager` class in `api/libs/helper.py` is functioning as expected, as it handles token usage and calculation <sup>[[3]](https://github.com/langgenius/dify/blob/main/api/libs/helper.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.* --- <sup>Help Dosu learn! Give it feedback: [Great Response](https://app.dosu.dev/response-feedback/524a1e18-c5c5-4333-b91d-6c329393ee8b?feedback_type=great_response) | [Irrelevant Answer](https://app.dosu.dev/response-feedback/524a1e18-c5c5-4333-b91d-6c329393ee8b?feedback_type=irrelevant_answer) | [Incorrect Sources](https://app.dosu.dev/response-feedback/524a1e18-c5c5-4333-b91d-6c329393ee8b?feedback_type=incorrect_sources) | [Too Verbose](https://app.dosu.dev/response-feedback/524a1e18-c5c5-4333-b91d-6c329393ee8b?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/524a1e18-c5c5-4333-b91d-6c329393ee8b?feedback_type=hallucination) | [Bug Report](https://app.dosu.dev/response-feedback/524a1e18-c5c5-4333-b91d-6c329393ee8b?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/524a1e18-c5c5-4333-b91d-6c329393ee8b?feedback_type=other)</sup>
Author
Owner

@shaxiaozz commented on GitHub (Apr 11, 2025):

PLUGIN_BASED_TOKEN_COUNTING_ENABLED is already set to true, but it still doesn't work

@shaxiaozz commented on GitHub (Apr 11, 2025): PLUGIN_BASED_TOKEN_COUNTING_ENABLED is already set to true, but it still doesn't work
Author
Owner

@dosubot[bot] commented on GitHub (May 12, 2025):

Hi, @shaxiaozz. I'm Dosu, and I'm helping the Dify team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • Reported bug in Dify version 1.2.0: token cost for chatbot messages displayed as 0.
  • Issue not related to front-end display.
  • I suggested checking PLUGIN_BASED_TOKEN_COUNTING_ENABLED and TokenManager class.
  • You confirmed the configuration is correct, but the issue persists.

Next Steps:

  • Please confirm if this issue is still relevant to the latest version of Dify. If so, you can keep the discussion open by commenting here.
  • If there is no further activity, this issue will be automatically closed in 15 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (May 12, 2025): Hi, @shaxiaozz. I'm [Dosu](https://dosu.dev), and I'm helping the Dify team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - Reported bug in Dify version 1.2.0: token cost for chatbot messages displayed as 0. - Issue not related to front-end display. - I suggested checking `PLUGIN_BASED_TOKEN_COUNTING_ENABLED` and `TokenManager` class. - You confirmed the configuration is correct, but the issue persists. **Next Steps:** - Please confirm if this issue is still relevant to the latest version of Dify. If so, you can keep the discussion open by commenting here. - If there is no further activity, this issue will be automatically closed in 15 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#12790