|
| 1 | +""" |
| 2 | +pyccmeter — Python companion for ccmeter. |
| 3 | +
|
| 4 | +Wraps the ccmeter CLI so you can read your Claude Code spend analysis as |
| 5 | +ordinary Python dataclasses. Useful for piping into pandas, plotting in |
| 6 | +matplotlib, building per-team rollups, etc. |
| 7 | +
|
| 8 | +Why a wrapper instead of a re-port: |
| 9 | + - The TypeScript ccmeter is the source of truth (parser, pricing, rules). |
| 10 | + - Re-implementing all that in Python would mean two copies that drift. |
| 11 | + - This module shells out to `ccmeter export --format json` and parses |
| 12 | + the output. Stable schema (versioned via `schemaVersion`), zero copies. |
| 13 | +
|
| 14 | +Usage: |
| 15 | + from pyccmeter import load_analysis |
| 16 | + a = load_analysis(days=30) |
| 17 | + print(f"${a.totals.total_cost:.2f}") |
| 18 | + for s in sorted(a.sessions, key=lambda s: s.cost.total_cost, reverse=True)[:5]: |
| 19 | + print(s.id, s.project_path, s.cost.total_cost) |
| 20 | +
|
| 21 | +Requirements: Python 3.9+, ccmeter on PATH. |
| 22 | +""" |
| 23 | + |
| 24 | +from __future__ import annotations |
| 25 | + |
| 26 | +import json |
| 27 | +import shutil |
| 28 | +import subprocess |
| 29 | +from dataclasses import dataclass, field |
| 30 | +from typing import Any, Dict, List, Optional |
| 31 | + |
| 32 | + |
| 33 | +# --- public dataclasses ---------------------------------------------------- |
| 34 | + |
| 35 | + |
| 36 | +@dataclass |
| 37 | +class Cost: |
| 38 | + input_cost: float |
| 39 | + output_cost: float |
| 40 | + cache_write_cost: float |
| 41 | + cache_read_cost: float |
| 42 | + total_cost: float |
| 43 | + input_tokens: int |
| 44 | + output_tokens: int |
| 45 | + cache_write_tokens: int |
| 46 | + cache_read_tokens: int |
| 47 | + model: str |
| 48 | + cache_tier: str |
| 49 | + |
| 50 | + |
| 51 | +@dataclass |
| 52 | +class CacheBust: |
| 53 | + ts: int |
| 54 | + tier: str |
| 55 | + gap_seconds: float |
| 56 | + wasted_cost: float |
| 57 | + session_id: str |
| 58 | + |
| 59 | + |
| 60 | +@dataclass |
| 61 | +class Session: |
| 62 | + id: str |
| 63 | + project_path: str |
| 64 | + start_ms: int |
| 65 | + end_ms: int |
| 66 | + duration_ms: int |
| 67 | + primary_model: str |
| 68 | + turn_count: int |
| 69 | + tool_use_count: int |
| 70 | + cost: Cost |
| 71 | + cache_busts: List[CacheBust] |
| 72 | + shape: str |
| 73 | + tag: Optional[str] |
| 74 | + tool_calls: Dict[str, int] = field(default_factory=dict) |
| 75 | + tool_cost: Dict[str, float] = field(default_factory=dict) |
| 76 | + |
| 77 | + |
| 78 | +@dataclass |
| 79 | +class Totals: |
| 80 | + total_cost: float |
| 81 | + input_tokens: int |
| 82 | + output_tokens: int |
| 83 | + cache_read_tokens: int |
| 84 | + cache_write_tokens: int |
| 85 | + busts: int |
| 86 | + bust_cost: float |
| 87 | + cache_hit_ratio: float |
| 88 | + sessions: int |
| 89 | + turns: int |
| 90 | + |
| 91 | + |
| 92 | +@dataclass |
| 93 | +class Recommendation: |
| 94 | + id: str |
| 95 | + severity: str |
| 96 | + title: str |
| 97 | + body: str |
| 98 | + estimated_monthly_savings: float |
| 99 | + |
| 100 | + |
| 101 | +@dataclass |
| 102 | +class Analysis: |
| 103 | + schema_version: int |
| 104 | + generated_at: int |
| 105 | + range_start_ms: int |
| 106 | + range_end_ms: int |
| 107 | + sessions: List[Session] |
| 108 | + totals: Totals |
| 109 | + recommendations: List[Recommendation] |
| 110 | + |
| 111 | + |
| 112 | +# --- public API ------------------------------------------------------------ |
| 113 | + |
| 114 | + |
| 115 | +def load_analysis(days: int = 30, ccmeter_bin: str = "ccmeter") -> Analysis: |
| 116 | + """Run ccmeter export and parse it into Python objects. |
| 117 | +
|
| 118 | + Raises RuntimeError if ccmeter isn't installed or returns non-zero. |
| 119 | + """ |
| 120 | + if shutil.which(ccmeter_bin) is None: |
| 121 | + raise RuntimeError( |
| 122 | + f"could not find `{ccmeter_bin}` on PATH. " |
| 123 | + "Install with: npm i -g ccmeter (or use `npx ccmeter`)." |
| 124 | + ) |
| 125 | + proc = subprocess.run( |
| 126 | + [ccmeter_bin, "export", "--format", "json", "--days", str(days)], |
| 127 | + capture_output=True, text=True, check=False, |
| 128 | + ) |
| 129 | + if proc.returncode != 0: |
| 130 | + raise RuntimeError(f"ccmeter exited {proc.returncode}: {proc.stderr.strip()}") |
| 131 | + return _parse(json.loads(proc.stdout)) |
| 132 | + |
| 133 | + |
| 134 | +# --- internal: untyped JSON → typed dataclasses ---------------------------- |
| 135 | + |
| 136 | + |
| 137 | +def _parse(j: Dict[str, Any]) -> Analysis: |
| 138 | + return Analysis( |
| 139 | + schema_version=int(j.get("schemaVersion", 1)), |
| 140 | + generated_at=int(j.get("generatedAt", 0)), |
| 141 | + range_start_ms=int(j.get("rangeStartMs", 0)), |
| 142 | + range_end_ms=int(j.get("rangeEndMs", 0)), |
| 143 | + sessions=[_session(s) for s in j.get("sessions", [])], |
| 144 | + totals=_totals(j.get("totals", {})), |
| 145 | + recommendations=[_rec(r) for r in j.get("recommendations", [])], |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +def _session(s: Dict[str, Any]) -> Session: |
| 150 | + return Session( |
| 151 | + id=s["id"], |
| 152 | + project_path=s.get("projectPath", ""), |
| 153 | + start_ms=int(s.get("startMs", 0)), |
| 154 | + end_ms=int(s.get("endMs", 0)), |
| 155 | + duration_ms=int(s.get("durationMs", 0)), |
| 156 | + primary_model=s.get("primaryModel", ""), |
| 157 | + turn_count=int(s.get("turnCount", 0)), |
| 158 | + tool_use_count=int(s.get("toolUseCount", 0)), |
| 159 | + cost=_cost(s.get("cost", {})), |
| 160 | + cache_busts=[_bust(b) for b in s.get("cacheBusts", [])], |
| 161 | + shape=s.get("shape", ""), |
| 162 | + tag=s.get("tag"), |
| 163 | + tool_calls=dict(s.get("toolCalls", {})), |
| 164 | + tool_cost={k: float(v) for k, v in s.get("toolCost", {}).items()}, |
| 165 | + ) |
| 166 | + |
| 167 | + |
| 168 | +def _cost(c: Dict[str, Any]) -> Cost: |
| 169 | + return Cost( |
| 170 | + input_cost=float(c.get("inputCost", 0)), |
| 171 | + output_cost=float(c.get("outputCost", 0)), |
| 172 | + cache_write_cost=float(c.get("cacheWriteCost", 0)), |
| 173 | + cache_read_cost=float(c.get("cacheReadCost", 0)), |
| 174 | + total_cost=float(c.get("totalCost", 0)), |
| 175 | + input_tokens=int(c.get("inputTokens", 0)), |
| 176 | + output_tokens=int(c.get("outputTokens", 0)), |
| 177 | + cache_write_tokens=int(c.get("cacheWriteTokens", 0)), |
| 178 | + cache_read_tokens=int(c.get("cacheReadTokens", 0)), |
| 179 | + model=c.get("model", ""), |
| 180 | + cache_tier=c.get("cacheTier", "none"), |
| 181 | + ) |
| 182 | + |
| 183 | + |
| 184 | +def _bust(b: Dict[str, Any]) -> CacheBust: |
| 185 | + return CacheBust( |
| 186 | + ts=int(b.get("ts", 0)), |
| 187 | + tier=b.get("tier", "5m"), |
| 188 | + gap_seconds=float(b.get("gapSeconds", 0)), |
| 189 | + wasted_cost=float(b.get("wastedCost", 0)), |
| 190 | + session_id=b.get("sessionId", ""), |
| 191 | + ) |
| 192 | + |
| 193 | + |
| 194 | +def _totals(t: Dict[str, Any]) -> Totals: |
| 195 | + return Totals( |
| 196 | + total_cost=float(t.get("totalCost", 0)), |
| 197 | + input_tokens=int(t.get("inputTokens", 0)), |
| 198 | + output_tokens=int(t.get("outputTokens", 0)), |
| 199 | + cache_read_tokens=int(t.get("cacheReadTokens", 0)), |
| 200 | + cache_write_tokens=int(t.get("cacheWriteTokens", 0)), |
| 201 | + busts=int(t.get("busts", 0)), |
| 202 | + bust_cost=float(t.get("bustCost", 0)), |
| 203 | + cache_hit_ratio=float(t.get("cacheHitRatio", 0)), |
| 204 | + sessions=int(t.get("sessions", 0)), |
| 205 | + turns=int(t.get("turns", 0)), |
| 206 | + ) |
| 207 | + |
| 208 | + |
| 209 | +def _rec(r: Dict[str, Any]) -> Recommendation: |
| 210 | + return Recommendation( |
| 211 | + id=r.get("id", ""), |
| 212 | + severity=r.get("severity", "info"), |
| 213 | + title=r.get("title", ""), |
| 214 | + body=r.get("body", ""), |
| 215 | + estimated_monthly_savings=float(r.get("estimatedMonthlySavings", 0)), |
| 216 | + ) |
| 217 | + |
| 218 | + |
| 219 | +if __name__ == "__main__": |
| 220 | + import sys |
| 221 | + days = int(sys.argv[1]) if len(sys.argv) > 1 else 30 |
| 222 | + a = load_analysis(days=days) |
| 223 | + print(f"ccmeter — last {days} days") |
| 224 | + print(f" total spend: ${a.totals.total_cost:.2f}") |
| 225 | + print(f" sessions: {a.totals.sessions}") |
| 226 | + print(f" cache hit rate: {a.totals.cache_hit_ratio * 100:.1f}%") |
| 227 | + print(f" busts: {a.totals.busts} (wasted ${a.totals.bust_cost:.2f})") |
| 228 | + if a.recommendations: |
| 229 | + print("\n top recommendation:") |
| 230 | + r = a.recommendations[0] |
| 231 | + print(f" [{r.severity}] {r.title} (~${r.estimated_monthly_savings:.0f}/mo)") |
0 commit comments