Skip to main content

llm-cost-guard

PyPI Python License: MIT

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 a Cost dataclass.
  • enforce_budget() is preserved and accepts both Cost and the JS dict shape.
  • Budget adds session-level tracking and a mode='throw' path -- not present in the JS package.

See the JS sibling's README for the broader design notes.

Metadata

Release files for llm-cost-guard-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 llm-cost-guard-py 0.1.0
File Size Uploaded Provenance Actions
llm_cost_guard_py-0.1.0.tar.gz 8.3 kB Without provenance Details for llm_cost_guard_py-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llm-cost-guard-py 0.1.0
File Interpreter ABI Platform Actions
llm_cost_guard_py-0.1.0-py3-none-any.whl Python 3 none any Details for llm_cost_guard_py-0.1.0-py3-none-any.whl

Total release size: 16.8 kB

Release files / llm_cost_guard_py-0.1.0.tar.gz

Download URL llm_cost_guard_py-0.1.0.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
13309a22033361c1662a9e5cbf558ee79147ea4c5ab1144d201a40bfbd103909
BLAKE2b-256 checksum
How to use checksums
6601f51537007608f2486a65f781c791377f6e67f1dac613a596d8f060d27ee3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

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

Download URL llm_cost_guard_py-0.1.0-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
669def81f752c6c1b5d2ee82acfc6bcb80b435c1fe82eea7c3a860c0aebd20a0
BLAKE2b-256 checksum
How to use checksums
f5ca09aba84b3c647446d4675973af6aba55928580da3ccb0d5b167883d15e27
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for llm-cost-guard-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