Total tokens overcount in workflow caused by loop/iteration nodes #20415

Closed
opened 2026-02-21 20:07:22 -05:00 by yindo · 1 comment
Owner

Originally created by @anobaka on GitHub (Nov 19, 2025).

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.9.2

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Create a workflow like this and run it.

Image

✔️ Expected Behavior

The total tokens of loop or iteration nodes themselves should not be counted.

Image

Actual Behavior

The total tokens inside loop or iteration nodes were counted twice.

Image

I would be happy to submit a PR to address this issue.

Originally created by @anobaka on GitHub (Nov 19, 2025). ### 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.9.2 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce Create a workflow like this and run it. <img width="1227" height="573" alt="Image" src="https://github.com/user-attachments/assets/3d464f3b-599c-4985-965d-fbfc0ebf1bf8" /> ### ✔️ Expected Behavior The total tokens of loop or iteration nodes themselves should not be counted. <img width="1149" height="377" alt="Image" src="https://github.com/user-attachments/assets/3fad8c01-b46d-4357-9501-002648a45e9c" /> ### ❌ Actual Behavior The total tokens inside loop or iteration nodes were counted twice. <img width="938" height="345" alt="Image" src="https://github.com/user-attachments/assets/0feb493d-abfb-4784-9cef-93da1b91b2f4" /> I would be happy to submit a PR to address this issue.
yindo added the 🐞 bug label 2026-02-21 20:07:22 -05:00
yindo closed this issue 2026-02-21 20:07:22 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Nov 19, 2025):

Hi @anobaka! I'm Dosu and I’m helping the dify team.

This is a confirmed bug in Dify v1.9.2: tokens inside loop and iteration nodes are being double-counted, which inflates the total token usage for workflows. The root cause is that both the loop/iteration node and the workflow engine's event handler each add the tokens from the inner graph executions to the total, so tokens from inside the loop get summed twice. This is visible in the code for both LoopNode and IterationNode, where they manually accumulate tokens from each iteration and then the event handler also adds usage again on node completion, leading to double counting (LoopNode code, IterationNode code, event handler).

A workaround, as suggested in earlier issues, is to reset the token counter before each iteration or adjust the aggregation logic so tokens are only counted once per LLM call, not both inside the loop and again at the node level (reference). Until this is fixed in the codebase, you may need to patch your local deployment to avoid double-counting if accurate token accounting is critical for your use case.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Nov 19, 2025): <!-- Greeting --> Hi @anobaka! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the dify team. <!-- Answer --> This is a confirmed bug in Dify v1.9.2: tokens inside loop and iteration nodes are being double-counted, which inflates the total token usage for workflows. The root cause is that both the loop/iteration node and the workflow engine's event handler each add the tokens from the inner graph executions to the total, so tokens from inside the loop get summed twice. This is visible in the code for both LoopNode and IterationNode, where they manually accumulate tokens from each iteration and then the event handler also adds usage again on node completion, leading to double counting ([LoopNode code](https://github.com/langgenius/dify/blob/88c9b18cb679cc037be68ac5f168c6bc28809889/api/core/workflow/nodes/loop/loop_node.py#L45-L230), [IterationNode code](https://github.com/langgenius/dify/blob/88c9b18cb679cc037be68ac5f168c6bc28809889/api/core/workflow/nodes/iteration/iteration_node.py#L63-L673), [event handler](https://github.com/langgenius/dify/blob/88c9b18cb679cc037be68ac5f168c6bc28809889/api/core/workflow/graph_engine/event_management/event_handlers.py#L47-L348)). A workaround, as suggested in earlier issues, is to reset the token counter before each iteration or adjust the aggregation logic so tokens are only counted once per LLM call, not both inside the loop and again at the node level ([reference](https://github.com/langgenius/dify/issues/12801)). Until this is fixed in the codebase, you may need to patch your local deployment to avoid double-counting if accurate token accounting is critical for your use case. <!-- 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/bb5919fe-d599-4192-be76-efca1784d2d1?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/bb5919fe-d599-4192-be76-efca1784d2d1?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/bb5919fe-d599-4192-be76-efca1784d2d1?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/bb5919fe-d599-4192-be76-efca1784d2d1?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/bb5919fe-d599-4192-be76-efca1784d2d1?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/bb5919fe-d599-4192-be76-efca1784d2d1?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/bb5919fe-d599-4192-be76-efca1784d2d1?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&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/28405)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#20415