Skip to content

Conversation

@anobaka
Copy link

@anobaka anobaka commented Nov 19, 2025

…odes

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

Total tokens overcount in workflow caused by loop/iteration nodes
fix: #28405

Screenshots

Before After
Image Image

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Copilot AI review requested due to automatic review settings November 19, 2025 09:57
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Nov 19, 2025
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @anobaka, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where token usage was being overcounted in workflow processes involving loop and iteration nodes. By refactoring the token accumulation logic, it ensures that token usage is accurately tracked through the llm_usage object, preventing redundant additions and providing a more precise representation of resource consumption for workflow executions.

Highlights

  • Token Calculation Correction: Removed direct accumulation of total_tokens within iteration_node.py and loop_node.py to prevent overcounting. The llm_usage object is now solely responsible for tracking token usage.
  • Parameter Simplification: Eliminated the tokens_used parameter from _execute_parallel_iterations and _execute_single_iteration_parallel functions in iteration_node.py as it is no longer needed for token tracking.
  • Redundant Variable Removal: Removed the cost_tokens variable from loop_node.py as its purpose was to accumulate tokens, which is now handled by the llm_usage mechanism.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@dosubot dosubot bot added the 🌊 feat:workflow Workflow related stuff. label Nov 19, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively resolves the token overcounting issue within loop and iteration nodes. The changes correctly eliminate the redundant token accumulation logic, ensuring that usage is calculated accurately through the LLMUsage object. The fix is well-targeted, consistent across both node types, and improves the reliability of workflow cost tracking. The changes are sound and I have no further suggestions.

Copilot finished reviewing on behalf of anobaka November 19, 2025 10:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a token overcounting bug in workflow loop and iteration nodes (issue #28405). The fix removes redundant direct manipulation of graph_runtime_state.total_tokens, relying instead on the proper usage tracking mechanism via _accumulate_usage() which updates llm_usage.

Key Changes:

  • Removed cost_tokens variable and its accumulation in loop nodes
  • Removed tokens_used from parallel iteration return values
  • Removed direct total_tokens updates that caused double-counting

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
api/core/workflow/nodes/loop/loop_node.py Removed redundant cost_tokens tracking and direct total_tokens manipulation; relies on _accumulate_usage() to properly track tokens via llm_usage
api/core/workflow/nodes/iteration/iteration_node.py Removed tokens_used from parallel iteration return tuple and direct total_tokens updates; usage is now tracked consistently via _merge_usage() and the llm_usage field

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@crazywoola crazywoola requested a review from laipz8200 November 20, 2025 03:43
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🌊 feat:workflow Workflow related stuff. lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Total tokens overcount in workflow caused by loop/iteration nodes

2 participants