How to put a hard daily spending cap on Claude Code
If you run Claude Code (or any agent) with an API key, there's nothing on your machine that stops it from spending. A loop at 2am will happily keep going until you wake up. Here's how I cap it per day, so the worst case
If you run Claude Code (or any agent) with an API key, there's nothing on your machine that stops it from spending. A loop at 2am will happily keep going until you wake up.
Here's how I cap it per day, so the worst case is "it stopped at ¥1,000 / $7" instead of "check the invoice".
What the providers give you
Worth setting these first, they're free:
- Anthropic Console: a monthly spend limit per workspace. It's monthly, not daily, and it can take a bit to kick in, so people have gone past it.
- OpenAI: hard spend limits per organization or project (added this year). Same idea, also not instant.
- Turn off auto-reload on any account an agent can reach.
The gap is that these are monthly and per workspace. One bad night can use the whole month, and every agent on that workspace shares the same pool.
A daily cap per agent
The trick is to never give the agent the real key. The agent talks to a small proxy with its own key, the proxy holds the real one and checks the budget before every call.
I use Saifuban for this (I made it, it's open source). Setup is one command:
npx github:ut181749ut/saifuban
It asks for a daily budget and your Anthropic key, then prints two lines:
export ANTHROPIC_BASE_URL="https://saifuban.saifuban-app.workers.dev/proxy/anthropic"
export ANTHROPIC_API_KEY="sbk_..."
Put those in the shell where Claude Code runs. That's it. Claude Code works the same, it just goes through the proxy.
When the day's budget is used up, the next call gets refused with a message like:
[Saifuban] This call could cost up to ¥48, which would go over the daily budget (¥12 left)
and Claude Code stops instead of looping.
Why "before the call" matters
My first version counted cost after each call finished. That breaks as soon as you have parallel calls or retries: ten calls can all start while you're still under the limit, and you only find out after they finish.
So now the proxy reserves the most a call could cost before sending it (input size + max_tokens × the model's price), and settles to the real number when it ends. Reservations count against the budget, so parallel calls can't all slip through. If a request doesn't set max_tokens, the proxy sets one so the model can't write past what was reserved.
A few other things I had to handle:
- a stream that gets cut before the usage arrives is charged at the reservation, not zero
- a retried or late callback can't charge twice
- web searches are counted (they're billed per search, not per token)
- responses that take several minutes still stream to the end
I wrote up the test for the tricky cases (parallel calls under a ¥100 cap, delayed + retried + cancelled callback) here.
Other tools
Same setup works for the OpenAI SDK, LangChain, CrewAI and the Vercel AI SDK. You swap the base URL and the key. Examples are in the README.
If you only take one thing from this: give agents a key that has a daily limit, not your real key.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.