CostSentinel
AI cost governance middleware — budget enforcement, attribution, and reporting for LLM API calls.
CostSentinel sits between your application and LLM providers, tracking every token spent, enforcing budget policies, and attributing costs to teams, users, and endpoints. Zero external dependencies beyond PyYAML.
Features
- Token Cost Tracking — Automatic cost calculation for Claude, Titan, and custom models
- Budget Enforcement — Daily/monthly limits with configurable actions (block, downgrade, alert)
- Cost Attribution — Track spending by team, user, endpoint, and model
- Middleware Pattern — Decorator-based interception or manual tracking
- Reporting — CLI and programmatic cost breakdowns
- Zero Infrastructure — JSON file storage for development; DynamoDB-ready for production
Installation
pip install substrai-costsentinel
For AWS integration (DynamoDB state backend):
pip install substrai-costsentinel[aws]
For development:
pip install substrai-costsentinel[dev]
Quickstart
1. Initialize Configuration
costsentinel init
This creates costsentinel.yaml with default policies:
project_name: my-project
pricing:
claude-3.5-sonnet:
input: 0.003
output: 0.015
claude-3-haiku:
input: 0.00025
output: 0.00125
policies:
global:
limit_daily: 100.0
limit_monthly: 2000.0
on_exceed: block
team:
limit_daily: 25.0
on_exceed: downgrade
user:
limit_daily: 5.0
on_exceed: block
max_cost_per_request: 0.50
2. Add Middleware to Your Code
from costsentinel import CostMiddleware
middleware = CostMiddleware("costsentinel.yaml")
@middleware.intercept(model="claude-3-haiku", user_id="user-1", team_id="engineering")
def call_llm(prompt):
# Your LLM call here
response = my_llm_client.complete(prompt)
return response.text, response.input_tokens, response.output_tokens
# Returns CallResult with cost info
result = call_llm("Summarize this document...")
print(f"Cost: ${result.cost:.4f}, Remaining: ${result.budget_remaining:.2f}")
3. Manual Tracking
from costsentinel import CostMiddleware
middleware = CostMiddleware("costsentinel.yaml")
# After an LLM call completes
result = middleware.track_call(
model="claude-3.5-sonnet",
input_tokens=1500,
output_tokens=800,
metadata={"user_id": "user-1", "team_id": "engineering", "endpoint": "/api/chat"}
)
4. Check Reports
costsentinel report --today
costsentinel budget status
Architecture
┌─────────────────────────────────────────────────┐
│ Your Application │
├─────────────────────────────────────────────────┤
│ CostSentinel Middleware │
│ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │
│ │ Pricing │ │ Budget │ │ Attribution │ │
│ │ Engine │ │ Enforcer │ │ Store │ │
│ └──────────┘ └──────────┘ └──────────────┘ │
│ ┌──────────────────────────────────────────┐ │
│ │ State Management │ │
│ │ (JSON file / DynamoDB backend) │ │
│ └──────────────────────────────────────────┘ │
├─────────────────────────────────────────────────┤
│ LLM Provider (Bedrock) │
└─────────────────────────────────────────────────┘
CLI Commands
| Command | Description |
|---|---|
costsentinel init |
Create default configuration |
costsentinel report --today |
Show today's cost breakdown |
costsentinel budget status |
Display budget utilization |
costsentinel budget reset --scope user --id user-1 |
Reset a budget counter |
costsentinel validate |
Validate configuration file |
costsentinel status |
Show overall status |
Budget Policies
Policies are evaluated from most specific to least specific:
- User — Per-user daily/monthly limits
- Endpoint — Per-API-endpoint limits
- Team — Per-team limits
- Global — Organization-wide limits
Actions when budget is exceeded:
block— Reject the request withBudgetExceededErrordowngrade— Signal to use a cheaper modelalert— Allow but emit a warning
Development
git clone https://github.com/substrai/costsentinel.git
cd costsentinel
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest
License
MIT — Copyright (c) 2024 Gaurav Kumar Sinha
Release files for substrai-costsentinel 1.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| substrai_costsentinel-1.4.0.tar.gz | 85.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| substrai_costsentinel-1.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 158.6 kB
Release files / substrai_costsentinel-1.4.0.tar.gz
| Download URL | substrai_costsentinel-1.4.0.tar.gz |
|---|---|
| Size | 85.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3ac9765f85944853df9f9aa386fd4d95efe4c931531d404ebef5982be7d9e613
|
|
BLAKE2b-256 checksum How to use checksums |
91e56c3d4f320bd14016d650c94f42f251858cc799ce305b0f3d62f2dd7ffcec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.6
|
Release files / substrai_costsentinel-1.4.0-py3-none-any.whl
| Download URL | substrai_costsentinel-1.4.0-py3-none-any.whl |
|---|---|
| Size | 73.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
14a854915301cdcf33a0b2341693f1c9243feef5009d30ad290a50223ccbcd88
|
|
BLAKE2b-256 checksum How to use checksums |
e81aeff46662e7e8a16744bb8e42c629e9b08e4c4bade17beea40479c42a1bc8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.6
|