Thread-safe shared token + USD budget for concurrent LLM tasks. Raises BudgetExceeded on push past cap. Reserve/commit two-phase API for fan-out workloads. Zero runtime deps.
Project description
token-budget-py
Thread-safe shared token + USD budget for concurrent LLM tasks.
Fan-out workloads — agents, parallel summarizers, batch evals — race many
tasks to consume from one shared budget. This library is a small,
zero-dependency counter with two axes (tokens, USD) that returns
BudgetExceeded when a record would push past a configured cap.
Sibling to the Rust crate
token-budget-pool.
Install
pip install token-budget-py
Use
from token_budget import BudgetPool, BudgetExceeded
pool = BudgetPool(token_cap=1_000_000, usd_cap=10.0)
try:
pool.record(tokens=1200, usd=0.0036)
except BudgetExceeded as e:
# tell this worker to skip
print(f"out of budget: {e}")
Two-phase commit (reserve before the LLM call, commit the actual usage):
with pool.reserve(tokens=2000, usd=0.012) as r:
result = call_llm(prompt)
r.commit(tokens=result.usage.total_tokens, usd=result.cost_usd)
If the with block exits without r.commit() (e.g. the LLM call raised),
the reservation is auto-released — no orphaned slots.
Either axis is optional:
only_tokens = BudgetPool(token_cap=500_000) # USD unbounded
only_usd = BudgetPool(usd_cap=5.0) # tokens unbounded
unbounded = BudgetPool() # both unbounded (counter only)
Atomic read of current state:
snap = pool.snapshot()
snap.tokens_used # 1200
snap.usd_remaining # 9.9964
snap.tokens_remaining # 998800 (cap - used - reserved)
What it does NOT do
- No async runtime lock-in. Works under
asyncio,trio, threads, sync. The internal lock is a plainthreading.Lock(held only for the microseconds of a counter update). - No HTTP. Doesn't talk to any LLM provider.
- No cost calculation. Wrap a cost calculator that returns USD per call
and feed the result into
record. (Seeclaude-cost,openai-cost,gemini-cost,bedrock-coston crates.io for Rust cost calculators with the same authorship.) - No persistence. Counts live in process. For multi-process / multi-host budgets, wrap a Redis or DB increment instead.
- No automatic rollover. Call
pool.reset()from your own cron / time loop if you want a periodic window.
License
MIT
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 token_budget_py-0.1.0.tar.gz.
File metadata
- Download URL: token_budget_py-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41c699e9e48f666c280f9cee44cdb5cc16b6416c6f69ebceecf0e809be893e33
|
|
| MD5 |
33e4bcb787d72ee098a30a2c8b86b6f1
|
|
| BLAKE2b-256 |
f50a549513e9b5f15726144e030d0c94fbe1a2327d17a33de5d5e441d2d89a71
|
File details
Details for the file token_budget_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: token_budget_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a97a7255228c394d5f5c71afc34025991909efe86c34b2cee5758b086d865569
|
|
| MD5 |
bd8168a23bcd926a1d5e8595c0a2a614
|
|
| BLAKE2b-256 |
82e74bae5903e0e4f134c09fb945128829cce7fa0f06fd7813c38e616784e6f4
|