rolling-cost-cap
A runaway-cost circuit breaker for LLM and metered API calls. Judges every observed cost against three independent layers: a rolling-median anomaly cap, an absolute per-call ceiling, and a cumulative monthly budget. Zero dependencies, thread-safe, fully typed, 35 tests.
pip install rolling-cost-cap
Problem
A single malformed request can make an LLM call cost 50x the usual amount: a prompt that triggers maximum-length output, a retry loop that compounds, a model misconfiguration that routes traffic to a tier 5x more expensive. Provider billing alerts fire hours later, and a fixed per-call dollar limit is either too loose to catch anomalies or too tight for legitimate heavy calls.
The core difficulty is that "expensive" is relative. $0.10 is an anomaly when your median call costs $0.02, and completely normal when it costs $0.08. A static threshold cannot express that.
Solution
CostCap keeps a rolling window of recent call costs and trips when a new cost exceeds a multiple of the window's median. The median self-calibrates to your real traffic, so the same configuration works whether your calls cost fractions of a cent or dollars. Two fixed layers back it up: an absolute per-call ceiling that catches runaways before the median has warmed up, and a monthly budget that bounds total damage.
Decision precedence: absolute ceiling, then monthly budget, then rolling median. The median layer stays dormant until min_samples costs have been recorded, so one cheap first call cannot set a hair-trigger baseline. A median of zero (free calls) also keeps it dormant rather than tripping on the first nonzero cost.
Implementation: The rolling window is a fixed-size collections.deque. The median is computed via statistics.median() on a snapshot of the deque each evaluation, which is O(n log n). At realistic API-call rates this is noise; the snapshot approach was chosen over a two-heap incremental median because it is simpler to verify and the window is small. A threading.Lock guards all reads and writes so evaluate and check are safe to call from multiple threads or concurrent coroutines.
This design came from the cost-control layer of InvoiceFlow, where a TypeScript implementation of the same three rules caps Anthropic API spend in the deployed app. This package is a port of the design, not a dependency of that app, which is a Next.js project and cannot import Python.
Usage
from rolling_cost_cap import CostCap
cap = CostCap(
multiplier=3.0, # trip when cost > 3x rolling median
window=50, # median over the last 50 calls
min_samples=5, # median layer dormant until 5 costs recorded
absolute_ceiling=1.00, # never allow a single call over $1
monthly_budget=25.00, # stop everything past $25/month
)
# After each API call, compute the observed cost and evaluate it
usage = response.usage
cost = (usage.input_tokens / 1e6) * 3.00 + (usage.output_tokens / 1e6) * 15.00
decision = cap.evaluate(cost)
if not decision.allowed:
match decision.trip:
case "absolute-ceiling":
alert(f"single call cost ${decision.observed:.2f}")
case "rolling-median":
alert(f"cost {decision.observed / decision.median:.0f}x the median")
case "monthly-budget":
disable_feature_until_next_month()
evaluate judges the cost against the history that preceded it, then records it. Recording happens even when the decision trips, because the money is already spent and hiding real spend would make the monthly total under-count. For pre-call gating with an estimate, use check, which never records:
if not cap.check(estimated_cost).allowed:
return err_budget_exceeded()
The monthly window follows the wall clock (UTC by default) and resets on calendar-month rollover. Inject a clock callable to control time in tests:
cap = CostCap(monthly_budget=25.0, clock=lambda: datetime(2026, 8, 1, tzinfo=timezone.utc))
API
| Member | Behavior |
|---|---|
evaluate(cost) -> Decision |
Judge against prior history, then record. The common post-call path. |
check(cost) -> Decision |
Judge without recording. For pre-call estimates. |
record(cost) -> None |
Record without judging. For backfilling history. |
median |
Current rolling median, or None with empty history. |
monthly_spend |
Cumulative recorded spend this calendar month. |
reset() |
Clear window and monthly state. |
Decision is a frozen dataclass: allowed, trip ("absolute-ceiling" | "monthly-budget" | "rolling-median" | None), observed, cap, median, monthly_spend. All fields reflect state at decision time, before the cost is recorded.
cap is whichever limit the cost was measured against, so what it holds depends on trip: the absolute ceiling, the monthly budget, or median * multiplier both for a rolling-median trip and for an allowed call. It is None until the median layer arms. Read it alongside trip rather than as one per-call ceiling, or a message like "cost $0.50 exceeded cap $25.00" will print a monthly budget as though it were a limit on that single call.
Costs may be int, float or Decimal, since Decimal is what a money caller reaches for first; it is converted once at the boundary and the rolling window stays float. Costs must be non-negative and finite, so NaN, infinity, negatives, bool and non-numeric types raise instead of being silently ignored.
Benchmarks
evaluate throughput on an Apple M1, CPython 3.14.2, single thread, via
PYTHONPATH=src python bench.py. Best of five repeats after a warm-up pass,
and three separate invocations agreed within 2%:
| Window | Throughput | Latency |
|---|---|---|
| 50 (default) | 195,000 ops/sec | 5.1 us |
| 200 | 77,500 ops/sec | 12.9 us |
| 1,000 | 15,300 ops/sec | 65.3 us |
Known limitations
- State is in-process. Each worker, container, or serverless instance caps independently. A fleet of N workers can collectively spend up to N times the monthly budget. Acceptable as an anomaly detector and defense-in-depth; not a substitute for a provider-side billing cap.
- The rolling median can drift under a slow ramp. Costs that grow gradually (each within
multiplierof the current median) raise the baseline without ever tripping. The absolute ceiling is the backstop for this case. evaluateis post-call. It detects that money was spent; it does not prevent the spend. Real prevention lives upstream: bounded retries,max_tokenslimits, andcheckwith estimates where estimation is feasible.- Monthly rollover is calendar-based and UTC by default. There is no proration and no billing-cycle alignment beyond what a custom
clockgives you. - No persistence. A process restart forgets both the window and the month-to-date spend.
- Median lookup is O(n log n), not O(1).
_median()re-sorts the window on everyevaluate/checkcall viastatistics.median. Verified fine throughwindow=1000(see Benchmarks); pushing window into the hundreds of thousands would need a two-heap incremental median instead.
Metadata
Release files for rolling-cost-cap 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| rolling_cost_cap-0.2.0.tar.gz | 9.5 kB | With provenance | Details for rolling_cost_cap-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| rolling_cost_cap-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for rolling_cost_cap-0.2.0-py3-none-any.whl |
Total release size: 17.2 kB
Release files / rolling_cost_cap-0.2.0.tar.gz
| Download URL | rolling_cost_cap-0.2.0.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f9c72ce7a1c373f0146ad17186081be927bbf8321c5563f41075731b0425535b
|
|
BLAKE2b-256 checksum How to use checksums |
879f9babc58edb5f84f549bae5ecf08373e26b831759887f8628d92947b4a17e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logRelease files / rolling_cost_cap-0.2.0-py3-none-any.whl
| Download URL | rolling_cost_cap-0.2.0-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fc043d1ecd8662a0efe855d0464085f3432beaf658ce29ad13943ea2b2af2c0d
|
|
BLAKE2b-256 checksum How to use checksums |
9b5a354eeeaa8222efec17f4f924b745ab6295d07e03c86612e18708e69c86de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logSecurity for rolling-cost-cap 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for rolling_cost_cap-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 28, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.