costfuse (Python)
A fuse box for your AI bill.
Drop-in budget guardrails and runaway-loop kill switch for AI agents. Wrap your Anthropic or OpenAI client; costfuse kills requests before they spend money you didn't plan for, and writes an audit log you can hand to a regulator.
The Python version of costfuse — feature parity with the Node SDK.
Install
pip install costfuse
Use it
from anthropic import Anthropic
from costfuse import wrap, CostfuseConfig
claude = wrap(Anthropic(), CostfuseConfig(
max_spend_per_hour=5.00,
max_same_prompt_in_window={"count": 5, "window_ms": 60_000},
max_calls_per_minute=60,
audit_log_path="./costfuse-audit.jsonl",
))
# Use it exactly like the original client:
claude.messages.create(
model="claude-haiku-4-5",
max_tokens=200,
messages=[{"role": "user", "content": "Hello."}],
)
If a rule trips, the call raises CostfuseBlocked before any tokens are spent.
Rules
| Rule | What it blocks |
|---|---|
max_spend_per_hour |
Hard USD cap in the last 60 minutes |
max_spend_per_day |
Hard USD cap in the last 24 hours |
max_calls_per_minute |
Rate limit |
max_same_prompt_in_window |
Same prompt fingerprint repeating — runaway-loop detection |
max_recursion_depth |
Agent calling itself too deeply |
OpenAI also supported
from openai import OpenAI
from costfuse import wrap, CostfuseConfig
oai = wrap(OpenAI(), CostfuseConfig(max_spend_per_hour=5.00))
oai.chat.completions.create(model="gpt-4o-mini", messages=[...])
Recommended onboarding: log-only first, enforce later
For your first week, run with kill_on_breach=False. Costfuse will observe and write the audit log without blocking anything.
claude = wrap(Anthropic(), CostfuseConfig(
max_spend_per_hour=10,
kill_on_breach=False, # observe-only
audit_log_path="./costfuse-audit.jsonl",
))
Local testing
Three test scripts ship with the repo:
cd python
pip install -e ".[dev]"
python examples/mock_test.py # zero spend, all rules
python examples/loop_test.py # simulated runaway, zero spend
ANTHROPIC_API_KEY=sk-... python examples/claude_test.py # real calls, < $0.01 spend
License
Apache-2.0
Metadata
Release files for costfuse 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 | |
|---|---|---|---|
| costfuse-0.2.0.tar.gz | 7.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| costfuse-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.5 kB
Release files / costfuse-0.2.0.tar.gz
| Download URL | costfuse-0.2.0.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2e9363083838d7ab46d37ee943acf64d5be6e0eaa1f178ea13e4179bfe0ccfab
|
|
BLAKE2b-256 checksum How to use checksums |
5358690aa7efa5da87090f3213df8e00b5be88d77b11d48591ad0f395db9b2eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Release files / costfuse-0.2.0-py3-none-any.whl
| Download URL | costfuse-0.2.0-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc27b2d94dfd89d8d12fd03a5ed31e8a9edd444d3c7f2149a99ef9ad7fdecbd0
|
|
BLAKE2b-256 checksum How to use checksums |
754d9a4a5e39dbc48189936a985d04742b8414c071ecdd0a45cf6304dab35952
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|