|
| 1 | +# CCMonitor |
| 2 | + |
| 3 | +A native macOS menu bar app for real-time monitoring of [Claude Code](https://docs.anthropic.com/en/docs/claude-code) token usage and costs. |
| 4 | + |
| 5 | +<p align="center"> |
| 6 | + <img src="https://img.shields.io/badge/platform-macOS%2014%2B-blue" alt="Platform"> |
| 7 | + <img src="https://img.shields.io/badge/swift-5.10-orange" alt="Swift"> |
| 8 | + <img src="https://img.shields.io/badge/license-MIT-green" alt="License"> |
| 9 | +</p> |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- **Real-time Monitoring** — Watches `~/.claude/projects/**/*.jsonl` via FSEvents, processes new entries within seconds |
| 14 | +- **Accurate Pricing** — Fetches model pricing from LiteLLM database with 3-tier cache (remote → local → embedded fallback) |
| 15 | +- **Incremental Processing** — Tracks byte offsets per file; restarts pick up exactly where they left off |
| 16 | +- **Rich Dashboard** — Swift Charts with bar/line toggle, time range selection (minutes/hours/days), click-to-inspect data points |
| 17 | +- **Menu Bar at a Glance** — Configurable display: cost only, cost + tokens, or icon only |
| 18 | +- **Multi-dimensional Aggregation** — By time (minute/hour/day), by project, by model, by session |
| 19 | +- **Burn Rate & Forecast** — Real-time $/hr rate with daily and monthly cost projections |
| 20 | +- **Budget Alerts** — Configurable daily/monthly budgets with visual progress indicators |
| 21 | +- **Instant Startup** — Aggregation snapshots restore cached data in under a second |
| 22 | + |
| 23 | +## Requirements |
| 24 | + |
| 25 | +- macOS 14.0 (Sonoma) or later |
| 26 | +- [Claude Code](https://docs.anthropic.com/en/docs/claude-code) installed and used (generates the JSONL logs this app monitors) |
| 27 | + |
| 28 | +## Installation |
| 29 | + |
| 30 | +### Download Release |
| 31 | + |
| 32 | +Download the latest `CCMonitor.app.zip` from [Releases](https://github.com/AgentsMesh/CCMonitor/releases), unzip, and move to `/Applications/`. |
| 33 | + |
| 34 | +### Build from Source |
| 35 | + |
| 36 | +```bash |
| 37 | +git clone https://github.com/AgentsMesh/CCMonitor.git |
| 38 | +cd CCMonitor |
| 39 | + |
| 40 | +# Build release .app bundle |
| 41 | +./scripts/build-app.sh release |
| 42 | + |
| 43 | +# Run directly |
| 44 | +open build/CCMonitor.app |
| 45 | + |
| 46 | +# Or install to /Applications |
| 47 | +cp -R build/CCMonitor.app /Applications/ |
| 48 | +``` |
| 49 | + |
| 50 | +> **Note:** `SMAppService` requires the app to be in `/Applications` for Launch at Login to work. |
| 51 | +
|
| 52 | +## Architecture |
| 53 | + |
| 54 | +``` |
| 55 | +FSEvents (directory watch, 0.5s latency) |
| 56 | + │ |
| 57 | + ▼ |
| 58 | +IncrementalFileReader (byte offset tracking, persistent state) |
| 59 | + │ [String] new lines |
| 60 | + ▼ |
| 61 | +JSONLParser (filter type=assistant + message.usage, dedup by hash) |
| 62 | + │ [UsageEntry] |
| 63 | + ▼ |
| 64 | +CostCalculator + PricingService (token × price, 200K tiered pricing) |
| 65 | + │ UsageEntry + cost |
| 66 | + ▼ |
| 67 | +UsageAggregator (@Observable, incremental multi-dimension aggregation) |
| 68 | + │ automatic SwiftUI refresh |
| 69 | + ▼ |
| 70 | +Dashboard UI (Swift Charts + SwiftUI) |
| 71 | +``` |
| 72 | + |
| 73 | +### Key Design Decisions |
| 74 | + |
| 75 | +| Decision | Rationale | |
| 76 | +|----------|-----------| |
| 77 | +| Swift Package Manager (not Xcode project) | Reproducible builds, no `.xcodeproj` noise | |
| 78 | +| `@Observable` (Observation framework) | Modern SwiftUI state management, fine-grained updates | |
| 79 | +| FSEvents API | Native macOS file watching, recursive directory support | |
| 80 | +| Actor-based `IncrementalFileReader` | Thread-safe byte offset tracking across concurrent file access | |
| 81 | +| 3-tier pricing cache | Accurate online pricing with offline resilience | |
| 82 | +| Aggregation snapshots | Sub-second startup after first run | |
| 83 | + |
| 84 | +## Project Structure |
| 85 | + |
| 86 | +``` |
| 87 | +Sources/CCMonitor/ |
| 88 | +├── App/ # Entry point, AppState pipeline, Launch at Login |
| 89 | +├── Models/ # UsageEntry, ModelPricing, AggregatedUsage, SessionInfo, ProjectInfo |
| 90 | +├── Services/ |
| 91 | +│ ├── Watcher/ # FSEventsWatcher, IncrementalFileReader |
| 92 | +│ ├── Parser/ # JSONLParser |
| 93 | +│ ├── Pricing/ # PricingService (LiteLLM), CostCalculator (tiered pricing) |
| 94 | +│ ├── Aggregation/ # UsageAggregator, BurnRateCalculator, AggregationCache |
| 95 | +│ └── Persistence/ # SwiftData UsageStore |
| 96 | +├── ViewModels/ # MenuBarViewModel, DashboardViewModel, SettingsViewModel |
| 97 | +├── Views/ |
| 98 | +│ ├── MenuBar/ # Menu bar popover |
| 99 | +│ ├── Dashboard/Panels/ # Summary cards, time series, model distribution, projects, budget |
| 100 | +│ ├── Settings/ # General + Budget tabs |
| 101 | +│ └── Components/ # Reusable UI components |
| 102 | +├── Utilities/ # Constants, Formatters, PathDiscovery, WindowManager |
| 103 | +└── Resources/ # Embedded pricing fallback |
| 104 | +``` |
| 105 | + |
| 106 | +## Development |
| 107 | + |
| 108 | +```bash |
| 109 | +# Build debug |
| 110 | +swift build |
| 111 | + |
| 112 | +# Run tests |
| 113 | +swift test |
| 114 | + |
| 115 | +# Build release .app |
| 116 | +./scripts/build-app.sh release |
| 117 | +``` |
| 118 | + |
| 119 | +### Dependencies |
| 120 | + |
| 121 | +| Package | Version | Purpose | |
| 122 | +|---------|---------|---------| |
| 123 | +| [swift-log](https://github.com/apple/swift-log) | ≥ 1.5.0 | Structured logging | |
| 124 | + |
| 125 | +All other frameworks (SwiftUI, SwiftData, Charts, AppKit, ServiceManagement) are system-provided. |
| 126 | + |
| 127 | +## License |
| 128 | + |
| 129 | +MIT |
0 commit comments