How do I calculate the token usage of every user in Discourse AI?

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.

7 Likes

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 :pensive:

Hi @whitewaterdeu, that SQL query Sam posted can be run from the admin UI using the data explorer plugin.

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.