A hard monthly cap on Claude API spend
A shared Python library that every pipeline calls Claude through. It prices the worst case before each request, refuses calls that would cross a global or per-pipeline cap, and logs every call to a SQLite ledger.
- pipelines with their own cap
- 12
- error types, each with a clear fallback rule
- 7
- lines of Python
- ~1,440
The problem
Once several scripts call a paid model API, nobody can say what the month will cost until the invoice arrives. One runaway loop in one pipeline can spend the budget meant for all of them.
Each script also handled errors its own way, so a rate limit in one place crashed a job and in another place silently retried until it hit the cap.
The approach
- Every pipeline calls Claude through one function and names itself as a consumer. Caps for each consumer and for the whole month live in one small JSON file.
- Before a request is sent, the library prices its worst case from the model's price table and the request's max_tokens. If that worst case would cross the consumer cap or the global cap, the call is refused with BudgetExceeded and never reaches the API.
- Every call, allowed or refused, lands in a SQLite ledger with the consumer, model, token counts and actual cost, so the monthly number is a query, not a guess.
- Errors are sorted into types with one rule: anything that means "try the free model instead" (no key, over budget, rate limited, overloaded, auth failure) raises one catchable family, and a malformed request surfaces as a bug.
- The API key is read from its own variable and passed in explicitly, and the base URL is pinned, so a machine-wide setting cannot quietly move other tools onto the paid credit.
- Streaming, structured output and the half-price Batch API go through the same budget check.
How it flows
- Pipeline to claude-budget: call(consumer, model, max_tokens)
- claude-budget to Ledger: Spend so far this month
- Ledger to claude-budget: Consumer and global totals
- claude-budget to Pipeline: BudgetExceeded if the worst case crosses a cap
- claude-budget to Claude API: Send the request
- Claude API to claude-budget: Message and token usage
- claude-budget to Ledger: Record actual cost
- claude-budget to Pipeline: Message
Show this diagram as Mermaid text
sequenceDiagram
participant P0 as Pipeline
participant P1 as claude-budget
participant P2 as Ledger
participant P3 as Claude API
P0->>P1: call(consumer, model, max_tokens)
P1->>P2: Spend so far this month
P2-->>P1: Consumer and global totals
P1-->>P0: BudgetExceeded if the worst case crosses a cap
P1->>P3: Send the request
P3-->>P1: Message and token usage
P1->>P2: Record actual cost
P1-->>P0: MessageTech stack
- Language
- Python 3.10+
- API client
- Anthropic Python SDK
- Ledger
- SQLite
- Caps
- One JSON file: global cap, reset day, per-consumer caps
- Tests
- pytest
Results
- Twelve pipelines share one monthly budget, each with its own ceiling, and none of them can spend past it.
- When the budget is gone, pipelines fall back to free models on their own instead of failing.
- Monthly spend per pipeline is one SQL query against the ledger.
Every figure on this page comes from the project's own code, tests or notes. Where there is no measured number, we describe the outcome in words.
Demo
from claude_budget import call, BudgetExceeded, BudgetUnavailable
try:
msg = call("portal-chat", model="claude-sonnet-5-5",
max_tokens=2000, messages=[...])
except (BudgetExceeded, BudgetUnavailable):
return free_chain(text) # cap reached, no key, rate limitedSource code
The code is public. View the Claude Budget repository on GitHub.
Related
Service
Private AI infrastructure
LLM gateways, model routing and cost control, run on infrastructure you own.
See all custom build servicesFree tools built the same way
More builds
- AI Stack CVE Labs: Security research
- Agentic Agency: Agent framework
- AI Usage Dashboard: AI infrastructure and operations
- Geo-Intel: Data pipeline
- Security Knowledge Base: Knowledge base
- RapidAPI MCP: API integration
- Flightdeck: Agent operations
- Niagara BQL and NEQL Reference: OT reference
- IG Sanitize: Privacy tool
- WinHostPEAS: Security tooling
Last updated 2026-11-11