Skip to main content

token-budget-py

PyPI Python License: MIT

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 plain threading.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. (See claude-cost, openai-cost, gemini-cost, bedrock-cost on 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

Metadata

Release files for token-budget-py 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for token-budget-py 0.1.0
File Size Uploaded Provenance
token_budget_py-0.1.0.tar.gz 7.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for token-budget-py 0.1.0
File Interpreter ABI Platform
token_budget_py-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / token_budget_py-0.1.0.tar.gz

Download URL token_budget_py-0.1.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
41c699e9e48f666c280f9cee44cdb5cc16b6416c6f69ebceecf0e809be893e33
BLAKE2b-256 checksum
How to use checksums
f50a549513e9b5f15726144e030d0c94fbe1a2327d17a33de5d5e441d2d89a71
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / token_budget_py-0.1.0-py3-none-any.whl

Download URL token_budget_py-0.1.0-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a97a7255228c394d5f5c71afc34025991909efe86c34b2cee5758b086d865569
BLAKE2b-256 checksum
How to use checksums
82e74bae5903e0e4f134c09fb945128829cce7fa0f06fd7813c38e616784e6f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Security for token-budget-py 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page