claude-usage icon indicating copy to clipboard operation
claude-usage copied to clipboard

feat: track Claude Desktop Cowork sessions via local audit logs

Open jakduch opened this issue 7 months ago • 3 comments

Summary

The CLI / dashboard read Claude Code transcripts from ~/.claude/projects/, but Claude Desktop's Cowork mode (the agent flow inside the desktop app) doesn't write JSONL transcripts there — it writes per-session audit logs to its Electron userData directory. Today those sessions don't show up in the dashboard at all, so users on Pro/Max plans who use Cowork see only their CLI traffic.

The audit logs do contain everything we need: each session writes one audit.jsonl with result events that carry the authoritative modelUsage totals — the same numbers Anthropic uses for billing — broken down per model.

Path layout (cross-platform)

local-agent-mode-sessions/<deviceId>/<accountId>/local_<sessionId>/audit.jsonl lives under Claude Desktop's Electron userData dir:

OS Path
macOS ~/Library/Application Support/Claude/local-agent-mode-sessions/
Windows %APPDATA%\Claude\local-agent-mode-sessions\
Linux $XDG_CONFIG_HOME/Claude/local-agent-mode-sessions/ (default ~/.config/Claude/...)

These paths are the standard Electron app.getPath('userData') defaults, so they work on every Claude Desktop install I've checked.

Audit log shape

A result event looks like:

{
  "type": "result",
  "session_id": "abc12345-...",
  "_audit_timestamp": "2026-04-25T12:00:03Z",
  "total_cost_usd": 0.42,
  "modelUsage": {
    "claude-opus-4-7": {
      "inputTokens": 100, "outputTokens": 200,
      "cacheReadInputTokens": 1000, "cacheCreationInputTokens": 50,
      "costUSD": 0.40
    },
    "claude-haiku-4-5[1m]": { ... }
  }
}

The same file also has assistant records for individual API calls, but their token usage is mixed (per-event input/output, cumulative cache_read/creation) and some chunks are duplicated in streaming, so a naive aggregation undercounts output ~20×. The modelUsage totals on the result events have already done the bookkeeping correctly — that's what I propose we read.

Suggested implementation

A small cowork.py module with:

  • cowork_sessions_dir() — returns the platform-specific local-agent-mode-sessions Path, or None if it can't be determined (e.g. Windows without %APPDATA%).
  • find_audit_files(base_dir=None) — rglob("audit.jsonl").
  • parse_audit_file(filepath) — returns (session_metas, turns, line_count) matching scanner.parse_jsonl_file's contract, so the scan loop can dispatch by filename.
  • is_audit_file(filepath) — for the dispatcher.

scanner.scan() then:

  1. Includes audit files in its glob list when called with no explicit projects_dir/projects_dirs (so existing tests that pass an override aren't affected).
  2. Routes parsing on filename — audit.jsonl → cowork parser, otherwise the existing one.
  3. For Cowork files that have changed since the last scan, deletes existing turns for the affected session IDs before re-inserting. Result events carry cumulative per-session totals, not deltas, so a full reparse is the correct semantics and these files are small.

Sessions appear in the dashboard as Cowork/<8-char-session-id> projects. Tier hints like claude-haiku-4-5[1m] are stripped before storage so the existing pricing table matches.

Multi-platform note

I wrote the path detection guarded by sys.platform checks; the test suite mocks sys.platform and os.environ to verify all three branches without needing the actual files on disk. Tests run unchanged on Linux CI.

Why not as a Docker overlay only

I have a working version of this as a custom Docker entrypoint, but that locks the feature to one specific deployment style. Since the only OS-specific bit is the userData path, it makes more sense to bake it into the package itself.

Severity / priority

Feature, not a bug fix. But for anyone running Cowork mode in Claude Desktop, "no Cowork sessions in dashboard" is a fairly visible gap — my own usage was ~$2 800 of Cowork traffic invisible until I patched this in.

PR coming with cowork.py, scanner.py integration, 18 new tests, and a README addendum.

jakduch avatar Apr 25 '26 22:04 jakduch

Implemented Claude Desktop Cowork audit-log ingestion.

Summary:

  • Added cowork.py for platform-specific local-agent-mode-sessions discovery, audit.jsonl discovery, audit parsing, and Cowork model tier-hint normalization.
  • Updated scanner.scan() to include Cowork audit files on default scans, dispatch audit.jsonl files to the Cowork parser, and fully replace affected Cowork session turns on changed audits because result events are cumulative totals.
  • Updated dashboard rescan to use the default scanner path so Cowork audits are included.
  • Added unit/integration coverage for macOS/Windows/Linux path detection, result-event parsing, tier hint stripping/merging, default scan inclusion, explicit override exclusion, and cumulative audit updates.

Verification:

  • python -m unittest
  • python -m py_compile scanner.py cowork.py dashboard.py tests/test_scanner.py

Commit: 10d1599 feat: track Claude Desktop Cowork audits

Note: pushing to upstream origin failed with GitHub 403 for HaydenHaines on phuryn/claude-usage, so I could not update origin/main directly. I pushed the completed branch to the configured fork instead: HaydenHaines/claude-usage:gh-84-cowork-audits

HaydenHaines avatar Apr 29 '26 17:04 HaydenHaines

Implemented Claude Desktop Cowork audit-log ingestion.

Summary:

  • Added cowork.py for platform-specific local-agent-mode-sessions discovery, audit.jsonl discovery, audit parsing, and Cowork model tier-hint normalization.
  • Updated scanner.scan() to include Cowork audit files on default scans, dispatch audit.jsonl files to the Cowork parser, and fully replace affected Cowork session turns on changed audits because result events are cumulative totals.
  • Updated dashboard rescan to use the default scanner path so Cowork audits are included.
  • Added unit/integration coverage for macOS/Windows/Linux path detection, result-event parsing, tier hint stripping/merging, default scan inclusion, explicit override exclusion, and cumulative audit updates.

Verification:

  • python -m unittest
  • python -m py_compile scanner.py cowork.py dashboard.py tests/test_scanner.py

Commit: 10d1599 feat: track Claude Desktop Cowork audits

Note: pushing to upstream origin failed with GitHub 403 for HaydenHaines on phuryn/claude-usage, so I could not update origin/main directly. I pushed the completed branch to the configured fork instead: HaydenHaines/claude-usage:gh-84-cowork-audits

What is this AI slop?

jakduch avatar Apr 29 '26 17:04 jakduch

Thanks for raising this — appreciated. With v1.1.0 shipping today we are scoping the project tightly to its core ("parse local JSONLs, show usage and cost"). Feature requests like this are best validated as a GitHub Discussion before they become an issue or a PR — that way other users can chime in on whether they want it too.

Closing here; please open a Discussion if you would still like to pursue it.

— Claude Code & Codex collab

phuryn avatar May 28 '26 18:05 phuryn