Now that you are using
Claude code how can you track and control your costs?
I have my own personal Claude set up at home and I want to make sure I do not
overspend. I put out on article covering
how to set up an account with monthly limits https://www.whiteboardcoder.com/2026/03/claude-creating-account-and-setting-up.html [1]
That article covers how
to set up your account and set up a workspace.
I am going to assume you have a workspace and are accessing your account via
the command line. (I am also assuming
you are running this in Ubuntu 24.04 if you are on OS X you may have to tweak
some of the scripts)
Get the actuals
Let’s first create a
curl just to make sure I can pull the data in I want.
What do you need? You need admin privileges. You cannot get your costs from a workspace API. But since I am the boss of my account I can make this happen ๐ But if you are running a larger or you probably want to put some kind of proxy in place for your
Let me create a new
admin key
Head over to https://platform.claude.com/ and login
Click API Keys
Click on Admin Keys
Click on Create Admin Key
Give it a name. And click Add
I am calling mine cost-key. Currently an
admin key has access to everything… I hope in the future they tweak that so you
can limit a key to only access certain things like costs ๐
But for now its all powerful so be careful!
Copy the key.
You will see that admin key names start with sk-ant-admin.
Make sure to copy this key someplace safe as you cannot get it again. You can generate a new key but you can’t get
this key again.
Claude has a cost_report api endpoint. You can check their docs here https://platform.claude.com/docs/en/api/admin/cost_report/retrieve [2]
From that we can create a curl command to test ( I placed my key at ~/.claude-key-cost so that its simpler to ingest and I don’t want to put keys in my cmd history ๐
|
> curl -s
"https://api.anthropic.com/v1/organizations/cost_report?\
|
This will get me data
from 3/1/26 from 00:00 UTC to the present time… Well it is paginated so you may
have to call the next page.
You can also pass it an end date
|
> curl -s
"https://api.anthropic.com/v1/organizations/cost_report?\
|
Here I just got one day
back and you can see here I spent $0.15 that day. The amount will be in your smallest local
currency. So for USA it’s in pennies.
You can also get the results by workspace id, which is what I want so I can get the cost per workspace I have. Currently I only have one workspace, but in the future I plan to make a few more and I want all the info.
|
> curl -s
"https://api.anthropic.com/v1/organizations/cost_report?\
|
That is nice but I really want the workspace name…
I don’t see a way to get
the Workspace ID in the claude web portal.
Let me find a API to grab them.
Here it is https://platform.claude.com/docs/en/api/admin/workspaces/list [3]
|
> curl -s
https://api.anthropic.com/v1/organizations/workspaces \
|
That worked now I can tie the ID into the name.
One more thing, I want
to calculate my usage per month and what I have left over. Looking up some info it looks like the month
begins at UTC 00:00 time so… 2026-03-01T00:00:00Z (for march)
I do need to get my current workspaces monthly limit so I can calculate it….
After some research it looks like they do not yet have an
API end point that pulls this info…. Dang
maybe in a few months..
So for now I can hard code a number and use that until I can get an API
endpoint I can hit.
Let’s first create a simple command line tool to get the current monthly actuals.
|
> sudo vi /usr/bin/cost |
Here is the code
|
#!/usr/bin/env
python3
params["page"] = next_token # docs use "page=..." for next requests page_count += 1 return all_results #
------------------------------------------------ days_in_month = (end_month -
start_month).days + 1 spent_arr = {}
for workspace in spent_arr: #Under 15% left if
__name__ == "__main__":
|
Or download the code
from this gist https://gist.github.com/patmandenver/0001b0ac2500b7bfe69eaa92a8988d64
Change the permissions
|
> sudo chmod 755 /usr/bin/cost |
Now let’s try it.
|
> cost |
OK that gets me what I want.
This gets every workspace I have.
For now that works.
In the near future I would like to create a tool so that any person from a
workspace could access their workspace’s current monthly budget.
If I had to do that today I would have to make some API tool that others could
query like an AWS lambda. But I don’t
want to put my admin key in an AWS lambda function… too much power.
So hopefully in a few months, Ideally a person who has permissions into a
workspace with their workspace API should be able to query this with the
workspace key.
Here is hoping ๐
References
[1] Claude creating an account
and setting up limits
https://www.whiteboardcoder.com/2026/03/claude-creating-account-and-setting-up.html
Accessed 03/2026
[2] Claude CostReport API
https://platform.claude.com/docs/en/api/admin/cost_report/retrieve
Accessed 03/2026
[3] List Workspaces API
https://platform.claude.com/docs/en/api/admin/workspaces/list
Accessed 03/2026
No comments:
Post a Comment