Estimate LLM request cost and enforce per-request or per-session budgets. Python port of @mukundakatta/llm-cost-guard.
Project description
llm-cost-guard
Estimate LLM call cost and enforce per-request or per-session budgets. Built-in price table for the major frontier models, override or extend at runtime. Zero runtime dependencies.
Python port of @mukundakatta/llm-cost-guard.
Install
pip install llm-cost-guard-py
Usage
from llm_cost_guard import Budget, BudgetExceeded, estimate
# 1. Estimate the cost of a call you're about to make.
cost = estimate(model="gpt-5", input_tokens=1200, output_tokens=400)
print(cost.usd) # Decimal('0.0120')
print(cost.input_cost) # Decimal('0.0060')
print(cost.total_tokens) # 1600
# 2. Per-request and per-session budgets.
budget = Budget(
max_usd_per_request="0.10",
max_usd_per_session="5.00",
mode="throw", # "check" returns Decision instead of raising
)
try:
decision = budget.check(cost)
if decision.allowed:
# ... call your model here ...
budget.record(cost)
except BudgetExceeded as err:
print("blocked:", err.decision.reason)
Don't have token counts yet? Pass raw text and a heuristic estimator runs:
estimate(model="claude-sonnet-4-5", input="long prompt here", output="model response")
Override or extend the price table
from llm_cost_guard import PRICES, register_model
register_model("my-self-hosted-llama", input_per_1k="0.0001", output_per_1k="0.0002")
PRICES["gpt-5"]["input"] = "0.004" # bulk-discount tier
All amounts are decimal.Decimal for exact arithmetic; cast with float() when serializing.
API differences from the JS sibling
estimate()takes Python keyword args (model=,input_tokens=, ...) and returns aCostdataclass.enforce_budget()is preserved and accepts bothCostand the JS dict shape.Budgetadds session-level tracking and amode='throw'path -- not present in the JS package.
See the JS sibling's README for the broader design notes.
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 llm_cost_guard_py-0.1.0.tar.gz.
File metadata
- Download URL: llm_cost_guard_py-0.1.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13309a22033361c1662a9e5cbf558ee79147ea4c5ab1144d201a40bfbd103909
|
|
| MD5 |
9b8715fb780037ae82f062cdff4b11c3
|
|
| BLAKE2b-256 |
6601f51537007608f2486a65f781c791377f6e67f1dac613a596d8f060d27ee3
|
File details
Details for the file llm_cost_guard_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: llm_cost_guard_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
669def81f752c6c1b5d2ee82acfc6bcb80b435c1fe82eea7c3a860c0aebd20a0
|
|
| MD5 |
2ea7071ef9377d9d50013b3d7c98ab1b
|
|
| BLAKE2b-256 |
f5ca09aba84b3c647446d4675973af6aba55928580da3ccb0d5b167883d15e27
|