Token usage is none for ark provider #5590

Closed
opened 2026-02-21 18:11:47 -05:00 by yindo · 1 comment
Owner

Originally created by @sinomoe on GitHub (Sep 13, 2024).

Self Checks

  • 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 (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.8.1

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

use doubao LLM model in workflow

✔️ Expected Behavior

token usage for LLM node should not be empty

Actual Behavior

token usage for LLM node is 0

Originally created by @sinomoe on GitHub (Sep 13, 2024). ### Self Checks - [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 (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.8.1 ### Cloud or Self Hosted Self Hosted (Source) ### Steps to reproduce use doubao LLM model in workflow ### ✔️ Expected Behavior token usage for LLM node should not be empty ### ❌ Actual Behavior token usage for LLM node is 0
yindo added the 🐞 bug label 2026-02-21 18:11:47 -05:00
yindo closed this issue 2026-02-21 18:11:47 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Sep 13, 2024):

The token usage for the LLM node in the ark provider is showing as 0 in Dify version 0.8.1 when using the doubao LLM model in a self-hosted workflow because the LLMUsage object is initialized with empty usage and is not updated correctly during the invocation process.

In the _invoke_llm method, the usage variable is initialized with LLMUsage.empty_usage() and is only updated if an event of type ModelInvokeCompleted is yielded. If such an event is not yielded, the usage remains empty.

Here is the relevant code snippet:

def _invoke_llm(
    self,
    node_data_model: ModelConfig,
    model_instance: ModelInstance,
    prompt_messages: list[PromptMessage],
    stop: Optional[list[str]] = None,
) -> Generator[RunEvent | ModelInvokeCompleted, None, None]:
    """
    Invoke large language model
    :param node_data_model: node data model
    :param model_instance: model instance
    :param prompt_messages: prompt messages
    :param stop: stop
    :return:
    """
    db.session.close()

    invoke_result = model_instance.invoke_llm(
        prompt_messages=prompt_messages,
        model_parameters=node_data_model.completion_params,
        stop=stop,
        stream=True,
        user=self.user_id,
    )

    # handle invoke result
    generator = self._handle_invoke_result(invoke_result=invoke_result)

    usage = LLMUsage.empty_usage()
    for event in generator:
        yield event
        if isinstance(event, ModelInvokeCompleted):
            usage = event.usage

    # deduct quota
    self.deduct_llm_quota(tenant_id=self.tenant_id, model_instance=model_instance, usage=usage)

The usage variable is only updated if an event of type ModelInvokeCompleted is encountered. If this event is not yielded, the usage remains as LLMUsage.empty_usage(), which results in a token usage of 0 [1].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Sep 13, 2024): <!-- Answer --> The token usage for the LLM node in the ark provider is showing as 0 in Dify version 0.8.1 when using the doubao LLM model in a self-hosted workflow because the `LLMUsage` object is initialized with empty usage and is not updated correctly during the invocation process. In the `_invoke_llm` method, the `usage` variable is initialized with `LLMUsage.empty_usage()` and is only updated if an event of type `ModelInvokeCompleted` is yielded. If such an event is not yielded, the `usage` remains empty. Here is the relevant code snippet: ```python def _invoke_llm( self, node_data_model: ModelConfig, model_instance: ModelInstance, prompt_messages: list[PromptMessage], stop: Optional[list[str]] = None, ) -> Generator[RunEvent | ModelInvokeCompleted, None, None]: """ Invoke large language model :param node_data_model: node data model :param model_instance: model instance :param prompt_messages: prompt messages :param stop: stop :return: """ db.session.close() invoke_result = model_instance.invoke_llm( prompt_messages=prompt_messages, model_parameters=node_data_model.completion_params, stop=stop, stream=True, user=self.user_id, ) # handle invoke result generator = self._handle_invoke_result(invoke_result=invoke_result) usage = LLMUsage.empty_usage() for event in generator: yield event if isinstance(event, ModelInvokeCompleted): usage = event.usage # deduct quota self.deduct_llm_quota(tenant_id=self.tenant_id, model_instance=model_instance, usage=usage) ``` The `usage` variable is only updated if an event of type `ModelInvokeCompleted` is encountered. If this event is not yielded, the `usage` remains as `LLMUsage.empty_usage()`, which results in a token usage of 0 <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/workflow/nodes/llm/llm_node.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#5590