get access to llm returned usage data inside a workflow #4497

Closed
opened 2026-02-21 18:06:46 -05:00 by yindo · 2 comments
Owner

Originally created by @OCboy5 on GitHub (Jul 8, 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).
  • 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.6.11

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Inside a workflow, the llm request returns a json object of { "text", "usage"}, but the llm node only output the "text". How can I access the "usage" part outside of the llm node?

✔️ Expected Behavior

I want to know the cost of the llm request from it's returned usage[total_price] field.

Actual Behavior

I don't have a way to access that returned data outside of the llm node.

Originally created by @OCboy5 on GitHub (Jul 8, 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] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.6.11 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce Inside a workflow, the llm request returns a json object of { "text", "usage"}, but the llm node only output the "text". How can I access the "usage" part outside of the llm node? ### ✔️ Expected Behavior I want to know the cost of the llm request from it's returned usage[total_price] field. ### ❌ Actual Behavior I don't have a way to access that returned data outside of the llm node.
yindo closed this issue 2026-02-21 18:06:46 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 8, 2024):

To access the "usage" part, specifically the "total_price" field, from the LLM request's JSON object in Dify version 0.6.11, you need to modify the _run method in the LLMNode class to include the "usage" part in the outputs. The NodeRunResult already includes the "usage" part in the outputs dictionary, so you can access it directly from there.

Here is the relevant part of the code that shows how the "usage" part is included in the outputs:

def _run(self, variable_pool: VariablePool) -> NodeRunResult:
    # ... (other code)

    try:
        # ... (other code)

        # handle invoke result
        result_text, usage = self._invoke_llm(
            node_data_model=node_data.model,
            model_instance=model_instance,
            prompt_messages=prompt_messages,
            stop=stop
        )
    except Exception as e:
        return NodeRunResult(
            status=WorkflowNodeExecutionStatus.FAILED,
            error=str(e),
            inputs=inputs,
            process_data=process_data
        )

    outputs = {
        'text': result_text,
        'usage': jsonable_encoder(usage)
    }

    return NodeRunResult(
        status=WorkflowNodeExecutionStatus.SUCCEEDED,
        inputs=inputs,
        process_data=process_data,
        outputs=outputs,
        metadata={
            NodeRunMetadataKey.TOTAL_TOKENS: usage.total_tokens,
            NodeRunMetadataKey.TOTAL_PRICE: usage.total_price,
            NodeRunMetadataKey.CURRENCY: usage.currency
        }
    )

To access the "total_price" field from the "usage" part, you can retrieve it from the outputs dictionary of the NodeRunResult object. Here is an example of how you can do this:

# Assuming `node_run_result` is an instance of NodeRunResult
usage = node_run_result.outputs.get('usage')
if usage:
    total_price = usage.get('total_price')
    print(f"Total Price: {total_price}")

This code snippet demonstrates how to access the "total_price" field from the "usage" part of the LLM request's JSON object [1].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Jul 8, 2024): <!-- Answer --> To access the "usage" part, specifically the "total_price" field, from the LLM request's JSON object in Dify version 0.6.11, you need to modify the `_run` method in the `LLMNode` class to include the "usage" part in the outputs. The `NodeRunResult` already includes the "usage" part in the `outputs` dictionary, so you can access it directly from there. Here is the relevant part of the code that shows how the "usage" part is included in the outputs: ```python def _run(self, variable_pool: VariablePool) -> NodeRunResult: # ... (other code) try: # ... (other code) # handle invoke result result_text, usage = self._invoke_llm( node_data_model=node_data.model, model_instance=model_instance, prompt_messages=prompt_messages, stop=stop ) except Exception as e: return NodeRunResult( status=WorkflowNodeExecutionStatus.FAILED, error=str(e), inputs=inputs, process_data=process_data ) outputs = { 'text': result_text, 'usage': jsonable_encoder(usage) } return NodeRunResult( status=WorkflowNodeExecutionStatus.SUCCEEDED, inputs=inputs, process_data=process_data, outputs=outputs, metadata={ NodeRunMetadataKey.TOTAL_TOKENS: usage.total_tokens, NodeRunMetadataKey.TOTAL_PRICE: usage.total_price, NodeRunMetadataKey.CURRENCY: usage.currency } ) ``` To access the "total_price" field from the "usage" part, you can retrieve it from the `outputs` dictionary of the `NodeRunResult` object. Here is an example of how you can do this: ```python # Assuming `node_run_result` is an instance of NodeRunResult usage = node_run_result.outputs.get('usage') if usage: total_price = usage.get('total_price') print(f"Total Price: {total_price}") ``` This code snippet demonstrates how to access the "total_price" field from the "usage" part of the LLM request's JSON object <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.*
Author
Owner

@crazywoola commented on GitHub (Jul 8, 2024):

This is a bug report template, if you want to bring up something new, please use the right template to open a feature request.

@crazywoola commented on GitHub (Jul 8, 2024): This is a bug report template, if you want to bring up something new, please use the right template to open a feature request.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#4497