[PR #5336] fix: wrong token usage in iteration node for streaming result #24883

Closed
opened 2026-02-21 20:23:52 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/5336

State: closed
Merged: Yes


Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes #5323

The token usage of iteration node in run history is correct but not correct when the execution result is displayed via stream response.
The node component shows execution_metadata.total_tokens as the number of tokens and task_pipeline stores the accumulated token usage in the execution_metadata.total_tokens of iteration node. But it sets the token number to the top-level total_tokens field in stream response and lacks metadata_execution.

To fix the issue, this PR lets it respond token usage as execution_metadata in the stream response, as in the LLM node.
I didn't remove the existing total_tokens field as I'm not sure if it is not used.

Type of Change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update, included: Dify Document
  • Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement
  • Dependency upgrade

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • I manually test it on local
    CleanShot 2024-06-18 at 01 37 48

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
  • optional I have made corresponding changes to the documentation
  • optional I have added tests that prove my fix is effective or that my feature works
  • optional New and existing unit tests pass locally with my changes
**Original Pull Request:** https://github.com/langgenius/dify/pull/5336 **State:** closed **Merged:** Yes --- # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. Fixes #5323 The token usage of iteration node in run history is correct but not correct when the execution result is displayed via stream response. The node component[ shows `execution_metadata.total_tokens` as the number of tokens](https://github.com/langgenius/dify/blob/main/web/app/components/workflow/run/node.tsx#L96) and task_pipeline stores [the accumulated token usage in the execution_metadata.total_tokens of iteration node](https://github.com/langgenius/dify/blob/d7213b12cc0998b83bb6b6c901549e57d43fa040/api/core/app/task_pipeline/workflow_iteration_cycle_manage.py#L225). But it sets the token number t[o the top-level total_tokens field in stream response](https://github.com/langgenius/dify/blob/d7213b12cc0998b83bb6b6c901549e57d43fa040/api/core/app/task_pipeline/workflow_iteration_cycle_manage.py#L97) and lacks metadata_execution. To fix the issue, this PR lets it respond token usage as execution_metadata in the stream response, as [in the LLM node](https://github.com/langgenius/dify/blob/d7213b12cc0998b83bb6b6c901549e57d43fa040/api/core/app/task_pipeline/workflow_cycle_manage.py#L387). I didn't remove the existing total_tokens field as I'm not sure if it is not used. ## Type of Change Please delete options that are not relevant. - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [ ] Improvement, including but not limited to code refactoring, performance optimization, and UI/UX improvement - [ ] Dependency upgrade # How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration - [x] I manually test it on local ![CleanShot 2024-06-18 at 01 37 48](https://github.com/langgenius/dify/assets/8503062/059d65f5-e5b0-48b0-87af-8adba11ab171) # Suggested Checklist: - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [x] My changes generate no new warnings - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods - [ ] `optional` I have made corresponding changes to the documentation - [ ] `optional` I have added tests that prove my fix is effective or that my feature works - [ ] `optional` New and existing unit tests pass locally with my changes
yindo added the pull-request label 2026-02-21 20:23:52 -05:00
yindo closed this issue 2026-02-21 20:23:52 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#24883