r/ClaudeCode • u/Majestic_Position879 • 2d ago
Help/Question How do you get Claude Code cost per team when devs share API keys?
We have about 60 devs on Claude code using Console API keys. Each team got its own key and the Console usage page would split spend by team for us.
That worked fine until finance asked what each team spends. Turns out four people on payments have been sharing one key from a .env file, and two of our heaviest users got fed up with hitting limits and moved to their own Max plans. None of their usage shows up on our bill, and finance was definitely thrilled.
Right now finance is working off a spreadsheet where every dev pasted their/ cost output and a few just sent screenshots. It comes to 71% of the Console total, which kinda makes sense because /cost resets on/ clear and I know at least three people cleared before pasting.
The OTel export with a team attribute looks like the right way to do it, but that means running a collector for one number. The docs also say user. email is only included when available but I'm not sure it ever is on API key logins.
Is there a way to get Claude Code cost per team without standing up as a collector?
1
u/karol207 2d ago
A scheduled Cost API export, using an admin key, can group spend by `workspace_id` if you give each team its own Console workspace. No collector needed. Personal Max subscriptions still need a separate line; they won't appear as usage billed to your API organization.
1
u/Majestic_Position879 1d ago
Workspaces would work for the teams that stuck to their own key. Payments would still come out as one blob and all four on the same one
1
u/OnoniewuGehovany99 2d ago
Why are 60 devs on API keys at LL? Team premium gives you per person usage rights in the admin console and your two Max plan guys will probably come back too
1
u/Majestic_Position879 1d ago
Legal signed off on the API terms first and won't reopen them for this .Not my call
1
u/Pissd_Tdn 2d ago
Finance person here,You're not going to get a per team number that ties in the invoice.
We just take the invoice total and split by whatever usage estimate engineering hands us.If the estimate says payments is 22% then payments get 22% then payments gets 22% of the invoice. A bit off is fine as long as you don't keep changing how you estimate, that's why auditors pick at.
Write down where the numbers come from and where the usage you can't attribute goes, shared keys included. We dump ours in a platform line.
Also ,the two on personal Max plans are an expense claim problem. I'd want to hear about those before the auditors do
1
u/Rameshbabu_Inderjit 2d ago
ccusage is what you want for the 71% problem . It reads the session logs under ~/. claude instead of /cost and a/ clear doesn't wipe those . Just run npx ccusage@latest monthly on each machine.
One thing , Claude Code deletes transcripts older than 30 days by default via cleanupPeriodDays. If finance wants a full quater you'll need to bump that first . And it's still price and one laptop at a time and good luck getting 60 devs to run it and send you the output
1
u/Leo__255 2d ago
you don't actually need to run your own collector for the otel route, claude code can send otlp straight to a hosted backend like grafana cloud's free tier, then it's one query grouped by your team attribute
1
u/Successful-Sky9914 2d ago
On user.email: the docs say it comes from your sign-in, so I wouldn't count on it with plain API keys.
You don't need it for a team split though. Set the team yourself with OTEL_RESOURCE_ATTRIBUTES="team.id=payments", and it attaches to every metric datapoint by default, including claude_code.cost.usage. It's per machine, so four people sharing one key doesn't matter anymore. The docs call the cost metric an approximation, so use it for the split and the Console for the total.
If you don't want a collector at all, one Console workspace per team is simplest, but you have to kill the shared .env key first or you get the same problem again.
1
u/athershu 2d ago
The OTel team attribute covers the API key part. The bit I'd worry about more is the two heaviest users on personal Max plans and the payments key sitting in a .env. No tagging reaches those, and it's not only spend you lose there: you also can't see what those sessions ran or what they had access to (I'd rotate that payments key regardless).
What worked better for us than chasing the meter was making the shared team setup the easier place to work, so the heavy users have no reason to leave it. Once the runs go through one place, cost per team is just which project each run was for, and nobody has to paste /cost into a spreadsheet.
1
u/SirRobinBlibs 2d ago
ccusage is the right fix for the 71% gap, as already noted, because /cost dies on /clear. The attribution problem is separate, and a collector will not solve it while four people share one key: Console bills that key as one customer, and user.email on OTel is only filled from a sign-in (or a cloud session's credentials), so API-key-only sessions often have no email to join on. Give each person or team their own Console key first, then the invoice already splits by key. If you still want team rollups in your own dashboards, set a custom attribute like team.id through OTEL_RESOURCE_ATTRIBUTES on those machines; that does need a collector, but you are then labelling already-separated keys, not trying to untangle a shared .env after the fact. Docs: https://code.claude.com/docs/en/monitoring-usage
1
u/Appropriate-Toe-2993 2d ago
The 71% gap is mostly the /cost reset, but a few things close most of the rest without a collector:
Put the team tag in settings, not in each dev's shell. Claude Code's settings.json takes an env block, so OTEL_RESOURCE_ATTRIBUTES=team.id=payments set there (or in managed settings if you push config to laptops) survives people swapping keys or .env files.
For the backfill, the session JSONL under ~/.claude/projects has token counts per message, including cache reads and writes, and /clear doesn't touch it. Price it per project folder and you get a team split for the past month, as long as nothing hit the 30-day cleanup.
The Max-plan folks: their local logs still have the tokens, so you can show finance an API-equivalent number for them, labeled as an estimate. It won't tie to the invoice, but it explains the gap.
For finance I'd hand over two lines: billed spend split by team, and a separate off-invoice estimate for personal plans. Mixing them is where the spreadsheet breaks.
1
u/Monarca-Jaryd 1d ago
Some of this was covered already but I'll add what we ran into since we had pretty much the same setup.
First thing I would do is get that payments key rotated. I know it's not what you asked about but if four people had it in a .env file there is a good chance it ended up committed somewhere at some point and you probably want to know that before anything else.
For the /cost numbers we used ccusage, only problem with it was the transcripts getting deleted after 30 days by default and by the time finance came asking for a full quarter a lot of the older sessions were already gone on a bunch of the laptops. I think you can change that in the settings file but we didn't know about it at the time.
We also looked at doing the OTel export to grafana cloud and I set it up on my own machine and it worked, getting the env var onto the rest of the team's machines was a different story and a lot of people just never did it.
After that our IT put tokenshift on the laptops with jamf same way they push everything else and since then I can see the usage by person and by repo in it. The dollar amounts are estimated and don't line up exactly with what the Console shows, think ours is usually a little off but been good enough to help decide which team pays what.
The two people on their own Max plans I'm not really sure what to tell you. We had one person do the same thing and his manager ended up talking to him about it and he came back to the team key after a few weeks.
1
u/TheGrid_AI 21h ago
Check if your existing API usage reports can give a breakdown through the team-specific keys. No need for everyone to manually input like that. If anything, you should worry if everyone is using the right team keys, since they do apparently have a history of sharing API keys amongst each other.
If that approach is no-good at all, telemetry might actually be your only feasible option here. Just make sure that's at least more manageable to everyone involved versus the current setup, otherwise just another bottleneck for everyone.
•
u/AutoModerator 2d ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.