Request Input/Output Token consumption in API result #830

Closed
opened 2026-02-21 17:28:37 -05:00 by yindo · 2 comments
Owner

Originally created by @shouxinjk on GitHub (Dec 21, 2023).

Originally assigned to: @takatost on GitHub.

Self Checks

Description of the new feature / enhancement

We'd like to calculate token consumptions by USER. It would be helpful to have such data in API result.

Scenario when this would be used?

It is a multi-tenant system. Token consumption must be classified by User and Tenant.

Supporting information

No response

Originally created by @shouxinjk on GitHub (Dec 21, 2023). Originally assigned to: @takatost on GitHub. ### Self Checks - [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 file this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). ### Description of the new feature / enhancement We'd like to calculate token consumptions by USER. It would be helpful to have such data in API result. ### Scenario when this would be used? It is a multi-tenant system. Token consumption must be classified by User and Tenant. ### Supporting information _No response_
yindo added the 💪 enhancement label 2026-02-21 17:28:37 -05:00
yindo closed this issue 2026-02-21 17:28:37 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 20, 2024):

Close due to it's no longer active, if you have any questions, you can reopen it.

@github-actions[bot] commented on GitHub (Jan 20, 2024): Close due to it's no longer active, if you have any questions, you can reopen it.
Author
Owner

@dosubot[bot] commented on GitHub (Jan 20, 2024):

🤖

Hello @shouxinjk! I'm Dosu, an AI helper here to assist while we wait for a real human maintainer. I'm here to help you solve bugs, answer questions, and guide you on how to contribute to the project. Let's make your experience with dify repository as smooth as possible!

Based on the current implementation of dify (v0.4.9), there is indeed a mechanism to track token consumption per user. This is achieved through the MessageAgentThought model, which has fields such as message_token, answer_token, and tokens. These fields likely represent the number of tokens used for a message and an answer respectively, while the tokens field could represent the total number of tokens used.

To track token consumption by user, you would need to link this data with a specific user. This can be done using the created_by field in the MessageAgentThought model, which represents the user who created the message. If the message is from the console, the created_by field is populated with the from_account_id of the message. If the message is not from the console, it is assumed to be from an end user, and the created_by field is populated with the from_end_user_id of the message.

Here is a hypothetical query to get the total token consumption for a specific user:

from sqlalchemy import func

# Assuming 'user_id' is the UUID of the user
token_consumption = db.session.query(
    func.sum(MessageAgentThought.tokens)
).filter(
    MessageAgentThought.created_by == user_id
).scalar()

This query sums up the tokens field of all MessageAgentThought instances created by a specific user, effectively giving the total token consumption for that user. Please note that this is a hypothetical query and might need adjustments based on the actual implementation and database schema.

You can find more details in the following files:

However, it seems like you want this information to be directly available in the API result. While the current implementation does not provide this feature, it is certainly possible to modify the API to include this information in the response. This would likely involve modifying the relevant API endpoint to include the token consumption data in the response.

I hope this information is helpful. If you have any further questions or need more clarification, please let me know.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@dosubot[bot] commented on GitHub (Jan 20, 2024): _🤖_ <!-- Greeting --> Hello @shouxinjk! I'm [Dosu](https://dosu.dev), an AI helper here to assist while we wait for a real human maintainer. I'm here to help you solve bugs, answer questions, and guide you on how to contribute to the project. Let's make your experience with dify repository as smooth as possible! <!-- Answer --> Based on the current implementation of dify (v0.4.9), there is indeed a mechanism to track token consumption per user. This is achieved through the `MessageAgentThought` model, which has fields such as `message_token`, `answer_token`, and `tokens`. These fields likely represent the number of tokens used for a message and an answer respectively, while the `tokens` field could represent the total number of tokens used. To track token consumption by user, you would need to link this data with a specific user. This can be done using the `created_by` field in the `MessageAgentThought` model, which represents the user who created the message. If the message is from the console, the `created_by` field is populated with the `from_account_id` of the message. If the message is not from the console, it is assumed to be from an end user, and the `created_by` field is populated with the `from_end_user_id` of the message. Here is a hypothetical query to get the total token consumption for a specific user: ```py from sqlalchemy import func # Assuming 'user_id' is the UUID of the user token_consumption = db.session.query( func.sum(MessageAgentThought.tokens) ).filter( MessageAgentThought.created_by == user_id ).scalar() ``` This query sums up the `tokens` field of all `MessageAgentThought` instances created by a specific user, effectively giving the total token consumption for that user. Please note that this is a hypothetical query and might need adjustments based on the actual implementation and database schema. You can find more details in the following files: - [api/models/model.py](https://github.com/langgenius/dify/blob/main/api/models/model.py) - [api/core/callback_handler/agent_loop_gather_callback_handler.py](https://github.com/langgenius/dify/blob/main/api/core/callback_handler/agent_loop_gather_callback_handler.py) However, it seems like you want this information to be directly available in the API result. While the current implementation does not provide this feature, it is certainly possible to modify the API to include this information in the response. This would likely involve modifying the relevant API endpoint to include the token consumption data in the response. I hope this information is helpful. If you have any further questions or need more clarification, please let me know. ## Sources - [api/models/model.py](https://github.com/langgenius/dify/blob/main/api/models/model.py) - [api/core/callback_handler/agent_loop_gather_callback_handler.py](https://github.com/langgenius/dify/blob/main/api/core/callback_handler/agent_loop_gather_callback_handler.py) --- This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#830