Does Dify's token consumption statistics not match the tokens returned by the model #22089

Closed
opened 2026-02-21 20:15:43 -05:00 by yindo · 4 comments
Owner

Originally created by @lanqy123 on GitHub (Feb 5, 2026).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • 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, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.1.3 1.8.1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Does Dify's token consumption statistics not match the tokens returned by the model? Does Dify have its own token calculation rules?
dify:
Image
模型日志:
Image

✔️ Expected Behavior

Obtain token usage from the model's response.

Actual Behavior

No response

Originally created by @lanqy123 on GitHub (Feb 5, 2026). ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [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, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.1.3 1.8.1 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce Does Dify's token consumption statistics not match the tokens returned by the model? Does Dify have its own token calculation rules? dify: <img width="625" height="289" alt="Image" src="https://github.com/user-attachments/assets/ac03eeea-69b4-4310-b22e-4577dc93aa15" /> 模型日志: <img width="730" height="913" alt="Image" src="https://github.com/user-attachments/assets/2096852a-14b8-43f1-b49b-aa1887873271" /> ### ✔️ Expected Behavior Obtain token usage from the model's response. ### ❌ Actual Behavior _No response_
yindo closed this issue 2026-02-21 20:15:43 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Feb 5, 2026):

Dify is designed to display the token usage reported by the model itself, not to recalculate tokens independently. The core logic takes the token usage (prompt, completion, and total tokens) from the model's response and surfaces it in the UI and logs. There is a fallback mechanism for token counting, but it's only used if the model/plugin doesn't provide usage data, and it's controlled by a configuration flag. In normal operation, Dify should match the model's reported token usage, as seen in the code for the LargeLanguageModel and LLMUsage classes (source, source).

If you see a large discrepancy (like 222 vs. 69 tokens), it could be due to a plugin bug, a misconfiguration, or the plugin not passing usage data correctly. Double-check your plugin logs and configuration, and make sure your model integration is up to date. This is a known issue discussed by other users as well. If the problem persists, capturing detailed logs from both Dify and your model/plugin will help pinpoint where the mismatch occurs.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Feb 5, 2026): <!-- Answer --> Dify is designed to display the token usage reported by the model itself, not to recalculate tokens independently. The core logic takes the token usage (prompt, completion, and total tokens) from the model's response and surfaces it in the UI and logs. There is a fallback mechanism for token counting, but it's only used if the model/plugin doesn't provide usage data, and it's controlled by a configuration flag. In normal operation, Dify should match the model's reported token usage, as seen in the code for the LargeLanguageModel and LLMUsage classes ([source](https://github.com/langgenius/dify/blob/8c31b69c8e03dcfa72696ff650bacc9c90c4db61/api/core/model_runtime/model_providers/__base/large_language_model.py#L86-L491), [source](https://github.com/langgenius/dify/blob/8c31b69c8e03dcfa72696ff650bacc9c90c4db61/api/core/model_runtime/entities/llm_entities.py#L45-L170)). If you see a large discrepancy (like 222 vs. 69 tokens), it could be due to a plugin bug, a misconfiguration, or the plugin not passing usage data correctly. Double-check your plugin logs and configuration, and make sure your model integration is up to date. This is a known issue discussed by other users as well. If the problem persists, capturing detailed logs from both Dify and your model/plugin will help pinpoint where the mismatch occurs. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/5eeb375a-2e17-46ee-9004-4d2a0bdd5d2b?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/5eeb375a-2e17-46ee-9004-4d2a0bdd5d2b?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/5eeb375a-2e17-46ee-9004-4d2a0bdd5d2b?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/5eeb375a-2e17-46ee-9004-4d2a0bdd5d2b?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/5eeb375a-2e17-46ee-9004-4d2a0bdd5d2b?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/5eeb375a-2e17-46ee-9004-4d2a0bdd5d2b?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/5eeb375a-2e17-46ee-9004-4d2a0bdd5d2b?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=Dify%20is%20designed%20to%20display%20the%20token%20usage%20reported%20by%20the%20model%20itself%2C%20not%20to%20recalculate%20tokens%20independently.%20The%20core%20logic%20takes%20the%20token%20usage%20%28prompt%2C%20completion%2C%20and%20total%20tokens%29%20from%20the%20model%27s%20response%20and%20surfaces%20it%20in%20the%20UI%20and%20logs.%20There%20is%20a%20fallback%20mechanism%20for%20token%20counting%2C%20but%20it%27s%20only%20used%20if%20the%20model/plugin%20doesn%27t%20provide%20usage%20data%2C%20and%20it%27s%20controlled%20by%20a%20configuration%20flag.%20In%20normal%20operation%2C%20Dify%20should%20match%20the%20model%27s%20reported%20token%20usage%2C%20as%20seen%20in%20the%20code%20for%20the%20LargeLanguageModel%20and%20LLMUsage%20classes%20%28%5Bsource%5D%28https%3A//github.com/langgenius/dify/blob/8c31b69c8e03dcfa72696ff650bacc9c90c4db61/api/core/model_runtime/model_providers/__base/large_language_model.py%23L86-L491%29%2C%20%5Bsource%5D%28https%3A//github.com/langgenius/dify/blob/8c31b69c8e03dcfa72696ff650bacc9c90c4db61/api/core/model_runtime/entities/llm_entities.py%23L45-L170%29%29.%0A%0AIf%20you%20see%20a%20large%20discrepancy%20%28like%20222%20vs.%2069%20tokens%29%2C%20it%20could%20be%20due%20to%20a%20plugin%20bug%2C%20a%20misconfiguration%2C%20or%20the%20plugin%20not%20passing%20usage%20data%20correctly.%20Double-check%20your%20plugin%20logs%20and%20configuration%2C%20and%20make%20sure%20your%20model%20integration%20is%20up%20to%20date.%20This%20is%20a%20known%20issue%20discussed%20by%20other%20users%20as%20well.%20If%20the%20problem%20persists%2C%20capturing%20detailed%20logs%20from%20both%20Dify%20and%20your%20model/plugin%20will%20help%20pinpoint%20where%20the%20mismatch%20occurs.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/31985)
Author
Owner

@lanqy123 commented on GitHub (Feb 5, 2026):

plugin version:

Image

dify:token:137

Image

model: token:45

Image
@lanqy123 commented on GitHub (Feb 5, 2026): plugin version: <img width="905" height="90" alt="Image" src="https://github.com/user-attachments/assets/0f130d14-10c0-416c-b1d0-80fc3fd7e478" /> dify:token:137 <img width="1035" height="449" alt="Image" src="https://github.com/user-attachments/assets/637bbf50-a795-410f-a87c-dc1a5b88efce" /> model: token:45 <img width="715" height="853" alt="Image" src="https://github.com/user-attachments/assets/ccdbcaa3-c39f-4aad-a51f-672c19383f55" />
Author
Owner

@dosubot[bot] commented on GitHub (Feb 5, 2026):

The token usage mismatch happens because the OpenAI-API-compatible plugin (v0.0.34) relies on the model's metadata for token counts. If the model's response is missing or incomplete in the usage field—especially total_tokens—the plugin defaults to zero, and Dify falls back to its own token counting logic, which often doesn't match the model's actual count. This is a known issue, especially since the plugin uses GPT-2 token calculation by default, which may not align with qwen3-max's tokenizer. There are suggestions to switch to GPT-4o token calculation for better accuracy with Qwen models (discussion).

To improve accuracy: check that your model/plugin response includes all usage fields (prompt_tokens, completion_tokens, total_tokens), try enabling the PLUGIN_BASED_TOKEN_COUNTING_ENABLED flag in Dify, and consider updating both Dify and the plugin to the latest versions. If the issue persists, capturing and comparing the raw plugin response, model logs, and Dify's displayed token usage for the same interaction will help pinpoint the cause (plugin code reference).

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Feb 5, 2026): <!-- Answer --> The token usage mismatch happens because the OpenAI-API-compatible plugin (v0.0.34) relies on the model's metadata for token counts. If the model's response is missing or incomplete in the `usage` field—especially `total_tokens`—the plugin defaults to zero, and Dify falls back to its own token counting logic, which often doesn't match the model's actual count. This is a known issue, especially since the plugin uses GPT-2 token calculation by default, which may not align with qwen3-max's tokenizer. There are suggestions to switch to GPT-4o token calculation for better accuracy with Qwen models ([discussion](https://github.com/langgenius/dify-official-plugins/issues/789)). To improve accuracy: check that your model/plugin response includes all usage fields (`prompt_tokens`, `completion_tokens`, `total_tokens`), try enabling the `PLUGIN_BASED_TOKEN_COUNTING_ENABLED` flag in Dify, and consider updating both Dify and the plugin to the latest versions. If the issue persists, capturing and comparing the raw plugin response, model logs, and Dify's displayed token usage for the same interaction will help pinpoint the cause ([plugin code reference](https://github.com/langgenius/dify-official-plugins/blob/540d1772a346d95e140d361343c650f654ffc609/extensions/openai_compatible/endpoints/openai_compatible.py#L8-L242)). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/4f60d602-2bd7-4fb2-b973-fe390a3d56e0?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/4f60d602-2bd7-4fb2-b973-fe390a3d56e0?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/4f60d602-2bd7-4fb2-b973-fe390a3d56e0?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/4f60d602-2bd7-4fb2-b973-fe390a3d56e0?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/4f60d602-2bd7-4fb2-b973-fe390a3d56e0?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/4f60d602-2bd7-4fb2-b973-fe390a3d56e0?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/4f60d602-2bd7-4fb2-b973-fe390a3d56e0?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=The%20token%20usage%20mismatch%20happens%20because%20the%20OpenAI-API-compatible%20plugin%20%28v0.0.34%29%20relies%20on%20the%20model%27s%20metadata%20for%20token%20counts.%20If%20the%20model%27s%20response%20is%20missing%20or%20incomplete%20in%20the%20%60usage%60%20field%E2%80%94especially%20%60total_tokens%60%E2%80%94the%20plugin%20defaults%20to%20zero%2C%20and%20Dify%20falls%20back%20to%20its%20own%20token%20counting%20logic%2C%20which%20often%20doesn%27t%20match%20the%20model%27s%20actual%20count.%20This%20is%20a%20known%20issue%2C%20especially%20since%20the%20plugin%20uses%20GPT-2%20token%20calculation%20by%20default%2C%20which%20may%20not%20align%20with%20qwen3-max%27s%20tokenizer.%20There%20are%20suggestions%20to%20switch%20to%20GPT-4o%20token%20calculation%20for%20better%20accuracy%20with%20Qwen%20models%C2%A0%28%5Bdiscussion%5D%28https%3A//github.com/langgenius/dify-official-plugins/issues/789%29%29.%0A%0ATo%20improve%20accuracy%3A%20check%20that%20your%20model/plugin%20response%20includes%20all%20usage%20fields%20%28%60prompt_tokens%60%2C%20%60completion_tokens%60%2C%20%60total_tokens%60%29%2C%20try%20enabling%20the%20%60PLUGIN_BASED_TOKEN_COUNTING_ENABLED%60%20flag%20in%20Dify%2C%20and%20consider%20updating%20both%20Dify%20and%20the%20plugin%20to%20the%20latest%20versions.%20If%20the%20issue%20persists%2C%20capturing%20and%20comparing%20the%20raw%20plugin%20response%2C%20model%20logs%2C%20and%20Dify%27s%20displayed%20token%20usage%20for%20the%20same%20interaction%20will%20help%20pinpoint%20the%20cause%C2%A0%28%5Bplugin%20code%20reference%5D%28https%3A//github.com/langgenius/dify-official-plugins/blob/540d1772a346d95e140d361343c650f654ffc609/extensions/openai_compatible/endpoints/openai_compatible.py%23L8-L242%29%29.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/31985)
Author
Owner

@crazywoola commented on GitHub (Feb 5, 2026):

Hi @lanqy123, thanks for opening this issue.

Why this is being closed

This issue tracker is reserved for actionable bugs/tasks. This report looks like a usage question.

Next steps

Please use the community channels instead:

If this is actually a bug/task, please open a new issue with clear reproducible details.

Thanks for understanding and for supporting Dify.

@crazywoola commented on GitHub (Feb 5, 2026): Hi @lanqy123, thanks for opening this issue. ### Why this is being closed This issue tracker is reserved for actionable bugs/tasks. This report looks like a usage question. ### Next steps Please use the community channels instead: - https://forum.dify.ai/ - https://discord.com/invite/FngNHpbcY7 If this is actually a bug/task, please open a new issue with clear reproducible details. Thanks for understanding and for supporting Dify.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#22089