Every so often someone posts about leaving an agent running overnight and waking up to a surprising API bill. An agent stuck in a loop will keep calling the API until something stops it.
I didn't want that to happen to me, so I added a daily API budget to Saifuban, the spending guard for agents I've been building.
https://ut181749ut.github.io/saifuban/?src=devto
How it works
You store your provider API key in Saifuban and get a proxy key (sbk_...) back. The agent only ever sees the proxy key.
Requests go through Saifuban to the real API. Saifuban records the cost from the usage each response reports, and once the agent's spend for the day reaches its budget, it stops forwarding requests and returns an error instead:
403 [Saifuban] Over daily budget ($10.00)
The budget resets the next day.
Supported providers
- OpenAI
- Anthropic
- Google (Gemini)
- OpenRouter
OpenRouter reports the actual cost, so that's the most accurate. For the other three, the cost is calculated from token counts and the price table. Streaming responses are metered too.
Setup
- Open the site, press Start, and create an agent
- In Settings, store the API key for the provider you use
- Put the proxy base URL and the proxy key into your agent's config
For Claude Code, that's two environment variables:
export ANTHROPIC_BASE_URL="https://zrphnuvrsldcycxtdftv.supabase.co/functions/v1/saifuban-proxy/anthropic"
export ANTHROPIC_API_KEY="sbk_..."
Tools built on the OpenAI SDK work the same way with OPENAI_BASE_URL and OPENAI_API_KEY.
Budgets are per agent, so a research agent can get $1/day and a coding agent $10/day.
Things to know
The budget check happens before each request. Requests that are already in flight when the budget is reached still go through, so the day can end slightly over budget, by at most those requests.
You are also handing your real API key to a third party. It's kept in an encrypted vault, only the last few characters are ever shown, and prompts and responses are not stored. If that still bothers you, create a separate key with its own spending limit on the provider side and store that one.
It also handles other payments
The original idea behind Saifuban is that agents ask before spending money. Small amounts are approved automatically, and anything above your limit goes to your phone as a notification you can approve or reject. With the API cap on top, most of the ways an agent can spend money are covered.
It's free, there's no email signup, and amounts can be shown in USD or JPY.
If something's missing for your setup, I'd like to hear about it in the comments or on GitHub.

Top comments (0)