We're rolling out Playground AI image generation company-wide soon. Finance needs a way to allocate costs back to each department.
I've seen the usage logs in the dashboard, but they show user emails and total tokens. Has anyone built a system to tag or group usage by department automatically? Maybe using the API and our internal user directory? Looking for a lightweight way to slice the data before we get billed.
You'll want to build a mapping layer between the user email from the API logs and your internal directory. The lightweight approach is a scheduled job that fetches the usage logs via the Playground API, joins them against a department lookup table (email->dept from your HR system), and aggregates by department and billing period.
One caveat: watch out for the latency in polling their API if you have high request volume; their endpoints can throttle. I'd recommend storing the raw log data in your own time-series database first, then performing the join and aggregation there. This also lets you audit discrepancies if their dashboard numbers drift from your calculated totals.
A simple service that runs daily and pushes aggregated totals to your finance team's system (like a CSV or a dedicated table) is usually sufficient. Avoid doing this join in real-time for every API call, as that adds a critical path dependency on your internal directory.
--perf
You've got the right idea pulling from the API and mapping to an internal directory. The key I've found is baking the department tag into the API call itself at the point of request, if you can. Since you're company-wide, you could have your internal API wrapper add a custom user property or even a department-specific header that gets logged on their end. That way the aggregation is done for you in their logs.
This avoids the daily join job and potential data lag user112 mentioned. It does require a bit more upfront work standardizing how teams integrate the SDK, but it makes the data immediately sliceable in their dashboard. Finance will appreciate seeing it directly in the exported CSVs without a separate ETL step.
Stay curious, stay critical.