Python SDK for TokenFuse — runtime cost control for AI agents (import as `tokenfuse`).
Project description
tokenfuse (Python SDK)
Thin, dependency-free helpers to route an agent's LLM calls through the
TokenFuse gateway and turn its 402
blocks into typed exceptions.
TokenFuse is a drop-in proxy — you don't rewrite your agent, you point your provider client at the gateway and attach a few headers.
Install
pip install tokenfuse # (planned; for now: pip install -e sdk/python)
Use
import anthropic, tokenfuse
client = anthropic.Anthropic(
base_url=tokenfuse.gateway_url(), # http://127.0.0.1:4100
default_headers=tokenfuse.run_headers("run-42", budget_usd=5.0, task_type="code-review"),
)
try:
msg = client.messages.create(model="claude-sonnet", max_tokens=1024, messages=[...])
except tokenfuse.BudgetExceeded as e:
print(f"run {e.run_id}: spent ${e.spent_usd} of ${e.budget_usd}")
except tokenfuse.LoopDetected as e:
print(f"runaway loop on {e.run_id}: {e.reason}")
For raw HTTP clients (requests / httpx), call tokenfuse.check_response(resp)
after the request, or tokenfuse.raise_for_fuse(status_code, body).
Exceptions
All inherit tokenfuse.FuseError (fields: run_id, budget_usd, spent_usd,
policy_id, reason):
BudgetExceeded— the run's budget would be exceededLoopDetected— a runaway loop was detectedPolicyViolation— a policy limit was hitKilled— an operator killed the run
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 tokenfuse_sdk-0.3.0.tar.gz.
File metadata
- Download URL: tokenfuse_sdk-0.3.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea76c4ca7a85932f9d44995998801f0c5885aec727e92cb63ca37b029ace4089
|
|
| MD5 |
cc34c3f44732b7e58b17a3b36fbdab4a
|
|
| BLAKE2b-256 |
b3ac1ca1a7d2fd6def99ac67abaee97e606609086cb91103ce77b6ee37b2ec58
|
File details
Details for the file tokenfuse_sdk-0.3.0-py3-none-any.whl.
File metadata
- Download URL: tokenfuse_sdk-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
629cf79dbaa6901a4f00907ba85ca26ca99c37a468fb723955109cbb87ea37e9
|
|
| MD5 |
e638f601c36fd0f5253a7cc54d111ef0
|
|
| BLAKE2b-256 |
f2fb0ec8f9ef8f6d8af22a515d88c84b46df36fc43c02891d8eb90e5b5d109d7
|