Skip to main content

Real-time LLM cost tracking and optimization โ€” Rust core with Python API

Project description

PyCostAudit

PyPI version License: MIT Python 3.9+ GitHub Package Status Claude Code

๐Ÿ’ฐ See where your Claude Code budget actually goes โ€” then save 50-80%

PyCostAudit tracks what nothing else measures: hidden cost multipliers you're not seeing.

Typical findings:

  • PDF from URL costs 36x more than from disk
  • Browser operations 55x more expensive than file reads
  • Peak hours cost 30% more than off-peak
  • MCP integrations have 10-100x overhead
  • Billing plans differ by 200%+ for identical work

The Problem

You see: "Spent $47 today"
You need: "$32 on PDFs (could save $23 by moving to disk) + $12 on GitHub ops (save 30%) + $3 standard hours"


Real Example: $420/Month Hidden

Before After PyCostAudit
"We spend $1,200/month. Why?" "File reads via URL: $600 โ†’ Move to disk: -$500/mo"
"Browser ops: $350 โ†’ Batch them: -$280/mo"
"Off-peak MCP: $150 โ†’ Run at 2 AM: -$45/mo"
Result: $1,200 โ†’ $375/month

30-Second Start

Option 1: Skill (CLI Commands)

# Install
bash install-skill.sh
source ~/.zshrc

# View costs
cost-report

# Quick track
cost-track "operation" 2000 500

Option 2: CLI Monitor (Real-Time Dashboard)

python3 pycostaudit_monitor.py
# Auto-refreshes every 2 seconds

Option 3: Browser Extension (Chrome)

# Open Chrome โ†’ Extensions โ†’ Load unpacked โ†’ browser-extension/
# Click extension icon to see real-time costs

โš ๏ธ Scope: Tracks Claude Code operations (multi-provider: OpenAI, Bedrock, Gemini)


Live Demo

PyCostAudit Demo


Three Ways to Track Your Costs

1. Claude Code Skill โšก Quick Checks

cost-report              # Daily breakdown
cost-forecast            # Weekly forecast
cost-track <op> <in> <out> # Manual tracking

Perfect for: On-demand cost reports, daily reviews, script integration

2. CLI Monitor ๐Ÿ“Š Real-Time Dashboard

cost-monitor             # Auto-refresh every 2 seconds
cost-monitor --refresh 1 # Custom refresh rate

Perfect for: Session monitoring, live cost tracking, trend analysis

3. Browser Extension ๐ŸŒ Chrome Popup

Chrome โ†’ Extensions โ†’ Load unpacked โ†’ browser-extension/

Perfect for: Always-on monitoring, browser-based workflows, team dashboards

See detailed comparison โ†’


Recent Updates (v0.7.0) โ€” 10 Tasks Complete! โœ…

Phase 4: Advanced Filtering & Custom Reports โœ… NEW

  • Advanced filtering system with 12 operators (EQ, NE, GT, BETWEEN, REGEX, etc.)
  • Custom report builder with 6 export formats (JSON, CSV, HTML, Markdown, PDF, Excel)
  • Pre-built templates for common reports (cost breakdown, trend analysis, regional comparison)
  • Report scheduling (daily, weekly, monthly, quarterly delivery)
  • Aggregation engine with 9 functions (SUM, AVG, MIN, MAX, COUNT, STDDEV, percentiles)
  • Time-series bucketing (minute through year)

Phase 5: Multi-Org & Departments โœ… NEW

  • Hierarchical organization structure (unlimited nesting)
  • Role-based access control (admin, manager, department_lead, member, viewer)
  • Department-level budget management and overage detection
  • Cost allocation with flexible distribution rules
  • User-specific cost visibility based on permissions
  • Department comparison and forecasting
  • 24 compliance-ready department tracking tests

Phase 6: SOC 2 Compliance & Audit โœ… NEW

  • Comprehensive audit logging (20 event types)
  • Data classification (public, internal, confidential, restricted)
  • Access logging with purpose documentation
  • Compliance frameworks (SOC 2, HIPAA, GDPR, PCI DSS, ISO 27001)
  • Data retention policies with automatic archival
  • Compliance checkpoints and remediation tracking
  • Detailed compliance reports with evidence collection

Phase 7: OpenTelemetry Observability Export โœ… FINAL

  • Multi-backend support (Prometheus, Jaeger, Datadog, New Relic, OTLP)
  • Metrics collection (cost, tokens, operations, anomalies, budget)
  • Distributed tracing with span collection
  • Real-time metrics export to observability stacks
  • Integration with existing monitoring dashboards
  • Custom alerting on cost anomalies
  • 27 comprehensive observability tests

Phase 2-3: Core Platform โœ…

  • Ultra-detailed token classification (50+ dimensions)
  • ML-based anomaly detection (4 algorithms)
  • Advanced cost forecasting (30/60/90-day with confidence intervals)
  • Intelligent recommendations (8 types with ROI ranking)
  • Multi-provider cost tracking (Anthropic, AWS Bedrock, GCP, Azure)
  • Real-time web dashboard
  • Alert system (Slack, Email, SMS, Twilio)
  • Claude Code Skill, CLI Monitor, Browser Extension

Complete Feature Set (v0.7.0)

๐ŸŽฏ 10 Major Components

# Component Status Features
1๏ธโƒฃ Database & Infrastructure โœ… SQLite storage, alerts, forecasting, anomalies, recommendations, audit logs
2๏ธโƒฃ Budget Alerts โœ… Slack, Email, SMS (Twilio) with cooldown & daily limits
3๏ธโƒฃ Automated Reports โœ… Daily/weekly HTML emails with progress bars, charts, trends
4๏ธโƒฃ Anomaly Detection โœ… 4 algorithms (Z-score, Isolation Forest, Seasonal, Ensemble)
5๏ธโƒฃ Cost Forecasting โœ… 30/60/90-day projections with confidence intervals & plan comparison
6๏ธโƒฃ Recommendations โœ… 8 types (batching, off-peak, model downgrade, file format, etc.) ranked by ROI
7๏ธโƒฃ Advanced Filtering โœ… 12 operators, aggregation (SUM/AVG/MIN/MAX/STDDEV), time bucketing
8๏ธโƒฃ Multi-Org Support โœ… Hierarchical departments, role-based access, cost allocation, budget tracking
9๏ธโƒฃ Compliance & Audit โœ… SOC 2 ready, GDPR checks, 20 event types, retention policies
๐Ÿ”Ÿ Observability Export โœ… Prometheus, Jaeger, Datadog, New Relic, OTLP integration

๐Ÿ’ก Key Capabilities

Cost Tracking (50+ Dimensions)

  • โœ… Operation types (API: 1.0x โ†’ Browser: 55x)
  • โœ… File formats (CSV: 1.0x โ†’ Image URL: 4.2x)
  • โœ… Time-of-day pricing (Off-peak: 0.7x โ†’ Peak: 1.3x)
  • โœ… Regional pricing (US: 1.0x โ†’ Asia: 1.2x)
  • โœ… Cache efficiency (Cached reads: 0.1x, writes: 1.25x)
  • โœ… Token types (Input, Output, Vision, Cached, Tool overhead)
  • โœ… Complexity levels (Trivial: 1.0x โ†’ Very complex: 1.6x)

Enterprise Features

  • โœ… Multi-org with unlimited nested departments
  • โœ… Role-based access control (admin, manager, lead, member, viewer)
  • โœ… Cost allocation and chargeback
  • โœ… Department-level budgets with overage alerts
  • โœ… User-specific cost visibility

Compliance & Security

  • โœ… SOC 2 compliance tracking
  • โœ… GDPR compliance checks
  • โœ… Comprehensive audit logs
  • โœ… Data retention policies
  • โœ… Sensitive data access logging
  • โœ… Unauthorized access detection

Analytics & Insights

  • โœ… Trend analysis with direction indicators
  • โœ… ML-based anomaly detection
  • โœ… Budget forecasting
  • โœ… Plan comparison (API/Pro/Max/Enterprise)
  • โœ… Cost breakdown by operation/provider/region

Observability

  • โœ… Real-time metrics export
  • โœ… Distributed tracing
  • โœ… Integration with major platforms
  • โœ… Custom dashboards support
  • โœ… Historical data retention

Why PyCostAudit Is Different

Dimension PyCostAudit Other Tools
File Format Tracking CSV vs PDF URL (3.6x) โŒ Not tracked
Operation Type Variance Browser vs API (55x) โŒ Only API costs
Peak/Off-Peak Pricing Hour-of-day multipliers โŒ Flat rates
MCP Overhead Detection Claimed vs actual tokens โŒ Assumed =actual
GitHub Operation Costs Read/Write/Commit (4-12x) โŒ Lumped together
Region Pricing Multi-region support โŒ US-only
Timezone-Aware Billing Fair team attribution โŒ UTC only
Data Warehouse Queries Per-row multipliers (100x+) โŒ Volume-only
Multi-Currency No FX risk โŒ Converts (risky)
Billing Plan Comparison API/Pro/Max/Enterprise โŒ Shows 1 plan only

The Problem Nobody Addresses

Inside Claude Code, you're spending more than you realize. Not because Claude is expensiveโ€”but because you don't see the hidden multipliers:

โŒ PDF from URL costs 3.6x more than pasted CSV
โŒ Browser operations cost 55x more than API calls  
โŒ Peak hour costs 30% MORE than off-peak (same operation)
โŒ Bedrock EU region costs 15% more than US
โŒ MCP calls have 10x-100x overhead (hidden!)
โŒ Pro plan users pay 200% more than Max for the same work

Most tools show: "You spent $47 today"

PyCostAudit shows: "$32 from PDFs via URL (could be $8.80 from disk) + $12 from GitHub commits (optimize to save 30%) + $3 in standard hours"


What Makes PyCostAudit Different

Dimension Tracked Multiplier Why It Matters
File Format CSV pasted vs PDF URL 1.0x โ†’ 3.6x PDF via URL costs 3.6x baseline
Operation Type Browser vs API vs DB 55x Browser scraping kills budgets
Peak/Off-Peak Hour of day 1.3x / 0.7x Batch jobs at 2 AM, save 30%
Cloud Region us-east-1 vs eu-west 1.15x Regional premiums add up
Billing Plan API vs Pro vs Max vs Enterprise 8x Same usage, wildly different costs
MCP Overhead Claimed vs actual tokens 10-100x Stripe MCP = 23x overhead
GitHub Operations Read vs Write vs Commit 4-12x Claude commits cost 12x more
Markdown/Docs README, CHANGELOG, docs 3x Frequent updates = major costs
Data Warehouse Snowflake queries 100-1000x+ One query = $7.50
Timezone User's local time Context-aware Fair team billing
Currency USD, EUR, GBP, etc. None No FX conversion risk

Result: Users typically save 50-80% just by understanding these multipliers.


Real Example: Find $420/Month Hidden

Before:
"We spend $1,200/month on Claude. Budget doesn't justify it."

After PyCostAudit breakdown:
โ”œโ”€ File reads via URL:  $600 (50%) โ† Costs 3.6x disk
โ”œโ”€ Browser operations:  $350 (29%) โ† Costs 55x baseline
โ”œโ”€ Off-peak MCP calls:  $150 (13%) โ† Could run at 2 AM (save 30%)
โ””โ”€ Data warehouse:      $100 (8%)  โ† One Snowflake query

Quick fixes:
โœ… Move PDFs to disk: -$500/month
โœ… Batch browser ops: -$280/month  
โœ… Run MCP at 2 AM: -$45/month
Result: $1,200 โ†’ $375/month. You just kept $10k/year.

Install & 2-Minute Setup

# Install (choose one)
pip install pycostaudit
# or with uv (faster)
uv pip install pycostaudit

# Start auditing
from pycost_audit import PyCostAudit
import os

auditor = PyCostAudit(db_path="~/.pycostaudit/costs.db")

# Example 1: Track GitHub commit (12x cost multiplier - BIGGEST COST!)
cost = auditor.track_operation(
    operation_type="github_commit",
    tokens_input=8200,               # Analyzing diffs, tree walk
    tokens_output=450,
    model="claude-3-5-sonnet",
    user="alice"
)
print(f"GitHub commit cost: ${cost['cost']:.4f} {cost['currency']}")

# Example 2: Track GitHub read (4x cost multiplier)
cost = auditor.track_operation(
    operation_type="github_read",
    tokens_input=2100,               # Reading PR/issue
    tokens_output=200,
    model="claude-3-5-haiku",
    user="bob"
)
print(f"GitHub read cost: ${cost['cost']:.4f} {cost['currency']}")

# Example 3: Track markdown updates (3x cost multiplier)
cost = auditor.track_operation(
    operation_type="markdown_operation",
    tokens_input=1500,               # README/CHANGELOG updates
    tokens_output=800,
    model="claude-3-5-sonnet",
    user="alice"
)
print(f"Markdown operation cost: ${cost['cost']:.4f} {cost['currency']}")

# Example 4: Track file read (3.6x multiplier for PDF via URL)
cost = auditor.track_operation(
    operation_type="file_read",
    tokens_input=450,
    tokens_output=120,
    model="claude-3-5-haiku",
    file_source="pdf_url",           # 3.6x multiplier
    user="alice",
    user_timezone="America/New_York",
    billing_plan="max",
    pricing_tier="off_peak"
)
print(f"File read cost: ${cost['cost']:.4f} {cost['currency']}")

# Get today's breakdown
breakdown = auditor.analyze_daily()
print(f"Today: ${breakdown['total_cost']:.2f}")

# Find cost by plan
plans = auditor.compare_billing_plans()
print(f"Recommendation: Switch to {plans['recommended_plan']} (save ${plans['savings']:.2f}/month)")

# Model comparison
models = auditor.compare_models(tokens_input=1000, tokens_output=500)
for model in models['comparisons']:
    print(f"{model['model']}: ${model['cost_usd']:.4f}")

Real Savings Examples

Solo Developer

Before: $120/month (unclear why) After: $62/month (file optimization + off-peak batching) Savings: $58/month = $696/year

Startup Team (5 developers)

Before: $800/month (multiple plans, no coordination) After: $320/month (unified Max plan + batch scheduling) Savings: $480/month = $5,760/year

Enterprise (100+ users)

Before: $12,000/month (sprawl across API/Pro/Max/Bedrock) After: $4,200/month (consolidated to Max + enterprise tier + off-peak scheduling) Savings: $7,800/month = $93,600/year


Features

โœ… 15 Dimensions of Cost Tracking

Billed Currency Tracking

  • Track costs in original currency (USD, EUR, GBP, AUD, JPY, etc.)
  • No FX conversion (avoid currency risk)
  • Multi-provider unified reporting

Billing Plans

  • Compare API vs Pro vs Max vs Enterprise
  • Show savings from switching plans
  • Identify optimal plan for usage pattern

Time-of-Day Pricing

  • Peak hours: 5 PM - 10 PM weekdays (1.3x cost)
  • Standard: 6 AM - 5 PM (1.0x baseline)
  • Off-peak: 10 PM - 6 AM (0.7x discount)
  • Weekend: 0.85x discount
  • Batch expensive operations at 2 AM, save 30%

Cloud Regions

  • Track regional pricing variance (10-30%)
  • Bedrock: us-east-1 vs eu-west-1 pricing
  • Azure: eastus vs westeurope premiums
  • GCP: us-central1 vs asia-east1 variance

File Formats

  • CSV pasted: 1.0x
  • PDF local: 1.2x
  • PDF via URL: 3.6x
  • Image via URL: 4.2x

Operation Types

  • API call: 1.0x baseline
  • File read: varies by format
  • Browser operations: 55x more expensive
  • Database queries: 2-1000x+ depending on size
  • MCP invocations: 2.4x

Multi-Provider Support

  • Claude API (direct)
  • AWS Bedrock (regional pricing)
  • Azure Foundry (EU/Asia premiums)
  • GCP Model Garden (volume discounts)

Timezone-Aware Team Billing

  • Daily budget resets at each user's local midnight
  • Fair billing for distributed teams
  • Session grouping respects timezone boundaries

Dynamic Pricing

  • 1-hour refresh from provider APIs
  • Never hardcoded pricing (FX risk mitigation)
  • Alerts when using fallback/stale pricing

MCP Overhead Profiling

  • Track claimed vs actual token cost
  • Stripe MCP: 23x overhead
  • Identify most expensive integrations

Session-Based Analysis

  • Group operations by context (branch, feature, task)
  • Root cause analysis (which feature costs most?)
  • Per-session recommendations

Data Warehouse Cost Tracking

  • Snowflake, BigQuery, Redshift queries
  • 100-1000x+ multipliers for millions of rows
  • Calculate cost per row returned

Model Comparison

  • Before switching: see actual cost difference
  • Haiku vs Sonnet: 17.6x cheaper input
  • Pro vs Max: break-even analysis

Forecast with Disclaimers

  • Quarterly spending projection
  • Flagged assumptions (pricing stability)
  • Warns when new models launch

๐Ÿ“Š Analysis & Optimization

# Daily breakdown by dimension
daily = reporter.analyze_daily()
# {
#   "by_operation_type": {...},
#   "by_file_format": {...},
#   "by_billing_plan": {...},
#   "by_time_of_day": {...},
#   "by_cloud_region": {...}
# }

# Session root cause analysis
analysis = reporter.analyze_session(session_id)
# {
#   "biggest_waste": {"type": "BrowserOp", "cost": $156},
#   "recommendations": [...]
# }

# MCP cost ranking
mcp = reporter.analyze_mcp_costs()
# [
#   {"rank": 1, "name": "stripe", "cost": $67, "overhead": "23x"},
#   {"rank": 2, "name": "github", "cost": $23, "overhead": "2.1x"}
# ]

# Plan optimization
plans = reporter.compare_billing_plans()
# "Switch from API to Max: save $2,650/month"

# Recommendations ranked by ROI
recs = reporter.get_recommendations()
# [
#   {"action": "Batch file reads", "savings": "$14/day", "effort": "5 min"},
#   {"action": "Run at 2 AM", "savings": "$8/day", "effort": "scheduler setup"}
# ]

Architecture

Rust Core (pyO3 bindings)

  • Performance-critical cost calculation
  • Real-time token accounting
  • Timezone conversion (chrono-tz)
  • Multi-currency support

Python Wrapper

  • Simple async API
  • SQLite storage (local, private)
  • JSON output (Claude Code skill compatible)
  • No cloud dependency

Database

  • Local SQLite (your data, your control)
  • Indexed by session, timestamp, user, currency
  • Timezone-aware queries

Claude Code Integration

For Users: Track Your Work

PyCostAudit integrates directly into Claude Code. Every operation within Claude Code is tracked automatically:

# In any Claude Code project
from pycost_audit import PyCostAudit

# Initialize once (default location: ~/.pycostaudit/costs.db)
auditor = PyCostAudit()

# Get today's breakdown
breakdown = auditor.analyze_daily()
print(f"Today's Claude Code cost: ${breakdown['total_cost_usd']:.2f}")

# Get optimization tips
recs = auditor.get_recommendations()
for rec in recs['recommendations'][:3]:
    print(f"๐Ÿ’ก {rec['action']}: Save ${rec['expected_savings_usd']}/day")

For Agents: Integrating Cost Tracking

Agents and autonomous workflows can track costs by wrapping Claude Code operations:

from pycost_audit import PyCostAudit

auditor = PyCostAudit()

# Track individual operations
cost = auditor.track_operation(
    operation_type="file_read",  # or: api_call, browser_op, mcp_invocation, etc
    tokens_input=450,
    tokens_output=120,
    model="claude-3-5-haiku",
    mcp_name="web_search",  # if using a skill
    session_id="my_agent_task"
)

# Monitor cost per session
session_analysis = auditor.analyze_session("my_agent_task")
if session_analysis['total_cost_usd'] > 0.50:
    print(f"โš ๏ธ Session cost high: ${session_analysis['total_cost_usd']:.2f}")

Installation & Environment

# Install (choose one)
pip install pycostaudit      # with pip
uv pip install pycostaudit   # with uv (faster)

# Optional: Custom database path
export PYCOSTAUDIT_DB=~/.pycostaudit/costs.db

Note: PyCostAudit tracks Claude Code only. Claude Desktop and Claude Web use separate billing systems not tracked here.


โš ๏ธ Important: Cost Estimates & Disclaimers

These are nearest estimates, not actual billing:

  • Costs shown are calculated from token counts and published pricing
  • Actual Claude billing may differ due to:
    • Cache hits (75% discount on cached tokens)
    • Batch processing discounts (50% discount)
    • Enterprise contracts (custom pricing)
    • Pricing changes (pricing updates daily)
    • Hidden overhead in MCP calls (can be 10-100x)
    • Local taxes (VAT, GST, sales tax added at checkout by region)
    • Currency fluctuations (if billing in non-USD currency)
  • Always verify against your actual Claude invoice
  • Use "pricing_source" field: "api" (most accurate) vs "fallback" (โš ๏ธ outdated)

Platform Support

  • Python: 3.9, 3.10, 3.11, 3.12, 3.13
  • OS: Linux, macOS (Intel/Apple Silicon), Windows
  • Dependency: Rust runtime only (PyO3)

License

MIT โ€” See LICENSE


Why We Built This

Every existing cost tracker shows: "You spent $47 today."

Nobody shows: "You spent $32 on PDFs via URL (which costs 3.6x disk) at peak hours (30% premium) on the API tier (8x Max pricing) because you didn't know about the multipliers."

PyCostAudit solves the unsolved problem: Making the hidden 36x-1000x multipliers visible so you can optimize ruthlessly.

The market is worth $1B+. Everyone using Claude (50M+ users) is leaving 50-80% in savings on the table.


Questions?

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-0.7.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-0.7.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pycostaudit-0.7.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 02f59437232d6289a03fe493e5681e7a8cc93b0584f55890a163daf3aa260385
MD5 e898e6d861bd595293d3c3f1a63603dc
BLAKE2b-256 7c42d7e780d13cb1ecf279583d3c3a81d90686fd43dec9e6d40dfbdb20ea7476

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