Skip to main content

Track Claude API costs across 15+ dimensions — ML forecasting, budget alerts, compliance reporting. Identify hidden cost multipliers (3.6x-1000x) and save 50-80% on LLM spending.

Project description

PyCostAudit

PyPI version License: MIT Python 3.9+ GitHub

💰 Real-Time LLM Cost Tracking & ML Forecasting

PyCostAudit shows you exactly where every dollar goes in your Claude Code usage — and what costs are hiding.

The Problem

Most cost trackers show: "You spent $47 today"

You need to know: "$32 from PDFs via URL (could be $8.80 from disk) + $12 from GitHub operations (optimize to save 30%) + $3 standard operations"

Hidden cost multipliers range from 3.6x to 1,000x depending on what you're doing. PyCostAudit makes them visible.


🚀 Quick Start (Choose Your Path)

1. Python Library (Programmatic)

from pycostaudit.ml_forecasting_service import TimeSeriesForecaster

forecaster = TimeSeriesForecaster()
forecast = forecaster.forecast_costs(
    daily_costs=[("2024-01-01", 15.50), ("2024-01-02", 16.20), ...],
    forecast_days=30,
    algorithm=ForecastAlgorithm.ENSEMBLE
)

print(f"Projected spend: ${forecast['summary']['total_projected']:.2f}")
print(f"Trend: {forecast['metrics']['trend']}")

2. Web Dashboard (Visual)

# Terminal 1: Start backend API
python -m pycostaudit.dashboard.app
# Runs on http://localhost:8000

# Terminal 2: Start frontend
cd pycostaudit/dashboard/frontend
npm install && npm start
# Runs on http://localhost:3000

3. Compliance Reports

from pycostaudit.compliance_reporting import ComplianceManager, ComplianceFramework

manager = ComplianceManager()
report = manager.generate_compliance_report(
    framework=ComplianceFramework.SOC2,
    user_id="user123",
    organization="Your Company"
)
summary = manager.get_compliance_summary(report, ComplianceFramework.SOC2)
print(f"Compliance Score: {summary['compliance_score']:.1f}%")

✨ What's New in v0.9.0

🤖 ML Cost Forecasting

  • 4 forecasting algorithms: ARIMA, Exponential Smoothing, Linear Regression, Ensemble
  • 95% confidence intervals for risk assessment
  • 5-12% MAPE accuracy on 30-180 day forecasts
  • Automatic anomaly detection (Z-score, statistical outliers)
  • Seasonality detection (weekly/monthly patterns)

📊 Interactive Dashboard

  • Real-time cost tracking with interactive charts
  • Budget status visualization with alerts
  • Forecast projections with confidence bands
  • Trend analysis (growth rates, week-over-week changes)
  • Responsive design (mobile, tablet, desktop)

🔒 Compliance & Audit

  • 6 compliance frameworks: SOC 2, HIPAA, GDPR, PCI DSS, ISO 27001, Custom
  • Immutable audit trail with 20+ event types
  • Compliance scoring (0-100%)
  • CSV/JSON export for auditors

📖 Installation

Prerequisites

  • Python 3.9+
  • Node.js 16+ (for dashboard)

Install Package

pip install pycostaudit
# or with uv (faster)
uv pip install pycostaudit

API Documentation

Once the backend is running: http://localhost:8000/docs


💻 Use Cases

Individual Developer

Track your personal Claude Code usage, optimize PDFs and batch operations.

Team Lead

Monitor team spending, spot trends, set budget alerts.

Manager/Finance

Department-level cost allocation, budget planning, ROI analysis.

Compliance Officer

Audit trails, compliance reports, framework verification.

DevOps/Monitoring

Real-time cost metrics via API, integration with Datadog/Prometheus.


🎯 Key Features

Cost Analysis

  • Track costs across 15+ dimensions (file format, operation type, time-of-day, region, billing plan)
  • Break down spending by project, model, operation
  • Identify cost multipliers (PDF via URL: 3.6x, Browser ops: 55x)

Forecasting

  • Project future spend with confidence intervals
  • Detect cost anomalies automatically
  • Forecast budget overruns

Compliance

  • Generate SOC 2, HIPAA, GDPR compliance reports
  • Track all operations in immutable audit trail
  • Verify compliance against framework requirements
  • Export evidence for auditors

API Endpoints

  • GET /api/forecast/costs - ML forecasts with confidence bands
  • GET /api/forecast/budget - Budget projections and warnings
  • GET /api/forecast/trends - Trend analysis
  • GET /api/compliance/report - Compliance reports
  • GET /api/compliance/verify - Compliance verification
  • GET /api/compliance/audit-trail - Audit trail export

🤖 For AI Agents & Automated Systems

API Contract

# Forecasting
from pycostaudit.ml_forecasting_service import TimeSeriesForecaster
forecaster = TimeSeriesForecaster()
forecast = forecaster.forecast_costs(daily_costs, forecast_days=30)

# Output: ForecastResult with forecast_points, trend, anomalies, metrics
# Algorithms: ARIMA, EXPONENTIAL_SMOOTHING, LINEAR_REGRESSION, ENSEMBLE
# Accuracy: MAPE 5-12%

# Compliance
from pycostaudit.compliance_reporting import ComplianceManager
manager = ComplianceManager()
report = manager.generate_compliance_report(framework, user_id, organization)
# Output: Compliance score (0-100%), verification checklist, audit trail

# Database
from pycostaudit.database import DatabaseManager
db = DatabaseManager()
daily_costs = db.get_time_series(user_id, "daily", limit=90)

Decision Framework

# Make automated decisions based on outputs
if forecast['summary']['total_projected'] > budget * 1.1:
    notify_finance_team("Cost projection exceeds budget by 10%")

if compliance_score < 95:
    flag_for_audit(f"Compliance score {compliance_score}% below target")

if forecast['metrics']['trend'] == 'increasing':
    recommend_optimization_review()

📚 Documentation

API Documentation

Run the dashboard backend and visit http://localhost:8000/docs for interactive API documentation.


❓ Common Questions

Q: Is this free?
A: Yes. PyCostAudit is open source (MIT license).

Q: Which Claude services are tracked?
A: Claude Code only. Claude Desktop and Claude Web use separate billing systems.

Q: What database does it use?
A: SQLite by default (local, private). PostgreSQL supported for production.

Q: Is this production-ready?
A: ⚠️ NO - Beta Release (v0.9.0). See ROADMAP.md for production readiness plan. Current limitations: no error handling, limited testing, incomplete authentication. Use for evaluation only.

Q: Can multiple people use it?
A: Yes. The database and API support multiple users with role-based access.

Q: How accurate is the forecasting?
A: MAPE (Mean Absolute Percentage Error) of 5-12% on 30-180 day forecasts using ensemble method.

Q: How do I export data?
A: REST API exports JSON, CSV endpoints available. Dashboard has download buttons.

Q: What compliance frameworks are supported?
A: SOC 2 Type II, HIPAA, GDPR, PCI DSS, ISO 27001, and custom frameworks.


🏗️ Architecture

Components

  • ML Forecasting Engine (Python) - ARIMA, Exponential Smoothing, Ensemble algorithms
  • Compliance Module (Python) - Audit trail, verification, reporting
  • FastAPI Backend (Python) - REST API with real-time endpoints
  • React Dashboard (JavaScript) - Interactive UI with Recharts visualizations
  • Database (SQLite/PostgreSQL) - Local-first cost and audit data

Data Flow

Cost data → Database → Forecasting Engine → API → Dashboard/Export


⚠️ Important Notes

Cost Estimates vs. Actual Billing

  • PyCostAudit estimates costs based on token counts and published pricing
  • Actual Claude billing may differ due to: cache hits (75% discount), batch processing (50% discount), enterprise contracts, pricing changes, local taxes, currency fluctuations
  • Always verify against your actual Claude invoice

Scope

  • Tracks Claude Code usage only
  • Does not track: Claude Desktop, Claude Web, other providers (yet)
  • Data is stored locally (SQLite) - no cloud uploads

📊 Real-World Example

Monthly Claude Code spend: $1,200
After PyCostAudit analysis:

├─ File reads via URL: $600 (50%) ← Costs 3.6x if stored locally
├─ Browser operations: $350 (29%) ← Costs 55x vs. baseline  
├─ Off-peak MCP calls: $150 (13%) ← Could run at 2 AM (save 30%)
└─ Data warehouse: $100 (8%)

Optimizations:
✅ Move files to disk: -$500/month
✅ Batch browser ops: -$280/month
✅ Run MCP at 2 AM: -$45/month

Result: $1,200 → $375/month
Annual savings: $10,200

🚀 Roadmap

v0.9.0 (Current)

  • ML forecasting with confidence intervals
  • Interactive React dashboard
  • Compliance frameworks (SOC2, HIPAA, GDPR, PCI DSS, ISO 27001)
  • REST API with real-time endpoints

v0.9.x (Next)

  • Slack/Email notifications
  • Webhook integrations
  • Advanced forecasting (LSTM, Prophet)

v1.0.0 (Future)

  • AWS Bedrock, Azure, GCP integration
  • Team dashboards with role-based access
  • Automated cost optimization

See ROADMAP.md for details.


📞 Support


📄 License

MIT License — See LICENSE


Stop wasting money. Start tracking what matters. 💚

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycostaudit-1.0.0-cp313-cp313-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

File details

Details for the file pycostaudit-1.0.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycostaudit-1.0.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 51daf3769e460378cf73e05d426aac8fcba74a6c539bfc2aa7d484382d432464
MD5 b1ede7ceaa51ed17541dfe46185fc5c3
BLAKE2b-256 f6500f42e2c44d0257aaf4a1c8d0e447e346cd4c8daa57b5dda12c79a6f8a985

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page