enforces budget
Project description
coreason-budget (The Controller)
Mission: Enforce Financial Operations (FinOps) guardrails for LLM usage.
This package acts as the "Controller," treating Compute (Tokens) as Cash. It enforces daily quotas and rejects requests immediately if limits are exceeded.
Features
- Atomic Counting: Uses Redis for high-speed, atomic, thread-safe counters.
- Hierarchical Quotas: Enforces limits at User, Project, and Global scopes.
- Fail Closed: Security-first design; if the budget cannot be checked, the transaction is blocked.
- Manual Integration: Designed to be integrated into your middleware with a "Check-then-Charge" lifecycle.
Installation
pip install coreason-budget
or with Poetry:
poetry add coreason-budget
Usage
The package exposes a BudgetManager that you integrate into your API flow.
1. Configuration
The system is configured via BudgetConfig or environment variables (COREASON_BUDGET_*).
from coreason_budget import BudgetManager, BudgetConfig, BudgetExceededError
# Initialize with Redis URL and Limits
config = BudgetConfig(
redis_url="redis://localhost:6379",
daily_user_limit_usd=10.0,
daily_project_limit_usd=500.0,
daily_global_limit_usd=5000.0
)
budget = BudgetManager(config)
2. The Check-Charge Lifecycle
The middleware operates in two phases: Pre-Flight Check and Post-Flight Charge.
# --- Phase 1: Pre-Flight Check ---
# Before calling the LLM, verify budget availability.
user_id = "user_123"
try:
# Check if user can spend (optionally pass estimated_cost)
await budget.check_availability(user_id)
except BudgetExceededError:
# Block the request immediately
return Response("Daily Limit Reached", status_code=429)
# --- Execute LLM ---
# Perform the inference call
response = await llm.generate(...)
# --- Phase 2: Post-Flight Charge ---
# Calculate precise cost based on provider metadata
cost = budget.pricing.calculate(
model="gpt-4",
input_tokens=response.usage.prompt_tokens,
output_tokens=response.usage.completion_tokens
)
# Atomically record the spend
await budget.record_spend(
user_id=user_id,
cost=cost,
project_id="project_launch_sim", # Optional
model="gpt-4" # Optional, for logging
)
print(f"Transaction Cost: ${cost}")
Configuration Options
| Environment Variable | Description | Default |
|---|---|---|
COREASON_BUDGET_REDIS_URL |
Redis Connection URL | Required |
COREASON_BUDGET_DAILY_USER_LIMIT_USD |
Daily limit per user ($) | 10.0 |
COREASON_BUDGET_DAILY_PROJECT_LIMIT_USD |
Daily limit per project ($) | 500.0 |
COREASON_BUDGET_DAILY_GLOBAL_LIMIT_USD |
Global hard limit ($) | 5000.0 |
COREASON_BUDGET_LOG_PATH |
Path to log file | logs/app.log |
Architecture
- RedisLedger: Manages atomic increments and key expiration (UTC Midnight).
- BudgetGuard: Enforces limits and raises
BudgetExceededError. - PricingEngine: Calculates costs using
liteLLMor configured overrides.
Development
-
Install Dependencies:
poetry install -
Run Tests:
poetry run pytest
-
Code Quality:
poetry run pre-commit run --all-files
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 coreason_budget-0.2.0.tar.gz.
File metadata
- Download URL: coreason_budget-0.2.0.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc42e9ba3b34baf59d4cd3a9d818dc5a6c0abfd252e909898e5730265d36899e
|
|
| MD5 |
71e80a97c2d0c9d704dc4bf3d64df525
|
|
| BLAKE2b-256 |
59e964f826708429de725c0c69d36fd1e52d74b0c724501cfaea05b958db5d92
|
Provenance
The following attestation bundles were made for coreason_budget-0.2.0.tar.gz:
Publisher:
publish.yml on CoReason-AI/coreason-budget
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
coreason_budget-0.2.0.tar.gz -
Subject digest:
dc42e9ba3b34baf59d4cd3a9d818dc5a6c0abfd252e909898e5730265d36899e - Sigstore transparency entry: 808307369
- Sigstore integration time:
-
Permalink:
CoReason-AI/coreason-budget@b7fb1dfca6bdf08698a9b66764561fa268fab55f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/CoReason-AI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b7fb1dfca6bdf08698a9b66764561fa268fab55f -
Trigger Event:
release
-
Statement type:
File details
Details for the file coreason_budget-0.2.0-py3-none-any.whl.
File metadata
- Download URL: coreason_budget-0.2.0-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5b17b7c0a63b0f65cf4323d1a7b7e0d1ceaf2e272fa4fa11e2a096f023779f4
|
|
| MD5 |
c051d605dad924155ee3a13f0f795346
|
|
| BLAKE2b-256 |
afb96f45be81de377c85252fed5a6f5209c2f6721f8f6b741e43747570bac45c
|
Provenance
The following attestation bundles were made for coreason_budget-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on CoReason-AI/coreason-budget
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
coreason_budget-0.2.0-py3-none-any.whl -
Subject digest:
c5b17b7c0a63b0f65cf4323d1a7b7e0d1ceaf2e272fa4fa11e2a096f023779f4 - Sigstore transparency entry: 808307372
- Sigstore integration time:
-
Permalink:
CoReason-AI/coreason-budget@b7fb1dfca6bdf08698a9b66764561fa268fab55f -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/CoReason-AI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b7fb1dfca6bdf08698a9b66764561fa268fab55f -
Trigger Event:
release
-
Statement type: