A fuse box for your AI bill. Drop-in budget guardrails and runaway-loop kill switch for Claude and OpenAI agents.
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file costfuse-0.2.0.tar.gz.
File metadata
- Download URL: costfuse-0.2.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e9363083838d7ab46d37ee943acf64d5be6e0eaa1f178ea13e4179bfe0ccfab
|
|
| MD5 |
30fc9dcdd9980a666475aa583bfea78b
|
|
| BLAKE2b-256 |
5358690aa7efa5da87090f3213df8e00b5be88d77b11d48591ad0f395db9b2eb
|
File details
Details for the file costfuse-0.2.0-py3-none-any.whl.
File metadata
- Download URL: costfuse-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc27b2d94dfd89d8d12fd03a5ed31e8a9edd444d3c7f2149a99ef9ad7fdecbd0
|
|
| MD5 |
e3976a7d656229d4d09dfadc66030d27
|
|
| BLAKE2b-256 |
754d9a4a5e39dbc48189936a985d04742b8414c071ecdd0a45cf6304dab35952
|