Workflow node should return tokens #15532

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

Originally created by @fairyto2 on GitHub (Jul 21, 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.3

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

When I use a workflow-type tool, the token consumption statistics do not include the token consumption within the workflow. In fact, the consumption within the workflow can be tracked.

Image Image

✔️ Expected Behavior

When using a workflow-type tool, the token statistics include the token consumption within the workflow.

The return value of the workflow-type tool node includes the token consumption.

Actual Behavior

No response

Originally created by @fairyto2 on GitHub (Jul 21, 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.3 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce When I use a workflow-type tool, the token consumption statistics do not include the token consumption within the workflow. In fact, the consumption within the workflow can be tracked. <img width="341" height="245" alt="Image" src="https://github.com/user-attachments/assets/fd57d9d2-34f3-41bf-9a46-02a51fb317ce" /> <img width="470" height="273" alt="Image" src="https://github.com/user-attachments/assets/8982550a-c188-4c60-8af0-3d0f0986fe10" /> ### ✔️ Expected Behavior When using a workflow-type tool, the token statistics include the token consumption within the workflow. The return value of the workflow-type tool node includes the token consumption. ### ❌ Actual Behavior _No response_
yindo added the outdated label 2026-02-21 19:22:09 -05:00
yindo closed this issue 2026-02-21 19:22:09 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 21, 2025):

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

Dify tracks token usage at both the workflow and node levels, but when a workflow is used as a tool, the returned output only includes business results—not token consumption. To include token usage (like total_tokens) in the output of a workflow-type tool, you need to modify the WorkflowTool._invoke method. The workflow execution result already contains token stats (e.g., total_tokens), so you can inject this value into the outputs dictionary before returning or yielding it. This change is minimal and aligns with Dify’s design, and does not require changes to the underlying workflow execution logic. For reference, see the implementation and data flow in WorkflowTool and related classes.

Recent PRs like "Expose LLM usage in workflows" and "sys.total_tokens feature" have improved internal token tracking, but exposing this in tool outputs still requires a code change in the tool node logic. There’s no built-in config or extension point for this yet.

To implement: in WorkflowTool._invoke, after getting data = result.get("data", {}), add outputs["total_tokens"] = data.get("total_tokens") before yielding or returning the outputs. This will make token usage visible in the tool’s return value.

If you need more granular stats (per node), you can also extract them from the workflow execution metadata, as these are tracked internally.

Let me know if you want a code snippet or more details on where to make this change.

To reply, just mention @dosu.


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

@dosubot[bot] commented on GitHub (Jul 21, 2025): <!-- Greeting --> Hi @fairyto2! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the dify team. <!-- Answer --> Dify tracks token usage at both the workflow and node levels, but when a workflow is used as a tool, the returned output only includes business results—not token consumption. To include token usage (like total_tokens) in the output of a workflow-type tool, you need to modify the `WorkflowTool._invoke` method. The workflow execution result already contains token stats (e.g., `total_tokens`), so you can inject this value into the outputs dictionary before returning or yielding it. This change is minimal and aligns with Dify’s design, and does not require changes to the underlying workflow execution logic. For reference, see the implementation and data flow in [`WorkflowTool`](https://github.com/langgenius/dify/blob/8fa3b3f9315f8cf87fc0ea5de4dff6c0fde10a96/api/core/tools/workflow_as_tool/tool.py) and related classes. Recent PRs like ["Expose LLM usage in workflows"](https://github.com/langgenius/dify/pull/21766) and ["sys.total_tokens feature"](https://github.com/langgenius/dify/pull/22201) have improved internal token tracking, but exposing this in tool outputs still requires a code change in the tool node logic. There’s no built-in config or extension point for this yet. To implement: in `WorkflowTool._invoke`, after getting `data = result.get("data", {})`, add `outputs["total_tokens"] = data.get("total_tokens")` before yielding or returning the outputs. This will make token usage visible in the tool’s return value. If you need more granular stats (per node), you can also extract them from the workflow execution metadata, as these are tracked internally. Let me know if you want a code snippet or more details on where to make this change. <!-- 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/cce9d628-591b-41fb-99c8-f1e2cde0e137?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/cce9d628-591b-41fb-99c8-f1e2cde0e137?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/cce9d628-591b-41fb-99c8-f1e2cde0e137?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/cce9d628-591b-41fb-99c8-f1e2cde0e137?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/cce9d628-591b-41fb-99c8-f1e2cde0e137?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/cce9d628-591b-41fb-99c8-f1e2cde0e137?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/cce9d628-591b-41fb-99c8-f1e2cde0e137?feedback_type=other)</sup>&nbsp;&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/22717)
Author
Owner

@crazywoola commented on GitHub (Jul 21, 2025):

We removed token calculation after a certain version because we found that it significantly reduced performance.

@crazywoola commented on GitHub (Jul 21, 2025): We removed token calculation after a certain version because we found that it significantly reduced performance.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#15532