Fuseboxie Python SDK for guarding AI calls and tracking token usage from backend applications.
Project description
Fuseboxie Python SDK
Backend SDK for tracking AI usage and blocking AI calls when a Fuseboxie guard rule says no.
Install
pip install fuseboxie
Package Checks
Run the release check before publishing:
npm run check
That command runs unit tests, compiles the SDK/examples, removes old package artifacts, and builds both the wheel and source distribution in dist/.
After publishing, verify the public package and send one smoke event:
pip install --upgrade fuseboxie
set FUSEBOXIE_PROJECT_KEY=fbxi_live_your_project_key
python scripts/smoke_pypi_usage.py
Usage
from fuseboxie import init
fuseboxie = init(
project_key="pk_live_123",
api_url="https://api.fuseboxie.com",
guard_failure_mode="throw",
request_timeout=3,
)
result = fuseboxie.can_use_ai(
user_id="user_123",
customer_id="customer_123",
role="trial",
estimated_tokens=2000,
estimated_cost_usd=0.02,
operation="chat.completion",
)
if not result["allowed"]:
raise RuntimeError(result.get("reason") or "AI usage is blocked.")
response = openai_client.chat.completions.create(...)
fuseboxie.track_openai(
response,
user_id="user_123",
customer_id="customer_123",
role="trial",
operation="chat.completion",
)
When api_url is configured, init() automatically sends a lightweight setup check to Fuseboxie. This lets the Fuseboxie dashboard mark the project as connected as soon as your backend starts.
If you want to wait for that setup check during a smoke test, call:
fuseboxie.wait_for_setup_check(timeout=10)
guard_failure_mode controls what happens if Fuseboxie cannot be reached before an AI call:
throwfails the current request with an SDK error. This is the default.allowlets the AI call continue when the guard service is unavailable.blockblocks the AI call when the guard service is unavailable.
Provider Helpers
track_openai(response, **context)mapsusage.prompt_tokens,usage.completion_tokens,usage.total_tokens, and the newerusage.input_tokens/usage.output_tokensshape.track_anthropic(response, **context)mapsusage.input_tokensandusage.output_tokens.track_gemini(response, **context)mapsusageMetadata.promptTokenCount,usageMetadata.candidatesTokenCount, andusageMetadata.totalTokenCount.
You can still call track_usage() manually when you already have normalized token counts:
fuseboxie.track_usage(
provider="openai",
model="gpt-4.1-mini",
input_tokens=1200,
output_tokens=420,
user_id="user_123",
customer_id="customer_123",
role="trial",
operation="chat.completion",
)
Role is stored as safe metadata so role-based reports and limits can work.
FastAPI Helper
FastAPI apps can attach Fuseboxie once as ASGI middleware, then guard and track from each route with the request object:
from fastapi import FastAPI, HTTPException, Request
from fuseboxie import init
from fuseboxie.fastapi import (
FuseboxieBlockedError,
FuseboxieMiddleware,
guard_ai_request,
header_value,
track_openai_response,
)
app = FastAPI()
fuseboxie = init(project_key="pk_live_123", api_url="https://api.fuseboxie.com")
app.add_middleware(
FuseboxieMiddleware,
client=fuseboxie,
user_id_getter=header_value("x-user-id"),
customer_id_getter=header_value("x-customer-id"),
role_getter=header_value("x-user-role"),
)
@app.post("/chat", name="chat")
def chat(request: Request):
try:
guard_ai_request(request, estimated_tokens=2000, estimated_cost_usd=0.02)
except FuseboxieBlockedError as error:
raise HTTPException(status_code=429, detail=str(error)) from error
response = openai_client.chat.completions.create(...)
track_openai_response(request, response)
return {"ok": True}
The FastAPI helper is optional. It does not add FastAPI as a package dependency; it works when imported inside a FastAPI or Starlette app.
Flask Helper
Flask apps can attach Fuseboxie once with init_app, then guard and track from
each route without repeating user/customer headers in every SDK call:
from flask import Flask, jsonify
from fuseboxie import init
from fuseboxie.flask import (
FuseboxieBlockedError,
guard_ai_request,
header_value,
init_app,
track_anthropic_response,
)
app = Flask(__name__)
fuseboxie = init(project_key="pk_live_123", api_url="https://api.fuseboxie.com")
init_app(
app,
client=fuseboxie,
user_id_getter=header_value("x-user-id"),
customer_id_getter=header_value("x-customer-id"),
role_getter=header_value("x-user-role"),
)
@app.post("/chat", endpoint="chat")
def chat():
try:
guard_ai_request(estimated_tokens=2000, estimated_cost_usd=0.02)
except FuseboxieBlockedError as error:
return jsonify({"error": str(error)}), 429
response = anthropic_client.messages.create(...)
track_anthropic_response(response)
return jsonify({"ok": True})
The Flask helper is optional. It imports Flask only when Flask helpers are used.
Framework Examples
The package includes examples for common Python backends:
examples/fastapi.pyexamples/flask.pyexamples/django_view.py
The SDK validates and normalizes usage events. It can send guard checks and usage events to the Fuseboxie API with api_url, and transports are injectable for custom testing or forwarding.
Project details
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 fuseboxie-0.1.3.tar.gz.
File metadata
- Download URL: fuseboxie-0.1.3.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9d39a0c86dfb30538822325f5f0b1e15a0205186f906758a4e00fce544068e7
|
|
| MD5 |
0a8762c66d7b94692b7da8bf430b4bdb
|
|
| BLAKE2b-256 |
be4c58d81f2fbc36ae202a2b33dd236342f49e43ae8aaf2039b439e85ae4d78f
|
File details
Details for the file fuseboxie-0.1.3-py3-none-any.whl.
File metadata
- Download URL: fuseboxie-0.1.3-py3-none-any.whl
- Upload date:
- Size: 11.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
817571d74a70692b1bec9e09dc61b06e2d9e2a2b993013d75ac494f702128b91
|
|
| MD5 |
a206e5e476b5e12f915fd6b9546cbeef
|
|
| BLAKE2b-256 |
4e9c7d53e5cf794944f1611ba9e896ac6e9d08d7f0fb0c44640955e482f28818
|