|
| 1 | +# Claude Token Parsing in VibeMeter |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +VibeMeter parses Claude Code usage logs to track token consumption. The app expects JSONL (JSON Lines) format files located in the `.claude/projects` directory. |
| 6 | + |
| 7 | +## Token Parsing Architecture |
| 8 | + |
| 9 | +### 1. **ClaudeLogManager** (Main Orchestrator) |
| 10 | +- Manages access to Claude log files via security-scoped bookmarks |
| 11 | +- Coordinates the scanning and parsing process |
| 12 | +- Caches parsed results for 5 minutes to improve performance |
| 13 | +- Uses SHA-256 hashing to detect file changes |
| 14 | + |
| 15 | +### 2. **ClaudeLogFileScanner** |
| 16 | +- Scans the `.claude/projects` directory for JSONL files |
| 17 | +- Filters out files older than 30 days based on filename dates or modification time |
| 18 | +- Returns files sorted by modification date (newest first) |
| 19 | + |
| 20 | +### 3. **ClaudeCodeLogParser** |
| 21 | +- The core parsing logic that supports multiple log formats |
| 22 | +- Uses 4 different parsing strategies in order: |
| 23 | + 1. Standard nested format (`message.usage`) |
| 24 | + 2. Top-level usage format |
| 25 | + 3. Claude Code specific formats |
| 26 | + 4. Regex-based extraction as fallback |
| 27 | + |
| 28 | +### 4. **ClaudeLogProcessor** (Background Actor) |
| 29 | +- Processes files asynchronously for better performance |
| 30 | +- Uses memory-mapped files for efficient reading |
| 31 | +- Processes data in 64KB chunks to manage memory |
| 32 | +- Skips files smaller than 100 bytes |
| 33 | + |
| 34 | +## Supported Log Formats |
| 35 | + |
| 36 | +### 1. Standard Nested Format (Original Claude API) |
| 37 | +```json |
| 38 | +{ |
| 39 | + "timestamp": "2025-01-06T10:30:00.000Z", |
| 40 | + "model": "claude-3-5-sonnet", |
| 41 | + "message": { |
| 42 | + "usage": { |
| 43 | + "input_tokens": 100, |
| 44 | + "output_tokens": 50 |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### 2. Top-Level Usage Format |
| 51 | +```json |
| 52 | +{ |
| 53 | + "timestamp": "2025-01-06T10:30:00.000Z", |
| 54 | + "model": "claude-3-5-sonnet", |
| 55 | + "usage": { |
| 56 | + "input_tokens": 100, |
| 57 | + "output_tokens": 50 |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### 3. Claude Code Format with CamelCase |
| 63 | +```json |
| 64 | +{ |
| 65 | + "timestamp": "2025-01-06T10:30:00.000Z", |
| 66 | + "model": "claude-3-5-sonnet", |
| 67 | + "message": { |
| 68 | + "usage": { |
| 69 | + "inputTokens": 100, |
| 70 | + "outputTokens": 50 |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### 4. Mixed Formats |
| 77 | +The parser can handle: |
| 78 | +- Both `input_tokens`/`output_tokens` and `inputTokens`/`outputTokens` |
| 79 | +- Usage data at top level or nested in `message.usage` |
| 80 | +- Additional fields that are ignored (type, event, metadata, etc.) |
| 81 | + |
| 82 | +## Data Structure |
| 83 | + |
| 84 | +### ClaudeLogEntry |
| 85 | +```swift |
| 86 | +struct ClaudeLogEntry { |
| 87 | + let timestamp: Date |
| 88 | + let model: String? |
| 89 | + let inputTokens: Int |
| 90 | + let outputTokens: Int |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +## Parsing Process |
| 95 | + |
| 96 | +1. **File Discovery** |
| 97 | + - Looks for JSONL files in `~/.claude/projects/` |
| 98 | + - Filters files by age (30-day cutoff) |
| 99 | + - Sorts by modification date |
| 100 | + |
| 101 | +2. **Line-by-Line Processing** |
| 102 | + - Each JSONL file contains one JSON object per line |
| 103 | + - Lines are processed individually |
| 104 | + - Non-relevant lines are skipped early (summary, user messages, etc.) |
| 105 | + |
| 106 | +3. **Token Extraction** |
| 107 | + - First attempts structured JSON parsing |
| 108 | + - Falls back to regex extraction for malformed JSON |
| 109 | + - Supports multiple field name variations |
| 110 | + |
| 111 | +4. **Filtering Rules** |
| 112 | + - Skip lines containing: `"type":"summary"`, `"type":"user"`, `leafUuid`, `sessionId`, `parentUuid` |
| 113 | + - Only process lines containing "tokens" or "Tokens" |
| 114 | + |
| 115 | +## Performance Optimizations |
| 116 | + |
| 117 | +1. **Caching**: 5-minute cache with SHA-256 file hashing |
| 118 | +2. **Memory-mapped files**: For efficient large file reading |
| 119 | +3. **Chunk processing**: 64KB chunks with autoreleasepool |
| 120 | +4. **Early filtering**: Skip small files and non-token lines |
| 121 | +5. **Parallel processing**: Background actor for async operations |
| 122 | + |
| 123 | +## Error Handling |
| 124 | + |
| 125 | +- Invalid JSON lines are skipped silently |
| 126 | +- Missing token fields result in line being skipped |
| 127 | +- Malformed timestamps use current date as fallback |
| 128 | +- File access errors are logged but don't stop processing |
| 129 | + |
| 130 | +## Usage in UI |
| 131 | + |
| 132 | +The parsed data is: |
| 133 | +- Grouped by day |
| 134 | +- Used to calculate costs based on token pricing |
| 135 | +- Displayed in the Claude Usage Report view |
| 136 | +- Used for 5-hour window calculations for Pro/Max tiers |
0 commit comments