@safetnsr/spend-cap
v0.1.0
Published
Client-side hard cap for AI API spending — no provider support needed
Maintainers
Readme
spend-cap
client-side hard cap for AI API spending — no provider support needed
install
npm install @safetnsr/spend-capusage
# add to any Node.js app that calls AI APIs
SPEND_CAP=5 node --import @safetnsr/spend-cap/register app.js
# or with --require for CJS
SPEND_CAP=5 node -r @safetnsr/spend-cap/register app.jswhen your daily spend hits $5, the next API call throws SpendCapError instead of making the request.
how it works
patches globalThis.fetch to intercept requests to:
- api.openai.com
- api.anthropic.com
- generativelanguage.googleapis.com
tracks cumulative daily spend in ~/.spend-cap-state.json. resets at midnight UTC.
cli
# check today's spend
spend-cap status
# reset spend counter
spend-cap resetflags
| env var | description | default | |---------|------------|---------| | SPEND_CAP | daily cap in USD | (required) | | SPEND_CAP_STATE_PATH | custom state file path | ~/.spend-cap-state.json |
supported models
accurate pricing for: gpt-4o, gpt-4o-mini, gpt-4.1, gpt-5.4, claude-sonnet, claude-opus, claude-haiku, gemini-flash, gemini-pro. fallback estimation for unknown models.
agent interface (--json)
spend-cap status --json outputs:
{
"date": "2026-03-07",
"spentUsd": 3.42,
"capUsd": 5.00,
"remainingUsd": 1.58,
"percentUsed": 68.4
}pair with
- @safetnsr/vet — audit your AI coding workflow
- @safetnsr/pinch — track AI costs over time
