tokenwise-cli 0.1.0__tar.gz → 0.1.2__tar.gz
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.
- tokenwise_cli-0.1.2/.claude/settings.local.json +11 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/.github/workflows/ci.yml +3 -3
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/PKG-INFO +14 -32
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/README.md +10 -28
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/pyproject.toml +3 -3
- tokenwise_cli-0.1.2/src/tokenwise/__init__.py +1 -0
- tokenwise_cli-0.1.2/src/tokenwise/analyzer.py +240 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/src/tokenwise/cli.py +27 -18
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/src/tokenwise/dashboard.py +6 -4
- tokenwise_cli-0.1.2/src/tokenwise/parser.py +269 -0
- tokenwise_cli-0.1.2/src/tokenwise/pricing.py +117 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/src/tokenwise/security.py +2 -2
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/src/tokenwise/storage.py +3 -3
- tokenwise_cli-0.1.2/tests/test_analyzer.py +415 -0
- tokenwise_cli-0.1.2/tests/test_cli.py +207 -0
- tokenwise_cli-0.1.2/tests/test_dashboard.py +313 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/tests/test_parser.py +88 -0
- tokenwise_cli-0.1.2/tests/test_pricing.py +195 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/tests/test_security.py +25 -3
- tokenwise_cli-0.1.2/tests/test_storage.py +168 -0
- tokenwise_cli-0.1.0/src/tokenwise/__init__.py +0 -1
- tokenwise_cli-0.1.0/src/tokenwise/analyzer.py +0 -216
- tokenwise_cli-0.1.0/src/tokenwise/parser.py +0 -166
- tokenwise_cli-0.1.0/src/tokenwise/pricing.py +0 -76
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/.github/workflows/codeql.yml +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/.github/workflows/dependency-review.yml +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/.github/workflows/release.yml +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/.github/workflows/scorecard.yml +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/.gitignore +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/CONTRIBUTING.md +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/LICENSE +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/Makefile +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/SECURITY.md +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/THREAT_MODEL.md +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.2}/tests/__init__.py +0 -0
|
@@ -83,9 +83,9 @@ jobs:
|
|
|
83
83
|
run: bandit -r src/ -c pyproject.toml
|
|
84
84
|
|
|
85
85
|
- name: pip-audit (vulnerability scan)
|
|
86
|
-
# CVE-2026-3219
|
|
87
|
-
#
|
|
88
|
-
run: pip-audit --ignore-vuln CVE-2026-3219
|
|
86
|
+
# CVE-2026-3219 and CVE-2026-6357 affect pip itself; not a tokenwise dependency.
|
|
87
|
+
# --skip-editable excludes the local tokenwise-cli package (not on PyPI).
|
|
88
|
+
run: pip-audit --skip-editable --ignore-vuln CVE-2026-3219 --ignore-vuln CVE-2026-6357
|
|
89
89
|
|
|
90
90
|
- name: Grep check — no network imports
|
|
91
91
|
run: |
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: tokenwise-cli
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Track and reduce your Claude/Anthropic API costs from the terminal
|
|
5
|
-
Project-URL: Homepage, https://github.com/
|
|
6
|
-
Project-URL: Bug Tracker, https://github.com/
|
|
5
|
+
Project-URL: Homepage, https://github.com/S0umith29/tokenwise
|
|
6
|
+
Project-URL: Bug Tracker, https://github.com/S0umith29/tokenwise/issues
|
|
7
7
|
License: MIT License
|
|
8
8
|
|
|
9
9
|
Copyright (c) 2026 Sowmith Kuppa
|
|
@@ -43,16 +43,13 @@ Description-Content-Type: text/markdown
|
|
|
43
43
|
|
|
44
44
|
> **See exactly where your Claude tokens go — and stop wasting them.**
|
|
45
45
|
|
|
46
|
-
[](https://pypi.org/project/tokenwise/)
|
|
50
|
-
[](https://pypi.org/project/tokenwise/)
|
|
46
|
+
[](https://github.com/S0umith29/tokenwise/actions/workflows/ci.yml)
|
|
47
|
+
[](https://github.com/S0umith29/tokenwise/actions/workflows/codeql.yml)
|
|
48
|
+
[](https://securityscorecards.dev/viewer/?uri=github.com/S0umith29/tokenwise)
|
|
49
|
+
[](https://pypi.org/project/tokenwise-cli/)
|
|
50
|
+
[](https://pypi.org/project/tokenwise-cli/)
|
|
51
51
|
[](LICENSE)
|
|
52
52
|
|
|
53
|
-
<!-- GIF placeholder -->
|
|
54
|
-

|
|
55
|
-
|
|
56
53
|
---
|
|
57
54
|
|
|
58
55
|
## Security
|
|
@@ -113,13 +110,13 @@ Zero cloud sync. Zero telemetry. Pure read-only.
|
|
|
113
110
|
## Install
|
|
114
111
|
|
|
115
112
|
```bash
|
|
116
|
-
pip install tokenwise
|
|
113
|
+
pip install tokenwise-cli
|
|
117
114
|
```
|
|
118
115
|
|
|
119
116
|
Or with [pipx](https://pypa.github.io/pipx/) (recommended for CLI tools):
|
|
120
117
|
|
|
121
118
|
```bash
|
|
122
|
-
pipx install tokenwise
|
|
119
|
+
pipx install tokenwise-cli
|
|
123
120
|
```
|
|
124
121
|
|
|
125
122
|
### Requirements
|
|
@@ -133,7 +130,7 @@ python3 --version
|
|
|
133
130
|
**Development setup** (for contributors):
|
|
134
131
|
|
|
135
132
|
```bash
|
|
136
|
-
git clone https://github.com/
|
|
133
|
+
git clone https://github.com/S0umith29/tokenwise.git
|
|
137
134
|
cd tokenwise
|
|
138
135
|
python3.13 -m venv .venv # or python3.10 / 3.11 / 3.12
|
|
139
136
|
source .venv/bin/activate
|
|
@@ -169,24 +166,9 @@ tokenwise export --csv > usage.csv
|
|
|
169
166
|
|
|
170
167
|
## How it works
|
|
171
168
|
|
|
172
|
-
Claude Code writes JSONL logs to `~/.claude/projects/<project-slug>/<session-id>.jsonl
|
|
173
|
-
|
|
174
|
-
Parsed data is cached in `~/.tokenwise/cache.json` (permissions `0o600`) so repeated runs are instant.
|
|
175
|
-
|
|
176
|
-
---
|
|
169
|
+
Claude Code writes JSONL logs to `~/.claude/projects/<project-slug>/<session-id>.jsonl` (plus `<session-id>/subagents/*.jsonl` for subagent runs). Each assistant message includes a `usage` object with `input_tokens`, `output_tokens`, `cache_creation_input_tokens`, and `cache_read_input_tokens`. `tokenwise` parses these, applies current Anthropic pricing per message, and aggregates the results by project and by local calendar day.
|
|
177
170
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
| Feature | tokenwise | ccusage |
|
|
181
|
-
|---|---|---|
|
|
182
|
-
| Live dashboard | ✅ | ❌ |
|
|
183
|
-
| Waste detection | ✅ | ❌ |
|
|
184
|
-
| Cache-aware cost | ✅ | partial |
|
|
185
|
-
| Per-session drill-down | ✅ | ✅ |
|
|
186
|
-
| CSV export | ✅ | ✅ |
|
|
187
|
-
| No API key required | ✅ | ✅ |
|
|
188
|
-
| Formal threat model | ✅ | ❌ |
|
|
189
|
-
| Runtime no-network assertion | ✅ | ❌ |
|
|
171
|
+
Claude Code logs the same message several times (once per content block, and again when a session is resumed), so `tokenwise` counts each message ID only once.
|
|
190
172
|
|
|
191
173
|
---
|
|
192
174
|
|
|
@@ -2,16 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
> **See exactly where your Claude tokens go — and stop wasting them.**
|
|
4
4
|
|
|
5
|
-
[](https://pypi.org/project/tokenwise/)
|
|
9
|
-
[](https://pypi.org/project/tokenwise/)
|
|
5
|
+
[](https://github.com/S0umith29/tokenwise/actions/workflows/ci.yml)
|
|
6
|
+
[](https://github.com/S0umith29/tokenwise/actions/workflows/codeql.yml)
|
|
7
|
+
[](https://securityscorecards.dev/viewer/?uri=github.com/S0umith29/tokenwise)
|
|
8
|
+
[](https://pypi.org/project/tokenwise-cli/)
|
|
9
|
+
[](https://pypi.org/project/tokenwise-cli/)
|
|
10
10
|
[](LICENSE)
|
|
11
11
|
|
|
12
|
-
<!-- GIF placeholder -->
|
|
13
|
-

|
|
14
|
-
|
|
15
12
|
---
|
|
16
13
|
|
|
17
14
|
## Security
|
|
@@ -72,13 +69,13 @@ Zero cloud sync. Zero telemetry. Pure read-only.
|
|
|
72
69
|
## Install
|
|
73
70
|
|
|
74
71
|
```bash
|
|
75
|
-
pip install tokenwise
|
|
72
|
+
pip install tokenwise-cli
|
|
76
73
|
```
|
|
77
74
|
|
|
78
75
|
Or with [pipx](https://pypa.github.io/pipx/) (recommended for CLI tools):
|
|
79
76
|
|
|
80
77
|
```bash
|
|
81
|
-
pipx install tokenwise
|
|
78
|
+
pipx install tokenwise-cli
|
|
82
79
|
```
|
|
83
80
|
|
|
84
81
|
### Requirements
|
|
@@ -92,7 +89,7 @@ python3 --version
|
|
|
92
89
|
**Development setup** (for contributors):
|
|
93
90
|
|
|
94
91
|
```bash
|
|
95
|
-
git clone https://github.com/
|
|
92
|
+
git clone https://github.com/S0umith29/tokenwise.git
|
|
96
93
|
cd tokenwise
|
|
97
94
|
python3.13 -m venv .venv # or python3.10 / 3.11 / 3.12
|
|
98
95
|
source .venv/bin/activate
|
|
@@ -128,24 +125,9 @@ tokenwise export --csv > usage.csv
|
|
|
128
125
|
|
|
129
126
|
## How it works
|
|
130
127
|
|
|
131
|
-
Claude Code writes JSONL logs to `~/.claude/projects/<project-slug>/<session-id>.jsonl
|
|
132
|
-
|
|
133
|
-
Parsed data is cached in `~/.tokenwise/cache.json` (permissions `0o600`) so repeated runs are instant.
|
|
134
|
-
|
|
135
|
-
---
|
|
128
|
+
Claude Code writes JSONL logs to `~/.claude/projects/<project-slug>/<session-id>.jsonl` (plus `<session-id>/subagents/*.jsonl` for subagent runs). Each assistant message includes a `usage` object with `input_tokens`, `output_tokens`, `cache_creation_input_tokens`, and `cache_read_input_tokens`. `tokenwise` parses these, applies current Anthropic pricing per message, and aggregates the results by project and by local calendar day.
|
|
136
129
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
| Feature | tokenwise | ccusage |
|
|
140
|
-
|---|---|---|
|
|
141
|
-
| Live dashboard | ✅ | ❌ |
|
|
142
|
-
| Waste detection | ✅ | ❌ |
|
|
143
|
-
| Cache-aware cost | ✅ | partial |
|
|
144
|
-
| Per-session drill-down | ✅ | ✅ |
|
|
145
|
-
| CSV export | ✅ | ✅ |
|
|
146
|
-
| No API key required | ✅ | ✅ |
|
|
147
|
-
| Formal threat model | ✅ | ❌ |
|
|
148
|
-
| Runtime no-network assertion | ✅ | ❌ |
|
|
130
|
+
Claude Code logs the same message several times (once per content block, and again when a session is resumed), so `tokenwise` counts each message ID only once.
|
|
149
131
|
|
|
150
132
|
---
|
|
151
133
|
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "tokenwise-cli"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.2"
|
|
8
8
|
description = "Track and reduce your Claude/Anthropic API costs from the terminal"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { file = "LICENSE" }
|
|
@@ -36,8 +36,8 @@ dev = [
|
|
|
36
36
|
tokenwise = "tokenwise.cli:main"
|
|
37
37
|
|
|
38
38
|
[project.urls]
|
|
39
|
-
Homepage = "https://github.com/
|
|
40
|
-
"Bug Tracker" = "https://github.com/
|
|
39
|
+
Homepage = "https://github.com/S0umith29/tokenwise"
|
|
40
|
+
"Bug Tracker" = "https://github.com/S0umith29/tokenwise/issues"
|
|
41
41
|
|
|
42
42
|
[tool.hatch.build.targets.wheel]
|
|
43
43
|
packages = ["src/tokenwise"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.2"
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from datetime import date, datetime
|
|
5
|
+
|
|
6
|
+
from .parser import Session
|
|
7
|
+
from .pricing import compute_cost
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def local_date(ts: datetime) -> date:
|
|
11
|
+
"""Calendar day of ``ts`` in the user's local timezone (matches date.today())."""
|
|
12
|
+
return ts.astimezone().date()
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass
|
|
16
|
+
class DayUsage:
|
|
17
|
+
input_tokens: int = 0
|
|
18
|
+
output_tokens: int = 0
|
|
19
|
+
cache_write_tokens: int = 0
|
|
20
|
+
cache_read_tokens: int = 0
|
|
21
|
+
cost_usd: float = 0.0
|
|
22
|
+
turns: int = 0
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass
|
|
26
|
+
class SessionStats:
|
|
27
|
+
session_id: str
|
|
28
|
+
project_slug: str
|
|
29
|
+
project_name: str
|
|
30
|
+
model: str
|
|
31
|
+
start_time: datetime | None
|
|
32
|
+
input_tokens: int
|
|
33
|
+
output_tokens: int
|
|
34
|
+
cache_write_tokens: int
|
|
35
|
+
cache_read_tokens: int
|
|
36
|
+
cost_usd: float
|
|
37
|
+
turns: int
|
|
38
|
+
# Usage split by the local calendar day each turn happened on. A session
|
|
39
|
+
# that runs past midnight counts toward both days.
|
|
40
|
+
daily: dict[date, DayUsage] = field(default_factory=dict)
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def total_tokens(self) -> int:
|
|
44
|
+
return (
|
|
45
|
+
self.input_tokens
|
|
46
|
+
+ self.output_tokens
|
|
47
|
+
+ self.cache_write_tokens
|
|
48
|
+
+ self.cache_read_tokens
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def waste_ratio(self) -> float:
|
|
53
|
+
"""High ratio = lots of context for little output (potentially wasteful)."""
|
|
54
|
+
context = self.input_tokens + self.cache_read_tokens
|
|
55
|
+
if context == 0:
|
|
56
|
+
return 0.0
|
|
57
|
+
return context / max(self.output_tokens, 1)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@dataclass
|
|
61
|
+
class ProjectStats:
|
|
62
|
+
project_name: str
|
|
63
|
+
project_slug: str
|
|
64
|
+
sessions: int = 0
|
|
65
|
+
input_tokens: int = 0
|
|
66
|
+
output_tokens: int = 0
|
|
67
|
+
cache_write_tokens: int = 0
|
|
68
|
+
cache_read_tokens: int = 0
|
|
69
|
+
cost_usd: float = 0.0
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def total_tokens(self) -> int:
|
|
73
|
+
return (
|
|
74
|
+
self.input_tokens
|
|
75
|
+
+ self.output_tokens
|
|
76
|
+
+ self.cache_write_tokens
|
|
77
|
+
+ self.cache_read_tokens
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@dataclass
|
|
82
|
+
class DailySummary:
|
|
83
|
+
date: date
|
|
84
|
+
sessions: int = 0
|
|
85
|
+
input_tokens: int = 0
|
|
86
|
+
output_tokens: int = 0
|
|
87
|
+
cache_write_tokens: int = 0
|
|
88
|
+
cache_read_tokens: int = 0
|
|
89
|
+
cost_usd: float = 0.0
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def total_tokens(self) -> int:
|
|
93
|
+
return (
|
|
94
|
+
self.input_tokens
|
|
95
|
+
+ self.output_tokens
|
|
96
|
+
+ self.cache_write_tokens
|
|
97
|
+
+ self.cache_read_tokens
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@dataclass
|
|
102
|
+
class AnalysisResult:
|
|
103
|
+
all_sessions: list[SessionStats]
|
|
104
|
+
by_project: dict[str, ProjectStats]
|
|
105
|
+
by_day: dict[date, DailySummary]
|
|
106
|
+
total_cost_usd: float
|
|
107
|
+
total_tokens: int
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _session_to_stats(session: Session) -> SessionStats:
|
|
111
|
+
daily: dict[date, DayUsage] = {}
|
|
112
|
+
for t in session.turns:
|
|
113
|
+
u = t.usage
|
|
114
|
+
d = daily.setdefault(local_date(t.timestamp), DayUsage())
|
|
115
|
+
d.input_tokens += u.input_tokens
|
|
116
|
+
d.output_tokens += u.output_tokens
|
|
117
|
+
d.cache_write_tokens += u.cache_creation_input_tokens
|
|
118
|
+
d.cache_read_tokens += u.cache_read_input_tokens
|
|
119
|
+
d.turns += 1
|
|
120
|
+
# Price each turn at its own model: sessions can switch models.
|
|
121
|
+
d.cost_usd += compute_cost(
|
|
122
|
+
t.model,
|
|
123
|
+
u.input_tokens,
|
|
124
|
+
u.output_tokens,
|
|
125
|
+
u.cache_creation_input_tokens,
|
|
126
|
+
u.cache_read_input_tokens,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
return SessionStats(
|
|
130
|
+
session_id=session.session_id,
|
|
131
|
+
project_slug=session.project_slug,
|
|
132
|
+
project_name=session.project_name,
|
|
133
|
+
model=session.primary_model,
|
|
134
|
+
start_time=session.start_time,
|
|
135
|
+
input_tokens=session.total_input,
|
|
136
|
+
output_tokens=session.total_output,
|
|
137
|
+
cache_write_tokens=session.total_cache_write,
|
|
138
|
+
cache_read_tokens=session.total_cache_read,
|
|
139
|
+
cost_usd=sum(d.cost_usd for d in daily.values()),
|
|
140
|
+
turns=len(session.turns),
|
|
141
|
+
daily=daily,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _session_days(stats: SessionStats) -> dict[date, DayUsage]:
|
|
146
|
+
"""Per-day usage, falling back to the start day for stats built without it."""
|
|
147
|
+
if stats.daily:
|
|
148
|
+
return stats.daily
|
|
149
|
+
if stats.start_time is None:
|
|
150
|
+
return {}
|
|
151
|
+
return {
|
|
152
|
+
local_date(stats.start_time): DayUsage(
|
|
153
|
+
input_tokens=stats.input_tokens,
|
|
154
|
+
output_tokens=stats.output_tokens,
|
|
155
|
+
cache_write_tokens=stats.cache_write_tokens,
|
|
156
|
+
cache_read_tokens=stats.cache_read_tokens,
|
|
157
|
+
cost_usd=stats.cost_usd,
|
|
158
|
+
turns=stats.turns,
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _aggregate(all_stats: list[SessionStats]) -> AnalysisResult:
|
|
164
|
+
by_project: dict[str, ProjectStats] = {}
|
|
165
|
+
by_day: dict[date, DailySummary] = {}
|
|
166
|
+
|
|
167
|
+
for stats in all_stats:
|
|
168
|
+
key = stats.project_slug
|
|
169
|
+
if key not in by_project:
|
|
170
|
+
by_project[key] = ProjectStats(
|
|
171
|
+
project_name=stats.project_name,
|
|
172
|
+
project_slug=stats.project_slug,
|
|
173
|
+
)
|
|
174
|
+
p = by_project[key]
|
|
175
|
+
p.sessions += 1
|
|
176
|
+
p.input_tokens += stats.input_tokens
|
|
177
|
+
p.output_tokens += stats.output_tokens
|
|
178
|
+
p.cache_write_tokens += stats.cache_write_tokens
|
|
179
|
+
p.cache_read_tokens += stats.cache_read_tokens
|
|
180
|
+
p.cost_usd += stats.cost_usd
|
|
181
|
+
|
|
182
|
+
for day, u in _session_days(stats).items():
|
|
183
|
+
if day not in by_day:
|
|
184
|
+
by_day[day] = DailySummary(date=day)
|
|
185
|
+
d = by_day[day]
|
|
186
|
+
d.sessions += 1
|
|
187
|
+
d.input_tokens += u.input_tokens
|
|
188
|
+
d.output_tokens += u.output_tokens
|
|
189
|
+
d.cache_write_tokens += u.cache_write_tokens
|
|
190
|
+
d.cache_read_tokens += u.cache_read_tokens
|
|
191
|
+
d.cost_usd += u.cost_usd
|
|
192
|
+
|
|
193
|
+
return AnalysisResult(
|
|
194
|
+
all_sessions=all_stats,
|
|
195
|
+
by_project=by_project,
|
|
196
|
+
by_day=by_day,
|
|
197
|
+
total_cost_usd=sum(s.cost_usd for s in all_stats),
|
|
198
|
+
total_tokens=sum(s.total_tokens for s in all_stats),
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def analyze(sessions: list[Session]) -> AnalysisResult:
|
|
203
|
+
return _aggregate([_session_to_stats(s) for s in sessions])
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _clip_session(stats: SessionStats, start: date, end: date) -> SessionStats | None:
|
|
207
|
+
"""Return ``stats`` restricted to the days in [start, end], or None if none."""
|
|
208
|
+
days = {d: u for d, u in _session_days(stats).items() if start <= d <= end}
|
|
209
|
+
if not days:
|
|
210
|
+
return None
|
|
211
|
+
if days == stats.daily:
|
|
212
|
+
return stats
|
|
213
|
+
return SessionStats(
|
|
214
|
+
session_id=stats.session_id,
|
|
215
|
+
project_slug=stats.project_slug,
|
|
216
|
+
project_name=stats.project_name,
|
|
217
|
+
model=stats.model,
|
|
218
|
+
start_time=stats.start_time,
|
|
219
|
+
input_tokens=sum(u.input_tokens for u in days.values()),
|
|
220
|
+
output_tokens=sum(u.output_tokens for u in days.values()),
|
|
221
|
+
cache_write_tokens=sum(u.cache_write_tokens for u in days.values()),
|
|
222
|
+
cache_read_tokens=sum(u.cache_read_tokens for u in days.values()),
|
|
223
|
+
cost_usd=sum(u.cost_usd for u in days.values()),
|
|
224
|
+
turns=sum(u.turns for u in days.values()),
|
|
225
|
+
daily=days,
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def filter_by_date_range(
|
|
230
|
+
result: AnalysisResult,
|
|
231
|
+
start: date,
|
|
232
|
+
end: date,
|
|
233
|
+
) -> AnalysisResult:
|
|
234
|
+
"""Keep only usage that happened between ``start`` and ``end`` (local days, inclusive)."""
|
|
235
|
+
clipped = [_clip_session(s, start, end) for s in result.all_sessions]
|
|
236
|
+
return _aggregate([s for s in clipped if s is not None])
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def top_wasteful_sessions(result: AnalysisResult, n: int = 10) -> list[SessionStats]:
|
|
240
|
+
return sorted(result.all_sessions, key=lambda s: s.waste_ratio, reverse=True)[:n]
|
|
@@ -6,8 +6,16 @@ from datetime import date, timedelta
|
|
|
6
6
|
|
|
7
7
|
import click
|
|
8
8
|
from rich.console import Console
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
from rich.markup import escape
|
|
10
|
+
|
|
11
|
+
from .analyzer import (
|
|
12
|
+
AnalysisResult,
|
|
13
|
+
SessionStats,
|
|
14
|
+
analyze,
|
|
15
|
+
filter_by_date_range,
|
|
16
|
+
local_date,
|
|
17
|
+
top_wasteful_sessions,
|
|
18
|
+
)
|
|
11
19
|
from .dashboard import (
|
|
12
20
|
build_daily_table,
|
|
13
21
|
build_projects_table,
|
|
@@ -25,6 +33,11 @@ assert_no_network_imports()
|
|
|
25
33
|
_err = Console(stderr=True)
|
|
26
34
|
|
|
27
35
|
|
|
36
|
+
def _by_start_desc(s: SessionStats) -> float:
|
|
37
|
+
# Sort key that never compares a datetime with a date (TypeError).
|
|
38
|
+
return s.start_time.timestamp() if s.start_time else float("-inf")
|
|
39
|
+
|
|
40
|
+
|
|
28
41
|
def _load() -> AnalysisResult:
|
|
29
42
|
if not CLAUDE_PROJECTS_DIR.exists():
|
|
30
43
|
_err.print(
|
|
@@ -51,7 +64,7 @@ def today() -> None:
|
|
|
51
64
|
today_date = date.today()
|
|
52
65
|
filtered = filter_by_date_range(result, today_date, today_date)
|
|
53
66
|
|
|
54
|
-
console.print(build_summary_panel(
|
|
67
|
+
console.print(build_summary_panel(result))
|
|
55
68
|
console.print()
|
|
56
69
|
console.print(build_projects_table(filtered))
|
|
57
70
|
|
|
@@ -63,7 +76,7 @@ def week() -> None:
|
|
|
63
76
|
start = date.today() - timedelta(days=6)
|
|
64
77
|
filtered = filter_by_date_range(result, start, date.today())
|
|
65
78
|
|
|
66
|
-
console.print(build_summary_panel(
|
|
79
|
+
console.print(build_summary_panel(result))
|
|
67
80
|
console.print()
|
|
68
81
|
console.print(build_daily_table(filtered, days=7))
|
|
69
82
|
console.print()
|
|
@@ -83,20 +96,20 @@ def project(name: str) -> None:
|
|
|
83
96
|
}
|
|
84
97
|
|
|
85
98
|
if not matches:
|
|
86
|
-
_err.print(f"[red]No project matching '{name}' found.[/red]")
|
|
87
|
-
available = ", ".join(s.project_name for s in result.by_project.values())
|
|
99
|
+
_err.print(f"[red]No project matching '{escape(name)}' found.[/red]")
|
|
100
|
+
available = ", ".join(escape(s.project_name) for s in result.by_project.values())
|
|
88
101
|
_err.print(f"Available: {available}")
|
|
89
102
|
sys.exit(1)
|
|
90
103
|
|
|
91
104
|
for slug, stats in matches.items():
|
|
92
105
|
sessions = [s for s in result.all_sessions if s.project_slug == slug]
|
|
93
|
-
sessions.sort(key=
|
|
106
|
+
sessions.sort(key=_by_start_desc, reverse=True)
|
|
94
107
|
|
|
95
108
|
from rich import box
|
|
96
109
|
from rich.table import Table
|
|
97
110
|
|
|
98
111
|
table = Table(
|
|
99
|
-
title=f"[bold]{stats.project_name}[/bold] — {len(sessions)} sessions",
|
|
112
|
+
title=f"[bold]{escape(stats.project_name)}[/bold] — {len(sessions)} sessions",
|
|
100
113
|
box=box.SIMPLE_HEAD,
|
|
101
114
|
header_style="bold cyan",
|
|
102
115
|
)
|
|
@@ -111,8 +124,8 @@ def project(name: str) -> None:
|
|
|
111
124
|
from .dashboard import _fmt_cost, _fmt_tokens
|
|
112
125
|
|
|
113
126
|
for s in sessions[:20]:
|
|
114
|
-
date_str = s.start_time.strftime("%Y-%m-%d %H:%M") if s.start_time else "—"
|
|
115
|
-
model_short = s.model.replace("claude-", "").replace("-20", " '")
|
|
127
|
+
date_str = s.start_time.astimezone().strftime("%Y-%m-%d %H:%M") if s.start_time else "—"
|
|
128
|
+
model_short = escape(s.model.replace("claude-", "").replace("-20", " '"))
|
|
116
129
|
table.add_row(
|
|
117
130
|
date_str,
|
|
118
131
|
model_short,
|
|
@@ -150,11 +163,7 @@ def waste(top: int) -> None:
|
|
|
150
163
|
def export(as_csv: bool) -> None:
|
|
151
164
|
"""Export raw session data."""
|
|
152
165
|
result = _load()
|
|
153
|
-
sessions = sorted(
|
|
154
|
-
result.all_sessions,
|
|
155
|
-
key=lambda s: s.start_time or date.min,
|
|
156
|
-
reverse=True,
|
|
157
|
-
)
|
|
166
|
+
sessions = sorted(result.all_sessions, key=_by_start_desc, reverse=True)
|
|
158
167
|
|
|
159
168
|
if as_csv:
|
|
160
169
|
writer = csv.writer(sys.stdout)
|
|
@@ -175,7 +184,7 @@ def export(as_csv: bool) -> None:
|
|
|
175
184
|
for s in sessions:
|
|
176
185
|
writer.writerow(
|
|
177
186
|
[
|
|
178
|
-
s.start_time
|
|
187
|
+
local_date(s.start_time) if s.start_time else "",
|
|
179
188
|
s.project_name,
|
|
180
189
|
s.session_id,
|
|
181
190
|
s.model,
|
|
@@ -200,8 +209,8 @@ def export(as_csv: bool) -> None:
|
|
|
200
209
|
table.add_column("Cost", justify="right")
|
|
201
210
|
for s in sessions[:50]:
|
|
202
211
|
table.add_row(
|
|
203
|
-
str(s.start_time
|
|
204
|
-
s.project_name,
|
|
212
|
+
str(local_date(s.start_time)) if s.start_time else "—",
|
|
213
|
+
escape(s.project_name),
|
|
205
214
|
_fmt_tokens(s.total_tokens),
|
|
206
215
|
_fmt_cost(s.cost_usd),
|
|
207
216
|
)
|
|
@@ -6,6 +6,7 @@ from datetime import date, datetime, timedelta
|
|
|
6
6
|
|
|
7
7
|
from rich import box
|
|
8
8
|
from rich.console import Console
|
|
9
|
+
from rich.markup import escape
|
|
9
10
|
from rich.panel import Panel
|
|
10
11
|
from rich.table import Table
|
|
11
12
|
|
|
@@ -43,7 +44,8 @@ def _sparkline(values: list[float], width: int = 8) -> str:
|
|
|
43
44
|
return "".join(bars[int(v / hi * 8)] for v in values[-width:])
|
|
44
45
|
|
|
45
46
|
|
|
46
|
-
def build_summary_panel(result: AnalysisResult
|
|
47
|
+
def build_summary_panel(result: AnalysisResult) -> Panel:
|
|
48
|
+
"""Today / This Week / All Time totals. Pass the *unfiltered* result."""
|
|
47
49
|
today = date.today()
|
|
48
50
|
today_result = filter_by_date_range(result, today, today)
|
|
49
51
|
week_start = today - timedelta(days=6)
|
|
@@ -86,7 +88,7 @@ def build_projects_table(result: AnalysisResult, top_n: int = 10) -> Table:
|
|
|
86
88
|
|
|
87
89
|
for p in projects:
|
|
88
90
|
table.add_row(
|
|
89
|
-
f"[bold]{p.project_name}[/bold]",
|
|
91
|
+
f"[bold]{escape(p.project_name)}[/bold]",
|
|
90
92
|
str(p.sessions),
|
|
91
93
|
_fmt_tokens(p.input_tokens),
|
|
92
94
|
_fmt_tokens(p.output_tokens),
|
|
@@ -151,9 +153,9 @@ def build_waste_table(sessions: list[SessionStats], n: int = 10) -> Table:
|
|
|
151
153
|
ratio = s.waste_ratio
|
|
152
154
|
ratio_str = f"{ratio:.0f}x"
|
|
153
155
|
color = _HOT_COLOR if ratio > 100 else _WARN_COLOR if ratio > 30 else "white"
|
|
154
|
-
date_str = s.start_time.strftime("%b %d") if s.start_time else "—"
|
|
156
|
+
date_str = s.start_time.astimezone().strftime("%b %d") if s.start_time else "—"
|
|
155
157
|
table.add_row(
|
|
156
|
-
s.project_name,
|
|
158
|
+
escape(s.project_name),
|
|
157
159
|
date_str,
|
|
158
160
|
_fmt_tokens(s.input_tokens + s.cache_read_tokens),
|
|
159
161
|
_fmt_tokens(s.output_tokens),
|