CYBERAUTOMATIONSStart a project
AI spend controlIn daily use

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. Streaming, structured output and the half-price Batch API go through the same budget check.

How it flows

Pipelineclaude-budgetLedgerClaude APIcall(consumer, model, max_tokens)Spend so far this monthConsumer and global totalsBudgetExceeded if the worst case crosses a capSend the requestMessage and token usageRecord actual costMessage
  1. Pipeline to claude-budget: call(consumer, model, max_tokens)
  2. claude-budget to Ledger: Spend so far this month
  3. Ledger to claude-budget: Consumer and global totals
  4. claude-budget to Pipeline: BudgetExceeded if the worst case crosses a cap
  5. claude-budget to Claude API: Send the request
  6. Claude API to claude-budget: Message and token usage
  7. claude-budget to Ledger: Record actual cost
  8. claude-budget to Pipeline: Message
Every call is priced before it is sent and logged after it returns. Solid arrows are requests, dashed arrows are replies.
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: Message

Tech 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

Calling Claude under a cap
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 limited
How a pipeline calls Claude through the library and falls back when the budget says no.

Source code

The code is public. View the Claude Budget repository on GitHub.

Service

Private AI infrastructure

LLM gateways, model routing and cost control, run on infrastructure you own.

See all custom build services

More builds

Last updated 2026-11-11

Want something like this on your data?

Tell us what you want automated. If it fits, we will tell you exactly what it looks like and what it costs.