i am using official discourse ai plugin with openai api key
Is there any way to calculate the token usage of every user?
whitewaterdeu is using the official Discourse AI plugin and wants to calculate the token usage of every user. sam provided an SQL query that calculates the total request and response tokens for each user within a specified date range. The query can be used to show top usage per user.
sam notes that the query will include all users, even those who aren’t using AI. Jagster questioned this, and sam suggested checking the logs to determine which feature is causing the issue.
whitewaterdeu asked for clarification on how to run the SQL query, and Lilly replied that it can be run from the admin UI using the Data Explorer plugin.
i am using official discourse ai plugin with openai api key
Is there any way to calculate the token usage of every user?
-- [params]
-- date :start_date
-- date :end_date
WITH token_usage AS (
SELECT
user_id,
SUM(request_tokens) AS total_request_tokens,
SUM(response_tokens) AS total_response_tokens,
SUM(request_tokens + response_tokens) AS total_tokens
FROM
ai_api_audit_logs
WHERE
created_at BETWEEN :start_date AND :end_date
GROUP BY
user_id
)
SELECT
u.id user_id,
tu.total_request_tokens,
tu.total_response_tokens,
tu.total_tokens
FROM
token_usage tu
JOIN
users u ON u.id = tu.user_id
ORDER BY
tu.total_tokens DESC
Will show top usage per user for date range.
It shows users who aren’t using anykind AI at all. Any ideas why, included in summaries or something?
Maybe, look at the logs, filter on user it will tell you which feature it is
SqlHelper persona can help you with the query
thanks for your reply, could you please explain more about how to run these code?
should i execute them after ./launcher enter app?
how to change start_date and end_date?
what’s the type of these code? i just know some python
Sorry for my ignorance ![]()
Hi @whitewaterdeu, that SQL query Sam posted can be run from the admin UI using the data explorer plugin.