ccstat 0.1.6

Analyze Claude Code usage data from local JSONL files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# ccstat User Guide

This guide provides comprehensive documentation for using ccstat to analyze your Claude API usage.

## Table of Contents

- [Getting Started]#getting-started
- [Command Reference]#command-reference
- [Common Use Cases]#common-use-cases
- [Advanced Features]#advanced-features
- [Tips and Tricks]#tips-and-tricks
- [FAQ]#faq

## Getting Started

### First Run

After installation, verify ccstat can find your Claude data:

```bash
# Check if data is found
ccstat daily

# If no data is found, check the discovery paths
RUST_LOG=ccstat=debug ccstat daily
```

### Basic Commands

The four main commands you'll use most often:

1. **Daily usage**: `ccstat daily`
2. **Monthly summary**: `ccstat monthly`
3. **Session details**: `ccstat session`
4. **Billing blocks**: `ccstat blocks`

## Command Reference

### Global Options

These options work with all commands:

- `--json`: Output in JSON format instead of tables
- `--help`: Show help for any command

### Daily Command

Show token usage aggregated by day.

```bash
ccstat daily [OPTIONS]
```

**Options:**
- `--since <DATE>`: Start date (YYYY-MM-DD)
- `--until <DATE>`: End date (YYYY-MM-DD)
- `--project <NAME>`: Filter by project name
- `--mode <MODE>`: Cost calculation mode (auto/calculate/display)
- `--verbose`: Show individual API calls
- `--parallel`: Enable parallel processing
- `--intern`: Use string interning
- `--arena`: Use arena allocation
- `--by-instance`: Group by instance ID

**Examples:**

```bash
# Today's usage
ccstat daily

# Last 7 days
ccstat daily --since $(date -d '7 days ago' +%Y-%m-%d)

# Specific project in January
ccstat daily --since 2024-01-01 --until 2024-01-31 --project my-project

# Detailed breakdown with individual calls
ccstat daily --verbose

# Optimized for large datasets
ccstat daily --parallel --intern --arena
```

### Monthly Command

Show usage aggregated by month.

```bash
ccstat monthly [OPTIONS]
```

**Options:**
- `--since <YYYY-MM>`: Start month
- `--until <YYYY-MM>`: End month
- `--project <NAME>`: Filter by project
- `--mode <MODE>`: Cost calculation mode

**Examples:**

```bash
# Current month
ccstat monthly

# Q1 2024
ccstat monthly --since 2024-01 --until 2024-03

# Full year 2024
ccstat monthly --since 2024-01 --until 2024-12
```

### Session Command

Show individual Claude sessions with duration and costs.

```bash
ccstat session [OPTIONS]
```

**Options:**
- `--since <DATE>`: Start date filter
- `--until <DATE>`: End date filter
- `--project <NAME>`: Filter by project
- `--mode <MODE>`: Cost calculation mode
- `--models`: Show models used in each session

**Examples:**

```bash
# All sessions
ccstat session

# Today's sessions
ccstat session --since $(date +%Y-%m-%d)

# Sessions with model details
ccstat session --models

# Export sessions for analysis
ccstat session --json > sessions.json
```

### Blocks Command

Show 5-hour billing blocks to track usage within billing periods.

```bash
ccstat blocks [OPTIONS]
```

**Options:**
- `--active`: Show only active blocks
- `--recent`: Show blocks from last 24 hours
- `--project <NAME>`: Filter by project
- `--limit <N>`: Token limit for warnings

**Examples:**

```bash
# All billing blocks
ccstat blocks

# Current active block
ccstat blocks --active

# Recent blocks with warnings for high usage
ccstat blocks --recent --limit 10000000
```

## Common Use Cases

### Daily Reporting

Create a daily usage report:

```bash
#!/bin/bash
# daily-report.sh

DATE=$(date +%Y-%m-%d)
echo "Claude Usage Report for $DATE"
echo "=============================="

# Daily summary
ccstat daily --since $DATE --until $DATE

# Active sessions
echo -e "\nActive Sessions:"
ccstat session --since $DATE --models

# Current billing block
echo -e "\nCurrent Billing Block:"
ccstat blocks --active
```

### Monthly Cost Tracking

Track costs over time:

```bash
# Current month costs
ccstat monthly

# Compare with previous month
CURRENT_MONTH=$(date +%Y-%m)
LAST_MONTH=$(date -d '1 month ago' +%Y-%m)

echo "This month:"
ccstat monthly --since $CURRENT_MONTH --until $CURRENT_MONTH

echo "Last month:"
ccstat monthly --since $LAST_MONTH --until $LAST_MONTH
```

### Project-Based Analysis

Analyze usage by project:

```bash
# List all projects (using jq)
ccstat daily --json | jq -r '.daily[].project' | sort -u

# Project-specific report
PROJECT="my-project"
ccstat daily --project $PROJECT
ccstat monthly --project $PROJECT
```

### Export for Spreadsheets

Export data for Excel/Google Sheets:

```bash
# Export daily data as CSV (using jq)
ccstat daily --json | jq -r '
  ["Date","Input","Output","Cache Create","Cache Read","Total","Cost"],
  (.daily[] | [
    .date,
    .tokens.input_tokens,
    .tokens.output_tokens,
    .tokens.cache_creation_tokens,
    .tokens.cache_read_tokens,
    .tokens.total,
    .total_cost
  ])
  | @csv'
```

## Advanced Features

### MCP Server Integration

Use ccstat as an MCP server for integration with other tools:

```bash
# Start MCP server on stdio
ccstat mcp

# HTTP server mode
ccstat mcp --transport http --port 8080
```

Example client request:

```python
import requests
import json

# Query daily usage via MCP
response = requests.post('http://localhost:8080/mcp', json={
    'jsonrpc': '2.0',
    'method': 'daily',
    'params': {
        'since': '2024-01-01',
        'until': '2024-01-31',
        'costMode': 'calculate'
    },
    'id': 1
})

data = response.json()
print(f"Total cost: ${data['result']['totals']['total_cost']:.2f}")
```

### Performance Optimization

For large datasets (millions of entries):

```bash
# Maximum performance mode
ccstat daily --parallel --intern --arena

# Benchmark different modes
time ccstat daily > /dev/null
time ccstat daily --parallel > /dev/null
time ccstat daily --parallel --intern --arena > /dev/null
```

### Custom Date Ranges

Flexible date filtering:

```bash
# Last 30 days
ccstat daily --since $(date -d '30 days ago' +%Y-%m-%d)

# Previous week
WEEK_START=$(date -d 'last monday' +%Y-%m-%d)
WEEK_END=$(date -d 'last sunday' +%Y-%m-%d)
ccstat daily --since $WEEK_START --until $WEEK_END

# Year to date
ccstat daily --since $(date +%Y)-01-01
```

## Tips and Tricks

### 1. Shell Aliases

Add to your `.bashrc` or `.zshrc`:

```bash
alias cctoday='ccstat daily --since $(date +%Y-%m-%d)'
alias ccweek='ccstat daily --since $(date -d "7 days ago" +%Y-%m-%d)'
alias ccmonth='ccstat monthly --since $(date +%Y-%m) --until $(date +%Y-%m)'
alias ccactive='ccstat blocks --active'
```

### 2. Watch Live Usage

Monitor usage in real-time:

```bash
# Update every 60 seconds
watch -n 60 'ccstat blocks --active'

# Or create a monitoring script
while true; do
  clear
  echo "Claude Usage Monitor - $(date)"
  echo "================================"
  ccstat blocks --active
  echo -e "\nToday's Usage:"
  ccstat daily --since $(date +%Y-%m-%d)
  sleep 300  # Update every 5 minutes
done
```

### 3. Cost Alerts

Create a script for cost alerts:

```bash
#!/bin/bash
# cost-alert.sh

LIMIT=100.0  # Daily cost limit
TODAY=$(date +%Y-%m-%d)

COST=$(ccstat daily --since $TODAY --json | jq -r '.totals.total_cost')

if (( $(echo "$COST > $LIMIT" | bc -l) )); then
  echo "ALERT: Daily cost ($COST) exceeds limit ($LIMIT)"
  # Send notification (email, slack, etc.)
fi
```

### 4. Backup Usage Data

Regularly backup your usage data:

```bash
# Create monthly backups
BACKUP_DIR="$HOME/claude-usage-backups"
MONTH=$(date +%Y-%m)

mkdir -p "$BACKUP_DIR"
ccstat monthly --json > "$BACKUP_DIR/usage-$MONTH.json"
```

## FAQ

### Q: How accurate are the cost calculations?

A: ccstat uses pricing data from LiteLLM's API. In 'calculate' mode, costs are computed based on current pricing. In 'auto' mode, pre-calculated costs from the usage logs are preferred when available.

### Q: Why don't I see any data?

A: Common reasons:
1. Claude Code hasn't been used yet
2. Data is in a non-standard location (set `CLAUDE_DATA_PATH`)
3. Permission issues (check directory read permissions)

### Q: How can I reduce memory usage for large datasets?

A: Use the optimization flags:
- `--parallel`: Process data in parallel
- `--intern`: Deduplicate strings in memory
- `--arena`: Use arena allocation for better memory layout

### Q: Can I use ccstat in scripts?

A: Yes! Use `--json` output for easy parsing. Exit codes:
- 0: Success
- 1: Error (check stderr for details)

### Q: How do billing blocks work?

A: Claude uses 5-hour rolling windows for billing. The `blocks` command shows these windows, helping you track usage within billing periods.

### Q: Can I filter by multiple projects?

A: Currently, only one project filter is supported at a time. Use multiple commands or process JSON output for multi-project analysis.

## Getting Help

- Run `ccstat --help` for general help
- Run `ccstat <command> --help` for command-specific help
- Enable debug logging: `RUST_LOG=ccstat=debug ccstat <command>`
- Report issues: [GitHub Issues]https://github.com/yourusername/ccstat/issues