clockwork-cli 0.2.1__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
clockwork_cli/__init__.py CHANGED
@@ -26,6 +26,7 @@ Options:
26
26
  form like 7d (7 days ago) or 2w (2 weeks ago)
27
27
  --detail <level> export granularity: raw | sessions | daily (default raw)
28
28
  --anonymize export: replace project paths with a hash id + generic name
29
+ --tokens Also report token usage and a per-model breakdown
29
30
 
30
31
  Examples:
31
32
  clockwork claude ~/code/myproject
@@ -37,6 +38,9 @@ Examples:
37
38
  clockwork both today
38
39
  clockwork claude export > clockwork.json
39
40
  clockwork codex export --detail sessions --anonymize > share.json
41
+ clockwork both today --tokens
42
+ clockwork claude all --tokens --since 7d
43
+ clockwork both export --tokens > clockwork.json
40
44
  clockwork claude all --since 7d
41
45
  clockwork claude ~/code/myproject --since 2026-07-01 --until 2026-07-05
42
46
  clockwork claude list
@@ -44,6 +48,22 @@ Examples:
44
48
  Data sources:
45
49
  claude → ~/.claude/projects/<encoded-path>/*.jsonl
46
50
  codex → $CODEX_HOME/sessions/**/rollout-*.jsonl (default ~/.codex/sessions)
51
+
52
+ Token usage (--tokens):
53
+ Both tools log per-response usage, counted differently, so clockwork
54
+ normalizes them before summing:
55
+
56
+ claude → `assistant` records carry message.model + message.usage. One API
57
+ response spans several lines (thinking, text, each tool_use), each
58
+ repeating its usage block, so responses are deduplicated on
59
+ message id, keeping the final (largest) count per field.
60
+ Subagent logs (<session>/subagents/*.jsonl) are included. Cache
61
+ reads/writes sit outside input_tokens.
62
+ codex → `event_msg -> token_count` records carry a cumulative
63
+ total_token_usage; per-turn cost is read as the rise in that counter
64
+ because last_token_usage is re-emitted on aborted turns. The model
65
+ comes from the preceding `turn_context`, and cached_input_tokens is
66
+ subtracted out of input_tokens to match Claude's split.
47
67
  """
48
68
 
49
69
  import sys
@@ -51,11 +71,12 @@ import json
51
71
  import os
52
72
  import re
53
73
  import glob
74
+ import bisect
54
75
  import hashlib
55
76
  from datetime import datetime, timedelta, timezone
56
77
  from collections import defaultdict
57
78
 
58
- __version__ = "0.2.1"
79
+ __version__ = "0.4.0"
59
80
 
60
81
 
61
82
  def _resolve_claude_dir():
@@ -129,6 +150,92 @@ def _add_session_to_daily(daily, start, end):
129
150
  cursor = seg_end
130
151
 
131
152
 
153
+ # --------------------------------------------------------------------------- #
154
+ # Token accounting
155
+ # --------------------------------------------------------------------------- #
156
+ #
157
+ # Both providers log per-response token usage, but they count it differently,
158
+ # so every backend normalizes into one record shape before anything is summed:
159
+ #
160
+ # input fresh (uncached) input tokens
161
+ # output output tokens, thinking/reasoning included
162
+ # cache_read input served from cache
163
+ # cache_write input written to cache (Claude only; Codex never reports it)
164
+ # reasoning thinking tokens, a SUBSET of output (Codex only; Claude folds
165
+ # thinking into output without breaking it out)
166
+ #
167
+ # Claude reports cache reads and writes outside `input_tokens`; Codex reports
168
+ # `cached_input_tokens` *inside* `input_tokens`, so the Codex reader subtracts
169
+ # it back out. After that, total = input + output + cache_read + cache_write
170
+ # holds for both, and `reasoning` is deliberately left out of it to avoid
171
+ # double counting the part of output it already covers.
172
+
173
+ TOKEN_FIELDS = ("input", "output", "cache_read", "cache_write", "reasoning")
174
+
175
+
176
+ def _new_token_rec():
177
+ rec = {"responses": 0}
178
+ rec.update({f: 0 for f in TOKEN_FIELDS})
179
+ return rec
180
+
181
+
182
+ def _accumulate_tokens(rec, event):
183
+ rec["responses"] += 1
184
+ for f in TOKEN_FIELDS:
185
+ rec[f] += event[f]
186
+
187
+
188
+ def _sealed(rec):
189
+ """Copy a running record with its derived `total` filled in."""
190
+ out = dict(rec)
191
+ out["total"] = rec["input"] + rec["output"] + rec["cache_read"] + rec["cache_write"]
192
+ return out
193
+
194
+
195
+ def summarize_tokens(events):
196
+ """Aggregate token events into overall totals plus a per-model breakdown."""
197
+ overall = _new_token_rec()
198
+ per_model = defaultdict(_new_token_rec)
199
+ for event in events:
200
+ _accumulate_tokens(overall, event)
201
+ _accumulate_tokens(per_model[event["model"] or "unknown"], event)
202
+ summary = _sealed(overall)
203
+ models = []
204
+ for model, rec in per_model.items():
205
+ entry = {"model": model}
206
+ entry.update(_sealed(rec))
207
+ models.append(entry)
208
+ # Biggest consumer first, name as the tie-break so output is deterministic.
209
+ models.sort(key=lambda m: (-m["total"], m["model"]))
210
+ summary["by_model"] = models
211
+ return summary
212
+
213
+
214
+ def tokens_by_day(events):
215
+ """Bucket token events into per-day (UTC) sealed records."""
216
+ daily = defaultdict(_new_token_rec)
217
+ for event in events:
218
+ _accumulate_tokens(daily[event["ts"].date()], event)
219
+ return {day: _sealed(rec) for day, rec in daily.items()}
220
+
221
+
222
+ def _filter_events(events, since, until):
223
+ if since is None and until is None:
224
+ return events
225
+ return [
226
+ e for e in events
227
+ if (since is None or e["ts"] >= since) and (until is None or e["ts"] <= until)
228
+ ]
229
+
230
+
231
+ def fmt_tokens(n):
232
+ """Compact token count for table cells (1_234_567 -> '1.2M')."""
233
+ for cutoff, suffix in ((1e9, "B"), (1e6, "M"), (1e3, "k")):
234
+ if n >= cutoff:
235
+ return f"{n / cutoff:.1f}{suffix}"
236
+ return str(int(n))
237
+
238
+
132
239
  def fmt_duration(minutes):
133
240
  h = int(minutes) // 60
134
241
  m = int(minutes) % 60
@@ -246,30 +353,178 @@ def _display_name(path):
246
353
  # Claude backend (~/.claude/projects/<encoded-path>/*.jsonl)
247
354
  # --------------------------------------------------------------------------- #
248
355
 
356
+ def _claude_encode_path(path):
357
+ """Encode a path the way Claude Code names its per-project log folder.
358
+
359
+ Every character that is not an ASCII letter or digit becomes `-`, so
360
+ `/a/team-5/My Drive` and `/a/team/5/My-Drive` both encode to
361
+ `-a-team-5-My-Drive`. That makes the encoding lossy; see
362
+ `_claude_folder_cwd` for recovering the real path.
363
+ """
364
+ return re.sub(r"[^A-Za-z0-9]", "-", path)
365
+
366
+
249
367
  def path_to_claude_folder(project_path):
250
368
  abs_path = os.path.abspath(os.path.expanduser(project_path))
251
- folder_name = abs_path.replace("/", "-").replace("\\", "-").replace(":", "-")
369
+ folder_name = _claude_encode_path(abs_path)
252
370
  if folder_name.startswith("-"):
253
371
  folder_name = folder_name[1:]
254
372
  return folder_name
255
373
 
256
374
 
375
+ def _claude_session_files(folder_dir):
376
+ """Return ([session logs], [subagent logs]) for one Claude project folder.
377
+
378
+ Subagent transcripts are not part of the session log: Claude Code writes
379
+ them beside it, under `<session-id>/subagents/agent-*.jsonl`.
380
+ """
381
+ sessions = glob.glob(os.path.join(folder_dir, "*.jsonl"))
382
+ subagents = glob.glob(os.path.join(folder_dir, "*", "subagents", "*.jsonl"))
383
+ return sessions, subagents
384
+
385
+
257
386
  def _claude_resolve_files(project_path):
258
- """Return (folder_name, [files]) for a project path, or (None, [])."""
387
+ """Return (folder_name, [session logs], [subagent logs]) for a project
388
+ path, or (None, [], [])."""
259
389
  folder = path_to_claude_folder(project_path)
260
390
  # Claude Code retains the leading dash from the absolute path, so try both
261
391
  # the dash-prefixed and stripped forms to match whichever convention exists.
262
392
  for candidate in ("-" + folder, folder):
263
- files = glob.glob(os.path.join(CLAUDE_DIR, candidate, "*.jsonl"))
393
+ files, subagents = _claude_session_files(os.path.join(CLAUDE_DIR, candidate))
264
394
  if files:
265
- return candidate, files
266
- return None, []
395
+ return candidate, files, subagents
396
+ return None, [], []
397
+
398
+
399
+ _CLAUDE_USAGE_FIELDS = (
400
+ ("input", "input_tokens"),
401
+ ("output", "output_tokens"),
402
+ ("cache_read", "cache_read_input_tokens"),
403
+ ("cache_write", "cache_creation_input_tokens"),
404
+ )
267
405
 
268
406
 
269
- def _claude_timestamps_from_files(files):
270
- """Parse user/human turn timestamps from Claude .jsonl files."""
407
+ def _claude_token_event(record, dt, seen):
408
+ """Normalize one Claude assistant record into a token event, or None.
409
+
410
+ Claude Code writes one line per content block — thinking, text, and each
411
+ tool_use — and every line repeats the usage of the whole API response.
412
+ Counting per line inflates totals roughly 2.6x, so responses are
413
+ deduplicated on the message id (falling back to the request id).
414
+
415
+ The repeated usage is not always identical: subagent logs write the first
416
+ line while the response is still streaming (`output_tokens: 3`) and only
417
+ the last line carries the final count. So a repeat is folded into the
418
+ event already recorded, keeping the larger value per field. Main-thread
419
+ lines are identical, so for them this is the same as keeping the first.
420
+ `seen` maps each key to the event recorded for it.
421
+ """
422
+ message = record.get("message") or {}
423
+ usage = message.get("usage")
424
+ model = message.get("model")
425
+ # `<synthetic>` marks locally generated messages (errors, interrupts);
426
+ # they carry a zero usage block and never hit the API.
427
+ if not usage or not model or model == "<synthetic>" or dt is None:
428
+ return None
429
+ event = {"ts": dt, "model": model}
430
+ for field, usage_key in _CLAUDE_USAGE_FIELDS:
431
+ event[field] = usage.get(usage_key) or 0
432
+ event["reasoning"] = 0 # Claude folds thinking into output_tokens
433
+ key = message.get("id") or record.get("requestId")
434
+ if key is not None:
435
+ prior = seen.get(key)
436
+ if prior is not None:
437
+ for field, _ in _CLAUDE_USAGE_FIELDS:
438
+ prior[field] = max(prior[field], event[field])
439
+ return None
440
+ seen[key] = event
441
+ return event
442
+
443
+
444
+ # Leading text of user-role records that Claude Code writes on the user's
445
+ # behalf rather than from anything sent as a prompt: local slash-command
446
+ # echoes and output (/model, /clear), `!` shell input and output, background
447
+ # task notifications, and interrupt markers. Skill-style commands that do go
448
+ # to the model start with `<command-message>` instead and are kept.
449
+ _CLAUDE_NOT_TYPED_PREFIXES = (
450
+ "<command-name>",
451
+ "<local-command-stdout>",
452
+ "<local-command-stderr>",
453
+ "<local-command-caveat>",
454
+ "<bash-input>",
455
+ "<bash-stdout>",
456
+ "<bash-stderr>",
457
+ "<task-notification>",
458
+ "<system-reminder>",
459
+ "[Request interrupted by user",
460
+ )
461
+
462
+
463
+ def _claude_is_typed_text(text):
464
+ text = (text or "").lstrip()
465
+ return bool(text) and not text.startswith(_CLAUDE_NOT_TYPED_PREFIXES)
466
+
467
+
468
+ def _claude_is_human_prompt(record):
469
+ """True when a user-role record is a message a person sent.
470
+
471
+ Most user-role records are not: the API returns every tool result to the
472
+ model as a user message, and Claude Code also logs injected context and
473
+ command output under the same role. Newer logs tag real input with
474
+ `origin.kind == "human"`, and that tag is trusted wherever it is present.
475
+ Records without an `origin` (older logs, and some newer ones) are
476
+ classified by shape instead.
477
+
478
+ One shape needs care: a message typed while a tool is running is folded
479
+ into that tool's result record as an extra text block. So a tool result
480
+ alone is not a prompt, but a tool result carrying typed text is.
481
+ """
482
+ origin = record.get("origin")
483
+ if isinstance(origin, dict) and origin.get("kind"):
484
+ return origin["kind"] == "human"
485
+ if record.get("isMeta") or record.get("isCompactSummary"):
486
+ return False
487
+ content = (record.get("message") or {}).get("content")
488
+ if isinstance(content, str):
489
+ return _claude_is_typed_text(content)
490
+ if not isinstance(content, list):
491
+ return False
492
+ blocks = [b for b in content if isinstance(b, dict)]
493
+ texts = [b.get("text") for b in blocks if b.get("type") == "text"]
494
+ if any(_claude_is_typed_text(t) for t in texts):
495
+ return True
496
+ # No typed text: an image pasted on its own still counts; a bare tool
497
+ # result or a wrapper-only record does not.
498
+ has_tool_result = any(b.get("type") == "tool_result" for b in blocks)
499
+ return not texts and not has_tool_result and bool(blocks)
500
+
501
+
502
+ def _claude_scan_files(files, subagent_files=()):
503
+ """Return (activity timestamps, token events, human prompt timestamps)
504
+ from Claude .jsonl files.
505
+
506
+ Activity is every user-role record, tool results included. Those are what
507
+ show Claude working through a long autonomous run, so session grouping and
508
+ active time are built on them. Human prompts are the subset a person
509
+ actually sent (`_claude_is_human_prompt`), deduplicated on record uuid
510
+ because a resumed session replays earlier messages into its new log.
511
+
512
+ Everything comes out of a single pass. The token dedup map is shared
513
+ across the whole file list so a response replayed into a resumed
514
+ session's log is still only counted once.
515
+
516
+ `subagent_files` contribute token events only. Their user-role records are
517
+ the parent's task prompt plus tool results, none of it typed by a person
518
+ (see ROADMAP §1, which is where subagent activity belongs).
519
+ """
271
520
  timestamps = []
272
- for f in sorted(files):
521
+ human = []
522
+ events = []
523
+ seen = {}
524
+ seen_prompts = set()
525
+ sources = [(f, True) for f in sorted(files)]
526
+ sources += [(f, False) for f in sorted(subagent_files)]
527
+ for f, is_session in sources:
273
528
  with open(f, encoding="utf-8") as fh:
274
529
  for line in fh:
275
530
  line = line.strip()
@@ -282,8 +537,19 @@ def _claude_timestamps_from_files(files):
282
537
  role = d.get("role") or d.get("type", "")
283
538
  dt = _parse_ts(d.get("timestamp"))
284
539
  if dt and role in ("human", "user"):
540
+ if not is_session:
541
+ continue
285
542
  timestamps.append(dt)
286
- return sorted(timestamps)
543
+ if _claude_is_human_prompt(d):
544
+ uid = d.get("uuid")
545
+ if uid is None or uid not in seen_prompts:
546
+ seen_prompts.add(uid)
547
+ human.append(dt)
548
+ elif role == "assistant":
549
+ event = _claude_token_event(d, dt, seen)
550
+ if event:
551
+ events.append(event)
552
+ return sorted(timestamps), events, sorted(human)
287
553
 
288
554
 
289
555
  def _claude_folder_to_pretty(folder):
@@ -293,25 +559,66 @@ def _claude_folder_to_pretty(folder):
293
559
  return "/" + folder.lstrip("-").replace("-", "/")
294
560
 
295
561
 
296
- def claude_single(project_path):
297
- folder, files = _claude_resolve_files(project_path)
562
+ def _claude_folder_cwd(folder, files):
563
+ """The project's real path, read from the `cwd` its records carry.
564
+
565
+ The folder name can't be decoded reliably (`team-5` and `team/5` encode
566
+ alike), and a wrong decode both mislabels the project and stops `both`
567
+ from merging it with the Codex rollouts for the same path. Every Claude
568
+ record carries `cwd`, so take the first one that encodes back to this
569
+ folder's name. The check matters because a session that `cd`s elsewhere
570
+ records the new cwd. Falls back to the decoded name if nothing matches.
571
+ """
572
+ for f in sorted(files):
573
+ try:
574
+ with open(f, encoding="utf-8") as fh:
575
+ for line in fh:
576
+ if '"cwd"' not in line:
577
+ continue
578
+ try:
579
+ cwd = json.loads(line).get("cwd")
580
+ except (json.JSONDecodeError, AttributeError):
581
+ continue
582
+ if cwd and _claude_encode_path(cwd) == folder:
583
+ return cwd
584
+ except OSError:
585
+ continue
586
+ return _claude_folder_to_pretty(folder)
587
+
588
+
589
+ def claude_scan_single(project_path):
590
+ folder, files, subagents = _claude_resolve_files(project_path)
298
591
  if not files:
299
- return None, 0, []
300
- return folder, len(files), _claude_timestamps_from_files(files)
592
+ return None, 0, [], [], []
593
+ ts, events, human = _claude_scan_files(files, subagents)
594
+ return folder, len(files), ts, events, human
301
595
 
302
596
 
303
- def claude_all():
597
+ def claude_single(project_path):
598
+ folder, count, ts, _, human = claude_scan_single(project_path)
599
+ return folder, count, ts, human
600
+
601
+
602
+ def claude_scan():
304
603
  result = []
305
604
  for folder in _claude_list_folders():
306
- files = glob.glob(os.path.join(CLAUDE_DIR, folder, "*.jsonl"))
307
- ts = _claude_timestamps_from_files(files)
605
+ files, subagents = _claude_session_files(os.path.join(CLAUDE_DIR, folder))
606
+ ts, events, human = _claude_scan_files(files, subagents)
308
607
  if ts:
309
- result.append((_claude_folder_to_pretty(folder), ts))
608
+ result.append((_claude_folder_cwd(folder, files), ts, events, human))
310
609
  return result
311
610
 
312
611
 
612
+ def claude_all():
613
+ return [(label, ts, human) for label, ts, _, human in claude_scan()]
614
+
615
+
313
616
  def _claude_list_folders():
314
- return sorted(os.listdir(CLAUDE_DIR)) if os.path.exists(CLAUDE_DIR) else []
617
+ if not os.path.exists(CLAUDE_DIR):
618
+ return []
619
+ # Skip stray files such as macOS's .DS_Store; only folders hold logs.
620
+ return sorted(name for name in os.listdir(CLAUDE_DIR)
621
+ if os.path.isdir(os.path.join(CLAUDE_DIR, name)))
315
622
 
316
623
 
317
624
  def claude_list():
@@ -328,16 +635,80 @@ def _codex_files():
328
635
  return glob.glob(pattern, recursive=True)
329
636
 
330
637
 
331
- def _codex_parse_file(path):
332
- """Return (cwd, [timestamps]) for one rollout file.
638
+ _CODEX_TOKEN_KEYS = ("input_tokens", "cached_input_tokens",
639
+ "output_tokens", "reasoning_output_tokens")
640
+
333
641
 
334
- Uses the canonical `event_msg -> user_message` prompt records; only falls
335
- back to `response_item` user-role messages for files that contain no
336
- canonical prompt events, to avoid double counting the same input.
642
+ def _codex_token_event(record, payload, running, model):
643
+ """Normalize one Codex `token_count` record into a token event, or None.
644
+
645
+ Codex reports both a per-turn `last_token_usage` and a cumulative
646
+ `total_token_usage`, and the per-turn number cannot be trusted on its own:
647
+ aborted and no-op turns re-emit a stale `last_token_usage` that the
648
+ cumulative counter never absorbs, so summing it overstates usage (by up to
649
+ 20% in a single session). The turn's real cost is the rise in the
650
+ cumulative counter, which is monotonic within a rollout and restarts at
651
+ zero in forked/resumed sessions — so no cross-file double counting either.
652
+ """
653
+ info = payload.get("info") or {}
654
+ total = info.get("total_token_usage") or {}
655
+ delta = {}
656
+ for k in _CODEX_TOKEN_KEYS:
657
+ value = total.get(k) or 0
658
+ delta[k] = max(0, value - running[k])
659
+ running[k] = value
660
+ if not any(delta.values()):
661
+ return None
662
+ dt = _parse_ts(record.get("timestamp") or payload.get("timestamp"))
663
+ if dt is None:
664
+ return None
665
+ cached = delta["cached_input_tokens"]
666
+ return {
667
+ "ts": dt,
668
+ "model": model,
669
+ # Codex counts cache reads inside input_tokens; split them back out so
670
+ # the number means the same thing it does on the Claude side.
671
+ "input": max(0, delta["input_tokens"] - cached),
672
+ "output": delta["output_tokens"],
673
+ "cache_read": cached,
674
+ "cache_write": 0, # not reported by Codex
675
+ "reasoning": delta["reasoning_output_tokens"],
676
+ }
677
+
678
+
679
+ # Records that carry a tool's result back to the model. Counted as activity.
680
+ _CODEX_TOOL_OUTPUTS = ("function_call_output", "custom_tool_call_output")
681
+
682
+
683
+ def _codex_scan_file(path):
684
+ """Return (cwd, [activity], [token events], [prompts]) for one rollout.
685
+
686
+ Activity is the prompts plus every tool result (`function_call_output`,
687
+ `custom_tool_call_output`), the same rule the Claude reader uses: a long
688
+ tool run with no human input is still work, not idle time. Prompts stay a
689
+ separate list, so tool results never count as something a person typed.
690
+
691
+ Prompts come from the first of these sources that the file contains, so
692
+ the same input is never counted twice:
693
+
694
+ 1. `event_msg -> user_message`, the canonical record in older rollouts
695
+ 2. `event_msg -> item_completed` with a `UserMessage` item, which
696
+ replaced it in newer rollouts
697
+ 3. `response_item` user-role messages, as a last resort. These also
698
+ carry injected context (environment, AGENTS.md), so they overcount.
699
+
700
+ The model is not in `session_meta` (which only names the provider) — it
701
+ rides on `turn_context`, so each turn is attributed to the most recent one.
337
702
  """
338
703
  cwd = None
339
704
  canonical = []
705
+ items = []
340
706
  fallback = []
707
+ tool_outputs = []
708
+ events = []
709
+ model = None
710
+ first_model = None
711
+ running = {k: 0 for k in _CODEX_TOKEN_KEYS}
341
712
  try:
342
713
  with open(path, encoding="utf-8") as fh:
343
714
  for line in fh:
@@ -352,42 +723,86 @@ def _codex_parse_file(path):
352
723
  payload = d.get("payload") or {}
353
724
  if t == "session_meta":
354
725
  cwd = payload.get("cwd")
726
+ elif t == "turn_context":
727
+ model = payload.get("model") or model
728
+ if first_model is None:
729
+ first_model = model
355
730
  elif t == "event_msg" and payload.get("type") == "user_message":
356
731
  dt = _parse_ts(d.get("timestamp") or payload.get("timestamp"))
357
732
  if dt:
358
733
  canonical.append(dt)
734
+ elif (t == "event_msg"
735
+ and payload.get("type") == "item_completed"
736
+ and (payload.get("item") or {}).get("type") == "UserMessage"):
737
+ dt = _parse_ts(d.get("timestamp"))
738
+ if dt:
739
+ items.append(dt)
740
+ elif t == "event_msg" and payload.get("type") == "token_count":
741
+ event = _codex_token_event(d, payload, running, model)
742
+ if event:
743
+ events.append(event)
359
744
  elif (t == "response_item"
360
745
  and payload.get("type") == "message"
361
746
  and payload.get("role") == "user"):
362
747
  dt = _parse_ts(d.get("timestamp") or payload.get("timestamp"))
363
748
  if dt:
364
749
  fallback.append(dt)
750
+ elif (t == "response_item"
751
+ and payload.get("type") in _CODEX_TOOL_OUTPUTS):
752
+ dt = _parse_ts(d.get("timestamp"))
753
+ if dt:
754
+ tool_outputs.append(dt)
365
755
  except OSError:
366
- return None, []
367
- return cwd, sorted(canonical if canonical else fallback)
368
-
369
-
370
- def codex_single(project_path):
756
+ return None, [], [], []
757
+ # A few turns are logged before the first `turn_context`; credit them to
758
+ # the model the session went on to use rather than losing them to unknown.
759
+ if first_model:
760
+ for event in events:
761
+ if event["model"] is None:
762
+ event["model"] = first_model
763
+ prompts = sorted(canonical or items or fallback)
764
+ return cwd, sorted(prompts + tool_outputs), events, prompts
765
+
766
+
767
+ def codex_scan_single(project_path):
371
768
  abs_path = os.path.abspath(os.path.expanduser(project_path))
372
769
  matched = 0
373
770
  timestamps = []
771
+ events = []
772
+ human = []
374
773
  for f in _codex_files():
375
- cwd, file_ts = _codex_parse_file(f)
774
+ cwd, file_ts, file_events, file_human = _codex_scan_file(f)
376
775
  if cwd == abs_path:
377
776
  matched += 1
378
777
  timestamps.extend(file_ts)
778
+ events.extend(file_events)
779
+ human.extend(file_human)
379
780
  if not matched:
380
- return None, 0, []
381
- return abs_path, matched, sorted(timestamps)
781
+ return None, 0, [], [], []
782
+ return abs_path, matched, sorted(timestamps), events, sorted(human)
382
783
 
383
784
 
384
- def codex_all():
385
- groups = defaultdict(list)
785
+ def codex_single(project_path):
786
+ label, count, ts, _, human = codex_scan_single(project_path)
787
+ return label, count, ts, human
788
+
789
+
790
+ def codex_scan():
791
+ ts_groups = defaultdict(list)
792
+ event_groups = defaultdict(list)
793
+ human_groups = defaultdict(list)
386
794
  for f in _codex_files():
387
- cwd, file_ts = _codex_parse_file(f)
795
+ cwd, file_ts, file_events, file_human = _codex_scan_file(f)
388
796
  if cwd and file_ts:
389
- groups[cwd].extend(file_ts)
390
- return [(cwd, sorted(ts)) for cwd, ts in groups.items()]
797
+ ts_groups[cwd].extend(file_ts)
798
+ event_groups[cwd].extend(file_events)
799
+ human_groups[cwd].extend(file_human)
800
+ return [(cwd, sorted(ts), event_groups[cwd], sorted(human_groups[cwd]))
801
+ for cwd, ts in ts_groups.items()]
802
+
803
+
804
+ def codex_all():
805
+ return [(label, ts, human) for label, ts, _, human in codex_scan()]
391
806
 
392
807
 
393
808
  def _codex_cwd_only(path):
@@ -427,21 +842,49 @@ def codex_list():
427
842
  # --------------------------------------------------------------------------- #
428
843
 
429
844
  def _merge_projects(*project_lists):
430
- """Merge [(label, timestamps)] lists, combining same-label projects."""
431
- groups = defaultdict(list)
845
+ """Merge [(label, timestamps, human)] lists, combining same-label projects."""
846
+ ts_groups = defaultdict(list)
847
+ human_groups = defaultdict(list)
432
848
  for projects in project_lists:
433
- for label, ts in projects:
434
- groups[label].extend(ts)
435
- return [(label, sorted(ts)) for label, ts in groups.items()]
849
+ for label, ts, human in projects:
850
+ ts_groups[label].extend(ts)
851
+ human_groups[label].extend(human)
852
+ return [(label, sorted(ts), sorted(human_groups[label]))
853
+ for label, ts in ts_groups.items()]
854
+
855
+
856
+ def _merge_scans(*scans):
857
+ """Merge [(label, timestamps, events, human)] lists, combining same-label
858
+ projects."""
859
+ ts_groups = defaultdict(list)
860
+ event_groups = defaultdict(list)
861
+ human_groups = defaultdict(list)
862
+ for scan in scans:
863
+ for label, ts, events, human in scan:
864
+ ts_groups[label].extend(ts)
865
+ event_groups[label].extend(events)
866
+ human_groups[label].extend(human)
867
+ return [(label, sorted(ts), event_groups[label], sorted(human_groups[label]))
868
+ for label, ts in ts_groups.items()]
869
+
870
+
871
+ def both_scan_single(project_path):
872
+ _, c_count, c_ts, c_events, c_human = claude_scan_single(project_path)
873
+ _, x_count, x_ts, x_events, x_human = codex_scan_single(project_path)
874
+ count = c_count + x_count
875
+ if count == 0:
876
+ return None, 0, [], [], []
877
+ return (project_path, count, sorted(c_ts + x_ts), c_events + x_events,
878
+ sorted(c_human + x_human))
436
879
 
437
880
 
438
881
  def both_single(project_path):
439
- _, c_count, c_ts = claude_single(project_path)
440
- _, x_count, x_ts = codex_single(project_path)
441
- count = c_count + x_count
442
- if count == 0:
443
- return None, 0, []
444
- return project_path, count, sorted(c_ts + x_ts)
882
+ label, count, ts, _, human = both_scan_single(project_path)
883
+ return label, count, ts, human
884
+
885
+
886
+ def both_scan():
887
+ return _merge_scans(claude_scan(), codex_scan())
445
888
 
446
889
 
447
890
  def both_all():
@@ -449,7 +892,10 @@ def both_all():
449
892
 
450
893
 
451
894
  def both_list():
452
- labels = {_claude_folder_to_pretty(f) for f in _claude_list_folders()}
895
+ labels = {
896
+ _claude_folder_cwd(f, glob.glob(os.path.join(CLAUDE_DIR, f, "*.jsonl")))
897
+ for f in _claude_list_folders()
898
+ }
453
899
  labels.update(codex_list())
454
900
  return sorted(labels)
455
901
 
@@ -458,22 +904,40 @@ def both_list():
458
904
  # Provider dispatch
459
905
  # --------------------------------------------------------------------------- #
460
906
 
907
+ # Every backend returns two timestamp lists per project: activity (every
908
+ # user-role record, which drives sessions and active time) and human (the
909
+ # messages a person sent, which is what the `human_prompts` count reports).
910
+ #
911
+ # single(path) → (label, files, activity, human)
912
+ # scan_single(path) → (label, files, activity, events, human)
913
+ # all() → [(label, activity, human)]
914
+ # scan() → [(label, activity, events, human)]
915
+ #
916
+ # `scan`/`scan_single` are the token-aware backends: same work as `all`/`single`
917
+ # plus the usage records picked up in the same pass. `all`/`single` remain the
918
+ # timing-only views, and are what the plain (no --tokens) commands read.
461
919
  PROVIDERS = {
462
920
  "claude": {
463
921
  "single": claude_single,
922
+ "scan_single": claude_scan_single,
464
923
  "all": claude_all,
924
+ "scan": claude_scan,
465
925
  "list": claude_list,
466
926
  "location": "~/.claude/projects/",
467
927
  },
468
928
  "codex": {
469
929
  "single": codex_single,
930
+ "scan_single": codex_scan_single,
470
931
  "all": codex_all,
932
+ "scan": codex_scan,
471
933
  "list": codex_list,
472
934
  "location": "$CODEX_HOME/sessions/",
473
935
  },
474
936
  "both": {
475
937
  "single": both_single,
938
+ "scan_single": both_scan_single,
476
939
  "all": both_all,
940
+ "scan": both_scan,
477
941
  "list": both_list,
478
942
  "location": "claude + codex",
479
943
  },
@@ -498,15 +962,119 @@ def _export_providers(provider):
498
962
  # Output modes
499
963
  # --------------------------------------------------------------------------- #
500
964
 
501
- def analyze_single(provider, project_path, idle_threshold, since, until, as_json):
502
- label, count, timestamps = PROVIDERS[provider]["single"](project_path)
965
+ MODEL_TABLE_WIDTH = 80
966
+
967
+
968
+ def _day_keys(daily, token_days):
969
+ """Every day either bucket knows about, in order.
970
+
971
+ A response can complete after midnight, past the last prompt that defines
972
+ the session's end — so a day can carry tokens without carrying active
973
+ minutes. Taking the union keeps the per-day rows summing to the totals.
974
+ """
975
+ return sorted(set(daily) | set(token_days))
976
+
977
+
978
+ def _load_projects(provider, show_tokens):
979
+ """[(label, activity, events, human)] for every project of a provider.
980
+
981
+ Reads the token-aware backend only when tokens were asked for; the plain
982
+ backend is the same scan without usage, so `events` is empty.
983
+ """
984
+ if show_tokens:
985
+ return PROVIDERS[provider]["scan"]()
986
+ return [(label, ts, [], human)
987
+ for label, ts, human in PROVIDERS[provider]["all"]()]
988
+
989
+
990
+ def _per_day(timestamps):
991
+ """Count timestamps per calendar day."""
992
+ counts = defaultdict(int)
993
+ for ts in timestamps:
994
+ counts[ts.date()] += 1
995
+ return counts
996
+
997
+
998
+ def _human_per_session(sessions, human):
999
+ """Human prompts inside each (start, end, count) session, in order.
1000
+
1001
+ Every human prompt is also an activity timestamp, so each one falls inside
1002
+ exactly one session's [start, end].
1003
+ """
1004
+ return [bisect.bisect_right(human, end) - bisect.bisect_left(human, start)
1005
+ for start, end, _ in sessions]
1006
+
1007
+
1008
+ def _day_entry(day, daily, prompts_per_day, human_per_day, token_days,
1009
+ show_tokens):
1010
+ """One JSON day bucket, with tokens attached when they were collected.
1011
+
1012
+ `prompts` keeps its original meaning (every user-role record, tool results
1013
+ included) so existing consumers are unaffected; `human_prompts` is the
1014
+ count of messages a person sent.
1015
+ """
1016
+ entry = {"date": str(day),
1017
+ "minutes": round(daily[day], 2),
1018
+ "prompts": prompts_per_day[day],
1019
+ "human_prompts": human_per_day[day]}
1020
+ if show_tokens:
1021
+ entry["tokens"] = token_days.get(day) or _sealed(_new_token_rec())
1022
+ return entry
1023
+
1024
+
1025
+ def _print_token_lines(summary):
1026
+ """The three-line token block used under a run's summary header."""
1027
+ print(f" Tokens : {fmt_tokens(summary['total'])} across "
1028
+ f"{summary['responses']:,} responses")
1029
+ print(f" In/Out : {fmt_tokens(summary['input'])} in · "
1030
+ f"{fmt_tokens(summary['output'])} out")
1031
+ print(f" Cache : {fmt_tokens(summary['cache_read'])} read · "
1032
+ f"{fmt_tokens(summary['cache_write'])} write")
1033
+ if summary["reasoning"]:
1034
+ print(f" Thinking: {fmt_tokens(summary['reasoning'])} "
1035
+ f"(Codex only; already counted inside output)")
1036
+
1037
+
1038
+ def _print_model_table(summary):
1039
+ """Per-model token breakdown, biggest consumer first."""
1040
+ if not summary["by_model"]:
1041
+ return
1042
+ print(f"\n {'MODEL':<20} {'RESP':>7} {'INPUT':>9} {'OUTPUT':>9} "
1043
+ f"{'CACHE R':>9} {'CACHE W':>9} {'TOTAL':>9}")
1044
+ print("-" * MODEL_TABLE_WIDTH)
1045
+ for m in summary["by_model"]:
1046
+ name = m["model"]
1047
+ if len(name) > 19:
1048
+ name = "…" + name[-18:]
1049
+ print(f" {name:<20} {m['responses']:>7,} {fmt_tokens(m['input']):>9} "
1050
+ f"{fmt_tokens(m['output']):>9} {fmt_tokens(m['cache_read']):>9} "
1051
+ f"{fmt_tokens(m['cache_write']):>9} {fmt_tokens(m['total']):>9}")
1052
+ print("-" * MODEL_TABLE_WIDTH)
1053
+ print(f" {'TOTAL':<20} {summary['responses']:>7,} "
1054
+ f"{fmt_tokens(summary['input']):>9} {fmt_tokens(summary['output']):>9} "
1055
+ f"{fmt_tokens(summary['cache_read']):>9} "
1056
+ f"{fmt_tokens(summary['cache_write']):>9} "
1057
+ f"{fmt_tokens(summary['total']):>9}")
1058
+
1059
+
1060
+ def analyze_single(provider, project_path, idle_threshold, since, until, as_json,
1061
+ show_tokens=False):
1062
+ if show_tokens:
1063
+ label, count, timestamps, events, human = \
1064
+ PROVIDERS[provider]["scan_single"](project_path)
1065
+ events = _filter_events(events, since, until)
1066
+ else:
1067
+ label, count, timestamps, human = PROVIDERS[provider]["single"](project_path)
1068
+ events = []
503
1069
  timestamps = _filter_timestamps(timestamps, since, until)
1070
+ human = _filter_timestamps(human, since, until)
504
1071
 
505
1072
  if count == 0:
506
1073
  if as_json:
507
1074
  _print_json({
508
1075
  "provider": provider, "project": project_path,
509
- "error": "no sessions found", "prompts": 0, "days": [],
1076
+ "error": "no sessions found", "prompts": 0,
1077
+ "human_prompts": 0, "days": [],
510
1078
  })
511
1079
  sys.exit(1)
512
1080
  print(f"❌ No {provider} sessions found for: {project_path}")
@@ -522,7 +1090,7 @@ def analyze_single(provider, project_path, idle_threshold, since, until, as_json
522
1090
  _print_json({
523
1091
  "provider": provider, "project": project_path, "label": label,
524
1092
  "session_files": count, "since": _iso(since), "until": _iso(until),
525
- "prompts": 0, "days": [],
1093
+ "prompts": 0, "human_prompts": 0, "days": [],
526
1094
  })
527
1095
  sys.exit(0)
528
1096
  print("No user prompts found in the selected range.")
@@ -535,12 +1103,14 @@ def analyze_single(provider, project_path, idle_threshold, since, until, as_json
535
1103
  last_ts = timestamps[-1]
536
1104
  span_days = (last_ts.date() - first_ts.date()).days + 1
537
1105
 
538
- prompts_per_day = defaultdict(int)
539
- for ts in timestamps:
540
- prompts_per_day[ts.date()] += 1
1106
+ prompts_per_day = _per_day(timestamps)
1107
+ human_per_day = _per_day(human)
1108
+
1109
+ token_summary = summarize_tokens(events) if show_tokens else None
1110
+ token_days = tokens_by_day(events) if show_tokens else {}
541
1111
 
542
1112
  if as_json:
543
- _print_json({
1113
+ payload = {
544
1114
  "provider": provider,
545
1115
  "project": project_path,
546
1116
  "label": label,
@@ -552,16 +1122,19 @@ def analyze_single(provider, project_path, idle_threshold, since, until, as_json
552
1122
  "last": _iso(last_ts),
553
1123
  "span_days": span_days,
554
1124
  "prompts": len(timestamps),
1125
+ "human_prompts": len(human),
555
1126
  "sessions": len(sessions),
556
1127
  "active_days": len(daily),
557
1128
  "total_minutes": round(total_minutes, 2),
558
1129
  "days": [
559
- {"date": str(day),
560
- "minutes": round(daily[day], 2),
561
- "prompts": prompts_per_day[day]}
562
- for day in sorted(daily)
1130
+ _day_entry(day, daily, prompts_per_day, human_per_day,
1131
+ token_days, show_tokens)
1132
+ for day in _day_keys(daily, token_days)
563
1133
  ],
564
- })
1134
+ }
1135
+ if show_tokens:
1136
+ payload["tokens"] = token_summary
1137
+ _print_json(payload)
565
1138
  return
566
1139
 
567
1140
  print(f"✅ Found {count} session file(s) for: {label}\n")
@@ -573,49 +1146,69 @@ def analyze_single(provider, project_path, idle_threshold, since, until, as_json
573
1146
  print(f" First : {first_ts.strftime('%Y-%m-%d %H:%M')}")
574
1147
  print(f" Last : {last_ts.strftime('%Y-%m-%d %H:%M')}")
575
1148
  print(f" Span : {span_days} calendar days")
576
- print(f" Prompts : {len(timestamps)} total")
1149
+ print(f" Prompts : {len(human)} total")
577
1150
  print(f" Sessions: {len(sessions)} (idle threshold: {idle_threshold} min)")
578
1151
  print(f" Active : {len(daily)} days")
579
1152
  print(f" Total : {fmt_duration(total_minutes)}")
1153
+ if show_tokens:
1154
+ _print_token_lines(token_summary)
580
1155
  print("=" * 52)
581
1156
 
582
- print(f"\n{'DATE':<14} {'ACTIVE TIME':<14} {'PROMPTS':<10} BAR")
583
- print("-" * 52)
1157
+ token_col = f" {'TOKENS':>9}" if show_tokens else ""
1158
+ print(f"\n{'DATE':<14} {'ACTIVE TIME':<14} {'PROMPTS':<10}{token_col} BAR")
1159
+ print("-" * (52 + len(token_col)))
584
1160
 
585
1161
  max_mins = max(daily.values())
586
- for day in sorted(daily):
1162
+ for day in _day_keys(daily, token_days):
587
1163
  mins = daily[day]
588
1164
  bar_len = int((mins / max_mins) * 20) if max_mins else 0
589
1165
  bar = "█" * bar_len
590
- print(f" {str(day):<12} {fmt_duration(mins):<14} {prompts_per_day[day]:<10} {bar}")
591
-
592
- print("-" * 52)
593
- print(f" {'TOTAL':<12} {fmt_duration(total_minutes):<14} {len(timestamps)}")
1166
+ cell = (f" {fmt_tokens(token_days.get(day, {}).get('total', 0)):>9}"
1167
+ if show_tokens else "")
1168
+ print(f" {str(day):<12} {fmt_duration(mins):<14} "
1169
+ f"{human_per_day[day]:<10}{cell} {bar}")
1170
+
1171
+ print("-" * (52 + len(token_col)))
1172
+ total_cell = f" {fmt_tokens(token_summary['total']):>9}" if show_tokens else ""
1173
+ # rstrip so the plain (no --tokens) row keeps its original width.
1174
+ print(f" {'TOTAL':<12} {fmt_duration(total_minutes):<14} "
1175
+ f"{len(human):<10}{total_cell}".rstrip())
1176
+ if show_tokens:
1177
+ _print_model_table(token_summary)
594
1178
  print()
595
1179
 
596
1180
 
597
- def analyze_all(provider, idle_threshold, since, until, as_json):
598
- projects = PROVIDERS[provider]["all"]()
1181
+ def analyze_all(provider, idle_threshold, since, until, as_json,
1182
+ show_tokens=False):
1183
+ projects = _load_projects(provider, show_tokens)
599
1184
 
600
1185
  rows = []
1186
+ all_events = []
601
1187
  grand_minutes = 0.0
602
1188
  grand_prompts = 0
1189
+ grand_human = 0
603
1190
  grand_sessions = 0
604
1191
 
605
- for label, timestamps in projects:
1192
+ for label, timestamps, events, human in projects:
606
1193
  timestamps = _filter_timestamps(timestamps, since, until)
607
1194
  if not timestamps:
608
1195
  continue
1196
+ human = _filter_timestamps(human, since, until)
1197
+ events = _filter_events(events, since, until)
609
1198
  daily, sessions = compute_active_hours(timestamps, idle_threshold)
610
1199
  total_minutes = sum(daily.values())
611
1200
  rows.append({
612
1201
  "label": label,
613
1202
  "minutes": total_minutes,
614
1203
  "prompts": len(timestamps),
1204
+ "human_prompts": len(human),
615
1205
  "sessions": len(sessions),
1206
+ "tokens": summarize_tokens(events) if show_tokens else None,
616
1207
  })
1208
+ all_events.extend(events)
617
1209
  grand_minutes += total_minutes
618
1210
  grand_prompts += len(timestamps)
1211
+ grand_human += len(human)
619
1212
  grand_sessions += len(sessions)
620
1213
 
621
1214
  if not rows:
@@ -623,60 +1216,96 @@ def analyze_all(provider, idle_threshold, since, until, as_json):
623
1216
  _print_json({
624
1217
  "provider": provider, "since": _iso(since), "until": _iso(until),
625
1218
  "projects": [],
626
- "totals": {"projects": 0, "minutes": 0, "prompts": 0, "sessions": 0},
1219
+ "totals": {"projects": 0, "minutes": 0, "prompts": 0,
1220
+ "human_prompts": 0, "sessions": 0},
627
1221
  })
628
1222
  sys.exit(1)
629
1223
  print(f"❌ No {provider} projects with activity found ({PROVIDERS[provider]['location']})")
630
1224
  sys.exit(1)
631
1225
 
632
1226
  rows.sort(key=lambda r: r["minutes"], reverse=True)
1227
+ grand_tokens = summarize_tokens(all_events) if show_tokens else None
633
1228
 
634
1229
  if as_json:
1230
+ projects_json = []
1231
+ for r in rows:
1232
+ entry = {"project": r["label"],
1233
+ "minutes": round(r["minutes"], 2),
1234
+ "prompts": r["prompts"],
1235
+ "human_prompts": r["human_prompts"],
1236
+ "sessions": r["sessions"]}
1237
+ if show_tokens:
1238
+ entry["tokens"] = r["tokens"]
1239
+ projects_json.append(entry)
1240
+ totals = {
1241
+ "projects": len(rows),
1242
+ "minutes": round(grand_minutes, 2),
1243
+ "prompts": grand_prompts,
1244
+ "human_prompts": grand_human,
1245
+ "sessions": grand_sessions,
1246
+ }
1247
+ if show_tokens:
1248
+ totals["tokens"] = grand_tokens
635
1249
  _print_json({
636
1250
  "provider": provider,
637
1251
  "idle_threshold_min": idle_threshold,
638
1252
  "since": _iso(since),
639
1253
  "until": _iso(until),
640
- "projects": [
641
- {"project": r["label"],
642
- "minutes": round(r["minutes"], 2),
643
- "prompts": r["prompts"],
644
- "sessions": r["sessions"]}
645
- for r in rows
646
- ],
647
- "totals": {
648
- "projects": len(rows),
649
- "minutes": round(grand_minutes, 2),
650
- "prompts": grand_prompts,
651
- "sessions": grand_sessions,
652
- },
1254
+ "projects": projects_json,
1255
+ "totals": totals,
653
1256
  })
654
1257
  return
655
1258
 
656
1259
  max_mins = max(r["minutes"] for r in rows)
1260
+ width = 88 if show_tokens else 78
657
1261
 
658
- print("=" * 78)
1262
+ print("=" * width)
659
1263
  print(f" CLOCKWORK — ALL {_provider_label(provider)} PROJECTS (idle threshold: {idle_threshold} min)")
660
- print("=" * 78)
1264
+ print("=" * width)
661
1265
  print(f" Projects: {len(rows)} with activity")
662
- print(f" Prompts : {grand_prompts} total")
1266
+ print(f" Prompts : {grand_human} total")
663
1267
  print(f" Sessions: {grand_sessions} total")
664
1268
  print(f" Total : {fmt_duration(grand_minutes)}")
665
- print("=" * 78)
1269
+ if show_tokens:
1270
+ _print_token_lines(grand_tokens)
1271
+ print("=" * width)
666
1272
  print()
667
- print(f" {'PROJECT':<40} {'TIME':>9} {'PROMPTS':>8} {'SESS':>5} BAR")
668
- print("-" * 78)
1273
+
1274
+ if show_tokens:
1275
+ # Token columns replace the bar — the numbers carry the comparison.
1276
+ print(f" {'PROJECT':<30} {'TIME':>9} {'PROMPTS':>8} {'IN':>8} "
1277
+ f"{'OUT':>8} {'CACHE':>9} {'TOTAL':>9}")
1278
+ else:
1279
+ print(f" {'PROJECT':<40} {'TIME':>9} {'PROMPTS':>8} {'SESS':>5} BAR")
1280
+ print("-" * width)
669
1281
 
670
1282
  for r in rows:
671
1283
  name = r["label"]
672
- if len(name) > 39:
673
- name = "…" + name[-38:]
674
- bar_len = int((r["minutes"] / max_mins) * 12) if max_mins else 0
675
- bar = "█" * bar_len
676
- print(f" {name:<40} {fmt_duration(r['minutes']):>9} {r['prompts']:>8} {r['sessions']:>5} {bar}")
1284
+ if show_tokens:
1285
+ if len(name) > 29:
1286
+ name = "…" + name[-28:]
1287
+ t = r["tokens"]
1288
+ print(f" {name:<30} {fmt_duration(r['minutes']):>9} {r['human_prompts']:>8} "
1289
+ f"{fmt_tokens(t['input']):>8} {fmt_tokens(t['output']):>8} "
1290
+ f"{fmt_tokens(t['cache_read'] + t['cache_write']):>9} "
1291
+ f"{fmt_tokens(t['total']):>9}")
1292
+ else:
1293
+ if len(name) > 39:
1294
+ name = "…" + name[-38:]
1295
+ bar_len = int((r["minutes"] / max_mins) * 12) if max_mins else 0
1296
+ bar = "█" * bar_len
1297
+ print(f" {name:<40} {fmt_duration(r['minutes']):>9} {r['human_prompts']:>8} {r['sessions']:>5} {bar}")
677
1298
 
678
- print("-" * 78)
679
- print(f" {'TOTAL':<40} {fmt_duration(grand_minutes):>9} {grand_prompts:>8} {grand_sessions:>5}")
1299
+ print("-" * width)
1300
+ if show_tokens:
1301
+ print(f" {'TOTAL':<30} {fmt_duration(grand_minutes):>9} {grand_human:>8} "
1302
+ f"{fmt_tokens(grand_tokens['input']):>8} "
1303
+ f"{fmt_tokens(grand_tokens['output']):>8} "
1304
+ f"{fmt_tokens(grand_tokens['cache_read'] + grand_tokens['cache_write']):>9} "
1305
+ f"{fmt_tokens(grand_tokens['total']):>9}")
1306
+ _print_model_table(grand_tokens)
1307
+ else:
1308
+ print(f" {'TOTAL':<40} {fmt_duration(grand_minutes):>9} {grand_human:>8} {grand_sessions:>5}")
680
1309
  print()
681
1310
 
682
1311
 
@@ -694,64 +1323,84 @@ def _period_window(period):
694
1323
  return start_today - timedelta(days=6), now, "THIS WEEK (last 7 days)"
695
1324
 
696
1325
 
697
- def analyze_summary(provider, period, idle_threshold, as_json):
1326
+ def analyze_summary(provider, period, idle_threshold, as_json, show_tokens=False):
698
1327
  since, until, title = _period_window(period)
699
- projects = PROVIDERS[provider]["all"]()
1328
+ projects = _load_projects(provider, show_tokens)
700
1329
 
701
1330
  rows = []
1331
+ all_events = []
702
1332
  combined_daily = defaultdict(float)
703
1333
  prompts_per_day = defaultdict(int)
1334
+ human_per_day = defaultdict(int)
704
1335
  grand_minutes = 0.0
705
1336
  grand_prompts = 0
1337
+ grand_human = 0
706
1338
  grand_sessions = 0
707
1339
 
708
- for label, timestamps in projects:
1340
+ for label, timestamps, events, human in projects:
709
1341
  timestamps = _filter_timestamps(timestamps, since, until)
710
1342
  if not timestamps:
711
1343
  continue
1344
+ human = _filter_timestamps(human, since, until)
1345
+ events = _filter_events(events, since, until)
712
1346
  daily, sessions = compute_active_hours(timestamps, idle_threshold)
713
1347
  rows.append({
714
1348
  "label": label,
715
1349
  "minutes": sum(daily.values()),
716
1350
  "prompts": len(timestamps),
1351
+ "human_prompts": len(human),
717
1352
  "sessions": len(sessions),
1353
+ "tokens": summarize_tokens(events) if show_tokens else None,
718
1354
  })
1355
+ all_events.extend(events)
719
1356
  for day, mins in daily.items():
720
1357
  combined_daily[day] += mins
721
1358
  for ts in timestamps:
722
1359
  prompts_per_day[ts.date()] += 1
1360
+ for ts in human:
1361
+ human_per_day[ts.date()] += 1
723
1362
  grand_minutes += sum(daily.values())
724
1363
  grand_prompts += len(timestamps)
1364
+ grand_human += len(human)
725
1365
  grand_sessions += len(sessions)
726
1366
 
727
1367
  rows.sort(key=lambda r: r["minutes"], reverse=True)
1368
+ grand_tokens = summarize_tokens(all_events) if show_tokens else None
1369
+ token_days = tokens_by_day(all_events) if show_tokens else {}
728
1370
 
729
1371
  if as_json:
1372
+ projects_json = []
1373
+ for r in rows:
1374
+ entry = {"project": r["label"],
1375
+ "minutes": round(r["minutes"], 2),
1376
+ "prompts": r["prompts"],
1377
+ "human_prompts": r["human_prompts"],
1378
+ "sessions": r["sessions"]}
1379
+ if show_tokens:
1380
+ entry["tokens"] = r["tokens"]
1381
+ projects_json.append(entry)
1382
+ totals = {
1383
+ "projects": len(rows),
1384
+ "minutes": round(grand_minutes, 2),
1385
+ "prompts": grand_prompts,
1386
+ "human_prompts": grand_human,
1387
+ "sessions": grand_sessions,
1388
+ }
1389
+ if show_tokens:
1390
+ totals["tokens"] = grand_tokens
730
1391
  _print_json({
731
1392
  "provider": provider,
732
1393
  "period": period,
733
1394
  "idle_threshold_min": idle_threshold,
734
1395
  "since": _iso(since),
735
1396
  "until": _iso(until),
736
- "projects": [
737
- {"project": r["label"],
738
- "minutes": round(r["minutes"], 2),
739
- "prompts": r["prompts"],
740
- "sessions": r["sessions"]}
741
- for r in rows
742
- ],
1397
+ "projects": projects_json,
743
1398
  "days": [
744
- {"date": str(day),
745
- "minutes": round(combined_daily[day], 2),
746
- "prompts": prompts_per_day[day]}
747
- for day in sorted(combined_daily)
1399
+ _day_entry(day, combined_daily, prompts_per_day, human_per_day,
1400
+ token_days, show_tokens)
1401
+ for day in _day_keys(combined_daily, token_days)
748
1402
  ],
749
- "totals": {
750
- "projects": len(rows),
751
- "minutes": round(grand_minutes, 2),
752
- "prompts": grand_prompts,
753
- "sessions": grand_sessions,
754
- },
1403
+ "totals": totals,
755
1404
  })
756
1405
  return
757
1406
 
@@ -765,35 +1414,61 @@ def analyze_summary(provider, period, idle_threshold, as_json):
765
1414
  print()
766
1415
  return
767
1416
  print(f" Active : {fmt_duration(grand_minutes)} across {len(rows)} project(s)")
768
- print(f" Prompts : {grand_prompts} Sessions: {grand_sessions}")
1417
+ print(f" Prompts : {grand_human} Sessions: {grand_sessions}")
1418
+ if show_tokens:
1419
+ print(f" Tokens : {fmt_tokens(grand_tokens['total'])} across "
1420
+ f"{grand_tokens['responses']:,} responses")
1421
+ print(f" In/Out : {fmt_tokens(grand_tokens['input'])} in · "
1422
+ f"{fmt_tokens(grand_tokens['output'])} out · "
1423
+ f"{fmt_tokens(grand_tokens['cache_read'] + grand_tokens['cache_write'])} cache")
769
1424
  print("=" * width)
770
1425
  print()
771
1426
 
772
- print(f" {'PROJECT':<34} {'TIME':>9} {'PROMPTS':>7} BAR")
1427
+ if show_tokens:
1428
+ print(f" {'PROJECT':<30} {'TIME':>9} {'PROMPTS':>7} {'TOKENS':>9}")
1429
+ else:
1430
+ print(f" {'PROJECT':<34} {'TIME':>9} {'PROMPTS':>7} BAR")
773
1431
  print("-" * width)
774
1432
  max_mins = max(r["minutes"] for r in rows)
775
1433
  for r in rows:
776
1434
  name = r["label"]
777
- if len(name) > 33:
778
- name = "…" + name[-32:]
779
- bar = "█" * (int((r["minutes"] / max_mins) * 10) if max_mins else 0)
780
- print(f" {name:<34} {fmt_duration(r['minutes']):>9} {r['prompts']:>7} {bar}")
1435
+ if show_tokens:
1436
+ if len(name) > 29:
1437
+ name = "…" + name[-28:]
1438
+ print(f" {name:<30} {fmt_duration(r['minutes']):>9} "
1439
+ f"{r['human_prompts']:>7} {fmt_tokens(r['tokens']['total']):>9}")
1440
+ else:
1441
+ if len(name) > 33:
1442
+ name = "…" + name[-32:]
1443
+ bar = "█" * (int((r["minutes"] / max_mins) * 10) if max_mins else 0)
1444
+ print(f" {name:<34} {fmt_duration(r['minutes']):>9} {r['human_prompts']:>7} {bar}")
781
1445
  print("-" * width)
782
- print(f" {'TOTAL':<34} {fmt_duration(grand_minutes):>9} {grand_prompts:>7}")
1446
+ if show_tokens:
1447
+ print(f" {'TOTAL':<30} {fmt_duration(grand_minutes):>9} "
1448
+ f"{grand_human:>7} {fmt_tokens(grand_tokens['total']):>9}")
1449
+ else:
1450
+ print(f" {'TOTAL':<34} {fmt_duration(grand_minutes):>9} {grand_human:>7}")
783
1451
 
784
1452
  if period == "week":
785
1453
  print()
786
- print(f" {'DAY':<12} {'TIME':>9} {'PROMPTS':>7} BAR")
1454
+ token_col = f" {'TOKENS':>9}" if show_tokens else ""
1455
+ print(f" {'DAY':<12} {'TIME':>9} {'PROMPTS':>7}{token_col} BAR")
787
1456
  print("-" * width)
788
1457
  max_day = max(combined_daily.values())
789
- for day in sorted(combined_daily):
1458
+ for day in _day_keys(combined_daily, token_days):
790
1459
  mins = combined_daily[day]
791
1460
  bar = "█" * (int((mins / max_day) * 10) if max_day else 0)
792
- print(f" {str(day):<12} {fmt_duration(mins):>9} {prompts_per_day[day]:>7} {bar}")
1461
+ cell = (f" {fmt_tokens(token_days.get(day, {}).get('total', 0)):>9}"
1462
+ if show_tokens else "")
1463
+ print(f" {str(day):<12} {fmt_duration(mins):>9} "
1464
+ f"{human_per_day[day]:>7}{cell} {bar}")
1465
+ if show_tokens:
1466
+ _print_model_table(grand_tokens)
793
1467
  print()
794
1468
 
795
1469
 
796
- def export_data(provider, idle_threshold, since, until, detail, anonymize):
1470
+ def export_data(provider, idle_threshold, since, until, detail, anonymize,
1471
+ show_tokens=False):
797
1472
  """Emit one versioned JSON bundle of every project for external tools.
798
1473
 
799
1474
  Always writes JSON to stdout. `detail` selects granularity:
@@ -811,32 +1486,48 @@ def export_data(provider, idle_threshold, since, until, detail, anonymize):
811
1486
  daily, sessions, and prompts. Top-level `provider` is "both" when more than
812
1487
  one provider contributed; `providers` lists them, and `totals.by_provider`
813
1488
  breaks the grand totals down per provider.
1489
+
1490
+ With `show_tokens`, every project, day, provider and the grand totals also
1491
+ carry a normalized `tokens` record with a per-model breakdown. The token
1492
+ fields are purely additive — a v2 consumer reads a v3 bundle unchanged.
1493
+
1494
+ `prompts` counts every user-role record, tool results included, and keeps
1495
+ that meaning for existing consumers. Wherever it appears (totals, daily,
1496
+ sessions, by_provider, and the raw timestamp list) a `human_prompts`
1497
+ sibling carries the messages a person actually sent. Also additive.
814
1498
  """
815
1499
  out_projects = []
816
1500
  by_provider = {}
817
1501
  grand_minutes = 0.0
818
1502
  grand_prompts = 0
1503
+ grand_human = 0
819
1504
  grand_sessions = 0
1505
+ grand_events = []
820
1506
  name_counter = 0
821
1507
 
822
1508
  for prov in _export_providers(provider):
823
- projects = PROVIDERS[prov]["all"]()
1509
+ projects = _load_projects(prov, show_tokens)
824
1510
  p_minutes = 0.0
825
1511
  p_prompts = 0
1512
+ p_human = 0
826
1513
  p_sessions = 0
827
1514
  p_projects = 0
1515
+ p_events = []
828
1516
 
829
- for label, timestamps in sorted(projects, key=lambda p: p[0]):
1517
+ for label, timestamps, events, human in sorted(projects, key=lambda p: p[0]):
830
1518
  timestamps = _filter_timestamps(timestamps, since, until)
831
1519
  if not timestamps:
832
1520
  continue
1521
+ human = _filter_timestamps(human, since, until)
1522
+ events = _filter_events(events, since, until)
833
1523
  daily, _ = compute_active_hours(timestamps, idle_threshold)
834
1524
  sessions = group_sessions(timestamps, idle_threshold)
835
1525
  total_minutes = sum(daily.values())
836
1526
 
837
- prompts_per_day = defaultdict(int)
838
- for ts in timestamps:
839
- prompts_per_day[ts.date()] += 1
1527
+ prompts_per_day = _per_day(timestamps)
1528
+ human_per_day = _per_day(human)
1529
+
1530
+ token_days = tokens_by_day(events) if show_tokens else {}
840
1531
 
841
1532
  name_counter += 1
842
1533
  proj = {
@@ -846,18 +1537,20 @@ def export_data(provider, idle_threshold, since, until, detail, anonymize):
846
1537
  "totals": {
847
1538
  "minutes": round(total_minutes, 2),
848
1539
  "prompts": len(timestamps),
1540
+ "human_prompts": len(human),
849
1541
  "sessions": len(sessions),
850
1542
  "active_days": len(daily),
851
1543
  "first": int(timestamps[0].timestamp()),
852
1544
  "last": int(timestamps[-1].timestamp()),
853
1545
  },
854
1546
  "daily": [
855
- {"date": str(day),
856
- "minutes": round(daily[day], 2),
857
- "prompts": prompts_per_day[day]}
858
- for day in sorted(daily)
1547
+ _day_entry(day, daily, prompts_per_day, human_per_day,
1548
+ token_days, show_tokens)
1549
+ for day in _day_keys(daily, token_days)
859
1550
  ],
860
1551
  }
1552
+ if show_tokens:
1553
+ proj["tokens"] = summarize_tokens(events)
861
1554
  if not anonymize:
862
1555
  proj["path"] = label
863
1556
  if detail in ("sessions", "raw"):
@@ -865,28 +1558,38 @@ def export_data(provider, idle_threshold, since, until, detail, anonymize):
865
1558
  {"start": int(s.timestamp()),
866
1559
  "end": int(e.timestamp()),
867
1560
  "minutes": round((e - s).total_seconds() / 60, 2),
868
- "prompts": c}
869
- for s, e, c in sessions
1561
+ "prompts": c,
1562
+ "human_prompts": h}
1563
+ for (s, e, c), h in zip(sessions,
1564
+ _human_per_session(sessions, human))
870
1565
  ]
871
1566
  if detail == "raw":
872
1567
  proj["prompts"] = [int(ts.timestamp()) for ts in timestamps]
1568
+ proj["human_prompts"] = [int(ts.timestamp()) for ts in human]
873
1569
 
874
1570
  out_projects.append(proj)
875
1571
  p_projects += 1
876
1572
  p_minutes += total_minutes
877
1573
  p_prompts += len(timestamps)
1574
+ p_human += len(human)
878
1575
  p_sessions += len(sessions)
1576
+ p_events.extend(events)
879
1577
 
880
1578
  if p_projects:
881
1579
  by_provider[prov] = {
882
1580
  "projects": p_projects,
883
1581
  "minutes": round(p_minutes, 2),
884
1582
  "prompts": p_prompts,
1583
+ "human_prompts": p_human,
885
1584
  "sessions": p_sessions,
886
1585
  }
1586
+ if show_tokens:
1587
+ by_provider[prov]["tokens"] = summarize_tokens(p_events)
887
1588
  grand_minutes += p_minutes
888
1589
  grand_prompts += p_prompts
1590
+ grand_human += p_human
889
1591
  grand_sessions += p_sessions
1592
+ grand_events.extend(p_events)
890
1593
 
891
1594
  # Report the providers that actually contributed activity; fall back to the
892
1595
  # requested set so a single-provider export is never emitted with an empty
@@ -895,25 +1598,32 @@ def export_data(provider, idle_threshold, since, until, detail, anonymize):
895
1598
  providers = present if present else sorted(_export_providers(provider))
896
1599
  provider_field = "both" if len(providers) > 1 else providers[0]
897
1600
 
1601
+ grand_tokens = summarize_tokens(grand_events) if show_tokens else None
1602
+ totals = {
1603
+ "projects": len(out_projects),
1604
+ "minutes": round(grand_minutes, 2),
1605
+ "prompts": grand_prompts,
1606
+ "human_prompts": grand_human,
1607
+ "sessions": grand_sessions,
1608
+ "by_provider": by_provider,
1609
+ }
1610
+ if show_tokens:
1611
+ totals["tokens"] = grand_tokens
1612
+
898
1613
  _print_json({
899
- "schema": "clockwork/v2",
1614
+ "schema": "clockwork/v3",
900
1615
  "generated_at": datetime.now().astimezone().isoformat(),
901
1616
  "provider": provider_field,
902
1617
  "providers": providers,
903
1618
  "idle_threshold_min": idle_threshold,
904
1619
  "detail": detail,
905
1620
  "anonymized": anonymize,
1621
+ "tokens": show_tokens,
906
1622
  "daily_tz": "UTC",
907
1623
  "since": _iso(since),
908
1624
  "until": _iso(until),
909
1625
  "projects": out_projects,
910
- "totals": {
911
- "projects": len(out_projects),
912
- "minutes": round(grand_minutes, 2),
913
- "prompts": grand_prompts,
914
- "sessions": grand_sessions,
915
- "by_provider": by_provider,
916
- },
1626
+ "totals": totals,
917
1627
  })
918
1628
 
919
1629
  # A friendly receipt on stderr — keeps stdout pure JSON for redirection.
@@ -922,11 +1632,17 @@ def export_data(provider, idle_threshold, since, until, detail, anonymize):
922
1632
  tags = [provider_field, f"detail={detail}"]
923
1633
  if anonymize:
924
1634
  tags.append("anonymized")
1635
+ if show_tokens:
1636
+ tags.append("tokens")
925
1637
  print(f"\n ✓ export complete → {where}", file=sys.stderr)
926
1638
  print(f" {' · '.join(tags)}", file=sys.stderr)
927
- print(f" {len(out_projects)} projects · {grand_prompts:,} prompts "
1639
+ print(f" {len(out_projects)} projects · {grand_human:,} prompts "
928
1640
  f"· {grand_sessions:,} sessions · {grand_minutes / 60:.1f}h active",
929
1641
  file=sys.stderr)
1642
+ if show_tokens:
1643
+ print(f" {grand_tokens['total']:,} tokens · "
1644
+ f"{grand_tokens['responses']:,} responses · "
1645
+ f"{len(grand_tokens['by_model'])} model(s)", file=sys.stderr)
930
1646
 
931
1647
 
932
1648
  def list_projects(provider, as_json):
@@ -955,7 +1671,7 @@ def _die(msg):
955
1671
  def _parse_args(argv):
956
1672
  """Split flags from positionals. Returns (positionals, opts)."""
957
1673
  opts = {"json": False, "since": None, "until": None,
958
- "detail": "raw", "anonymize": False}
1674
+ "detail": "raw", "anonymize": False, "tokens": False}
959
1675
  positionals = []
960
1676
  i = 0
961
1677
  while i < len(argv):
@@ -964,6 +1680,8 @@ def _parse_args(argv):
964
1680
  opts["json"] = True
965
1681
  elif a == "--anonymize":
966
1682
  opts["anonymize"] = True
1683
+ elif a == "--tokens":
1684
+ opts["tokens"] = True
967
1685
  elif a in ("--since", "--until", "--detail"):
968
1686
  i += 1
969
1687
  if i >= len(argv):
@@ -1030,13 +1748,16 @@ def main():
1030
1748
  _die(str(e))
1031
1749
 
1032
1750
  if target == "all":
1033
- analyze_all(provider, idle_threshold, since, until, opts["json"])
1751
+ analyze_all(provider, idle_threshold, since, until, opts["json"],
1752
+ opts["tokens"])
1034
1753
  elif target in ("today", "week"):
1035
- analyze_summary(provider, target, idle_threshold, opts["json"])
1754
+ analyze_summary(provider, target, idle_threshold, opts["json"],
1755
+ opts["tokens"])
1036
1756
  elif target == "export":
1037
1757
  export_data(provider, idle_threshold, since, until,
1038
- opts["detail"], opts["anonymize"])
1758
+ opts["detail"], opts["anonymize"], opts["tokens"])
1039
1759
  elif target == "list":
1040
1760
  list_projects(provider, opts["json"])
1041
1761
  else:
1042
- analyze_single(provider, target, idle_threshold, since, until, opts["json"])
1762
+ analyze_single(provider, target, idle_threshold, since, until,
1763
+ opts["json"], opts["tokens"])