tokenwise-cli 0.1.0__tar.gz → 0.1.1__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.0 → tokenwise_cli-0.1.1}/PKG-INFO +9 -24
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/README.md +8 -23
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/pyproject.toml +1 -1
- tokenwise_cli-0.1.1/tests/test_analyzer.py +357 -0
- tokenwise_cli-0.1.1/tests/test_cli.py +207 -0
- tokenwise_cli-0.1.1/tests/test_dashboard.py +313 -0
- tokenwise_cli-0.1.1/tests/test_pricing.py +148 -0
- tokenwise_cli-0.1.1/tests/test_storage.py +167 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/.github/workflows/ci.yml +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/.github/workflows/codeql.yml +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/.github/workflows/dependency-review.yml +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/.github/workflows/release.yml +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/.github/workflows/scorecard.yml +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/.gitignore +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/CONTRIBUTING.md +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/LICENSE +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/Makefile +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/SECURITY.md +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/THREAT_MODEL.md +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/src/tokenwise/__init__.py +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/src/tokenwise/analyzer.py +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/src/tokenwise/cli.py +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/src/tokenwise/dashboard.py +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/src/tokenwise/parser.py +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/src/tokenwise/pricing.py +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/src/tokenwise/security.py +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/src/tokenwise/storage.py +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/tests/__init__.py +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/tests/test_parser.py +0 -0
- {tokenwise_cli-0.1.0 → tokenwise_cli-0.1.1}/tests/test_security.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tokenwise-cli
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.1
|
|
4
4
|
Summary: Track and reduce your Claude/Anthropic API costs from the terminal
|
|
5
5
|
Project-URL: Homepage, https://github.com/sowmithkuppa/tokenwise
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/sowmithkuppa/tokenwise/issues
|
|
@@ -43,11 +43,11 @@ 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
53
|
<!-- GIF placeholder -->
|
|
@@ -113,13 +113,13 @@ Zero cloud sync. Zero telemetry. Pure read-only.
|
|
|
113
113
|
## Install
|
|
114
114
|
|
|
115
115
|
```bash
|
|
116
|
-
pip install tokenwise
|
|
116
|
+
pip install tokenwise-cli
|
|
117
117
|
```
|
|
118
118
|
|
|
119
119
|
Or with [pipx](https://pypa.github.io/pipx/) (recommended for CLI tools):
|
|
120
120
|
|
|
121
121
|
```bash
|
|
122
|
-
pipx install tokenwise
|
|
122
|
+
pipx install tokenwise-cli
|
|
123
123
|
```
|
|
124
124
|
|
|
125
125
|
### Requirements
|
|
@@ -133,7 +133,7 @@ python3 --version
|
|
|
133
133
|
**Development setup** (for contributors):
|
|
134
134
|
|
|
135
135
|
```bash
|
|
136
|
-
git clone https://github.com/
|
|
136
|
+
git clone https://github.com/S0umith29/tokenwise.git
|
|
137
137
|
cd tokenwise
|
|
138
138
|
python3.13 -m venv .venv # or python3.10 / 3.11 / 3.12
|
|
139
139
|
source .venv/bin/activate
|
|
@@ -175,21 +175,6 @@ Parsed data is cached in `~/.tokenwise/cache.json` (permissions `0o600`) so repe
|
|
|
175
175
|
|
|
176
176
|
---
|
|
177
177
|
|
|
178
|
-
## Comparison to `ccusage`
|
|
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 | ✅ | ❌ |
|
|
190
|
-
|
|
191
|
-
---
|
|
192
|
-
|
|
193
178
|
## Contributing
|
|
194
179
|
|
|
195
180
|
See [`CONTRIBUTING.md`](CONTRIBUTING.md).
|
|
@@ -2,11 +2,11 @@
|
|
|
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
12
|
<!-- GIF placeholder -->
|
|
@@ -72,13 +72,13 @@ Zero cloud sync. Zero telemetry. Pure read-only.
|
|
|
72
72
|
## Install
|
|
73
73
|
|
|
74
74
|
```bash
|
|
75
|
-
pip install tokenwise
|
|
75
|
+
pip install tokenwise-cli
|
|
76
76
|
```
|
|
77
77
|
|
|
78
78
|
Or with [pipx](https://pypa.github.io/pipx/) (recommended for CLI tools):
|
|
79
79
|
|
|
80
80
|
```bash
|
|
81
|
-
pipx install tokenwise
|
|
81
|
+
pipx install tokenwise-cli
|
|
82
82
|
```
|
|
83
83
|
|
|
84
84
|
### Requirements
|
|
@@ -92,7 +92,7 @@ python3 --version
|
|
|
92
92
|
**Development setup** (for contributors):
|
|
93
93
|
|
|
94
94
|
```bash
|
|
95
|
-
git clone https://github.com/
|
|
95
|
+
git clone https://github.com/S0umith29/tokenwise.git
|
|
96
96
|
cd tokenwise
|
|
97
97
|
python3.13 -m venv .venv # or python3.10 / 3.11 / 3.12
|
|
98
98
|
source .venv/bin/activate
|
|
@@ -134,21 +134,6 @@ Parsed data is cached in `~/.tokenwise/cache.json` (permissions `0o600`) so repe
|
|
|
134
134
|
|
|
135
135
|
---
|
|
136
136
|
|
|
137
|
-
## Comparison to `ccusage`
|
|
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 | ✅ | ❌ |
|
|
149
|
-
|
|
150
|
-
---
|
|
151
|
-
|
|
152
137
|
## Contributing
|
|
153
138
|
|
|
154
139
|
See [`CONTRIBUTING.md`](CONTRIBUTING.md).
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
"""Tests for the analyzer module."""
|
|
2
|
+
|
|
3
|
+
from datetime import date, datetime
|
|
4
|
+
|
|
5
|
+
from tokenwise.analyzer import (
|
|
6
|
+
AnalysisResult,
|
|
7
|
+
DailySummary,
|
|
8
|
+
ProjectStats,
|
|
9
|
+
SessionStats,
|
|
10
|
+
analyze,
|
|
11
|
+
filter_by_date_range,
|
|
12
|
+
top_wasteful_sessions,
|
|
13
|
+
)
|
|
14
|
+
from tokenwise.parser import AssistantTurn, Session, TokenUsage
|
|
15
|
+
|
|
16
|
+
# ---------------------------------------------------------------------------
|
|
17
|
+
# Helpers
|
|
18
|
+
# ---------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _ts(iso: str) -> datetime:
|
|
22
|
+
return datetime.fromisoformat(iso.replace("Z", "+00:00"))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _make_turn(
|
|
26
|
+
model: str = "claude-sonnet-4-6",
|
|
27
|
+
input_tokens: int = 1000,
|
|
28
|
+
output_tokens: int = 200,
|
|
29
|
+
cache_write: int = 500,
|
|
30
|
+
cache_read: int = 800,
|
|
31
|
+
timestamp: str = "2026-04-15T10:00:00Z",
|
|
32
|
+
) -> AssistantTurn:
|
|
33
|
+
return AssistantTurn(
|
|
34
|
+
uuid="test-uuid",
|
|
35
|
+
session_id="sess-1",
|
|
36
|
+
project_slug="-Users-test-myproject",
|
|
37
|
+
timestamp=_ts(timestamp),
|
|
38
|
+
model=model,
|
|
39
|
+
usage=TokenUsage(
|
|
40
|
+
input_tokens=input_tokens,
|
|
41
|
+
output_tokens=output_tokens,
|
|
42
|
+
cache_creation_input_tokens=cache_write,
|
|
43
|
+
cache_read_input_tokens=cache_read,
|
|
44
|
+
),
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _make_session(
|
|
49
|
+
session_id: str = "sess-1",
|
|
50
|
+
slug: str = "-Users-test-myproject",
|
|
51
|
+
project_path: str = "/Users/test/myproject",
|
|
52
|
+
turns: list[AssistantTurn] | None = None,
|
|
53
|
+
) -> Session:
|
|
54
|
+
return Session(
|
|
55
|
+
session_id=session_id,
|
|
56
|
+
project_slug=slug,
|
|
57
|
+
project_path=project_path,
|
|
58
|
+
turns=turns or [_make_turn()],
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# ---------------------------------------------------------------------------
|
|
63
|
+
# SessionStats properties
|
|
64
|
+
# ---------------------------------------------------------------------------
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def test_session_stats_total_tokens():
|
|
68
|
+
stats = SessionStats(
|
|
69
|
+
session_id="s1",
|
|
70
|
+
project_slug="slug",
|
|
71
|
+
project_name="proj",
|
|
72
|
+
model="claude-sonnet-4-6",
|
|
73
|
+
start_time=None,
|
|
74
|
+
input_tokens=1000,
|
|
75
|
+
output_tokens=200,
|
|
76
|
+
cache_write_tokens=500,
|
|
77
|
+
cache_read_tokens=800,
|
|
78
|
+
cost_usd=0.01,
|
|
79
|
+
turns=1,
|
|
80
|
+
)
|
|
81
|
+
assert stats.total_tokens == 2500
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def test_session_stats_waste_ratio_normal():
|
|
85
|
+
stats = SessionStats(
|
|
86
|
+
session_id="s1",
|
|
87
|
+
project_slug="slug",
|
|
88
|
+
project_name="proj",
|
|
89
|
+
model="claude-sonnet-4-6",
|
|
90
|
+
start_time=None,
|
|
91
|
+
input_tokens=1000,
|
|
92
|
+
output_tokens=100,
|
|
93
|
+
cache_write_tokens=0,
|
|
94
|
+
cache_read_tokens=500,
|
|
95
|
+
cost_usd=0.01,
|
|
96
|
+
turns=1,
|
|
97
|
+
)
|
|
98
|
+
assert stats.waste_ratio == (1000 + 500) / 100
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_session_stats_waste_ratio_zero_output():
|
|
102
|
+
stats = SessionStats(
|
|
103
|
+
session_id="s1",
|
|
104
|
+
project_slug="slug",
|
|
105
|
+
project_name="proj",
|
|
106
|
+
model="claude-sonnet-4-6",
|
|
107
|
+
start_time=None,
|
|
108
|
+
input_tokens=1000,
|
|
109
|
+
output_tokens=0,
|
|
110
|
+
cache_write_tokens=0,
|
|
111
|
+
cache_read_tokens=0,
|
|
112
|
+
cost_usd=0.0,
|
|
113
|
+
turns=0,
|
|
114
|
+
)
|
|
115
|
+
assert stats.waste_ratio == 0.0
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# ---------------------------------------------------------------------------
|
|
119
|
+
# ProjectStats properties
|
|
120
|
+
# ---------------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def test_project_stats_total_tokens():
|
|
124
|
+
p = ProjectStats(
|
|
125
|
+
project_name="proj",
|
|
126
|
+
project_slug="slug",
|
|
127
|
+
input_tokens=100,
|
|
128
|
+
output_tokens=200,
|
|
129
|
+
cache_write_tokens=300,
|
|
130
|
+
cache_read_tokens=400,
|
|
131
|
+
)
|
|
132
|
+
assert p.total_tokens == 1000
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
# ---------------------------------------------------------------------------
|
|
136
|
+
# DailySummary properties
|
|
137
|
+
# ---------------------------------------------------------------------------
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def test_daily_summary_total_tokens():
|
|
141
|
+
d = DailySummary(
|
|
142
|
+
date=date(2026, 4, 15),
|
|
143
|
+
input_tokens=100,
|
|
144
|
+
output_tokens=200,
|
|
145
|
+
cache_write_tokens=300,
|
|
146
|
+
cache_read_tokens=400,
|
|
147
|
+
)
|
|
148
|
+
assert d.total_tokens == 1000
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
# ---------------------------------------------------------------------------
|
|
152
|
+
# analyze — empty input
|
|
153
|
+
# ---------------------------------------------------------------------------
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def test_analyze_empty_sessions():
|
|
157
|
+
result = analyze([])
|
|
158
|
+
assert result.all_sessions == []
|
|
159
|
+
assert result.by_project == {}
|
|
160
|
+
assert result.by_day == {}
|
|
161
|
+
assert result.total_cost_usd == 0.0
|
|
162
|
+
assert result.total_tokens == 0
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
# ---------------------------------------------------------------------------
|
|
166
|
+
# analyze — single session
|
|
167
|
+
# ---------------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def test_analyze_single_session_produces_one_stat():
|
|
171
|
+
session = _make_session()
|
|
172
|
+
result = analyze([session])
|
|
173
|
+
assert len(result.all_sessions) == 1
|
|
174
|
+
stat = result.all_sessions[0]
|
|
175
|
+
assert stat.model == "claude-sonnet-4-6"
|
|
176
|
+
assert stat.input_tokens == 1000
|
|
177
|
+
assert stat.output_tokens == 200
|
|
178
|
+
assert stat.cache_write_tokens == 500
|
|
179
|
+
assert stat.cache_read_tokens == 800
|
|
180
|
+
assert stat.turns == 1
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def test_analyze_single_session_populates_by_project():
|
|
184
|
+
session = _make_session()
|
|
185
|
+
result = analyze([session])
|
|
186
|
+
assert "-Users-test-myproject" in result.by_project
|
|
187
|
+
p = result.by_project["-Users-test-myproject"]
|
|
188
|
+
assert p.sessions == 1
|
|
189
|
+
assert p.input_tokens == 1000
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def test_analyze_single_session_populates_by_day():
|
|
193
|
+
session = _make_session()
|
|
194
|
+
result = analyze([session])
|
|
195
|
+
day = date(2026, 4, 15)
|
|
196
|
+
assert day in result.by_day
|
|
197
|
+
assert result.by_day[day].sessions == 1
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def test_analyze_total_cost_is_positive():
|
|
201
|
+
session = _make_session()
|
|
202
|
+
result = analyze([session])
|
|
203
|
+
assert result.total_cost_usd > 0
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def test_analyze_total_tokens():
|
|
207
|
+
session = _make_session()
|
|
208
|
+
result = analyze([session])
|
|
209
|
+
assert result.total_tokens == 1000 + 200 + 500 + 800
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
# ---------------------------------------------------------------------------
|
|
213
|
+
# analyze — multiple sessions, aggregation
|
|
214
|
+
# ---------------------------------------------------------------------------
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def test_analyze_two_sessions_same_project_aggregates():
|
|
218
|
+
s1 = _make_session(session_id="s1")
|
|
219
|
+
s2 = _make_session(session_id="s2")
|
|
220
|
+
result = analyze([s1, s2])
|
|
221
|
+
p = result.by_project["-Users-test-myproject"]
|
|
222
|
+
assert p.sessions == 2
|
|
223
|
+
assert p.input_tokens == 2000
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def test_analyze_two_sessions_different_projects():
|
|
227
|
+
s1 = _make_session(session_id="s1", slug="-Users-a", project_path="/Users/a")
|
|
228
|
+
s2 = _make_session(session_id="s2", slug="-Users-b", project_path="/Users/b")
|
|
229
|
+
result = analyze([s1, s2])
|
|
230
|
+
assert len(result.by_project) == 2
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def test_analyze_session_without_start_time_skips_by_day():
|
|
234
|
+
session = Session(
|
|
235
|
+
session_id="empty",
|
|
236
|
+
project_slug="-Users-test",
|
|
237
|
+
project_path="/Users/test",
|
|
238
|
+
turns=[],
|
|
239
|
+
)
|
|
240
|
+
result = analyze([session])
|
|
241
|
+
assert result.by_day == {}
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
# ---------------------------------------------------------------------------
|
|
245
|
+
# filter_by_date_range
|
|
246
|
+
# ---------------------------------------------------------------------------
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def test_filter_by_date_range_keeps_matching_session():
|
|
250
|
+
session = _make_session()
|
|
251
|
+
result = analyze([session])
|
|
252
|
+
filtered = filter_by_date_range(result, date(2026, 4, 15), date(2026, 4, 15))
|
|
253
|
+
assert len(filtered.all_sessions) == 1
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def test_filter_by_date_range_excludes_outside_session():
|
|
257
|
+
session = _make_session()
|
|
258
|
+
result = analyze([session])
|
|
259
|
+
filtered = filter_by_date_range(result, date(2026, 4, 1), date(2026, 4, 10))
|
|
260
|
+
assert len(filtered.all_sessions) == 0
|
|
261
|
+
assert filtered.total_cost_usd == 0.0
|
|
262
|
+
assert filtered.total_tokens == 0
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def test_filter_by_date_range_aggregates_by_project():
|
|
266
|
+
s1 = _make_session(session_id="s1", turns=[_make_turn(timestamp="2026-04-15T10:00:00Z")])
|
|
267
|
+
s2 = _make_session(session_id="s2", turns=[_make_turn(timestamp="2026-04-20T10:00:00Z")])
|
|
268
|
+
result = analyze([s1, s2])
|
|
269
|
+
filtered = filter_by_date_range(result, date(2026, 4, 15), date(2026, 4, 15))
|
|
270
|
+
p = filtered.by_project["-Users-test-myproject"]
|
|
271
|
+
assert p.sessions == 1
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def test_filter_by_date_range_populates_by_day():
|
|
275
|
+
session = _make_session()
|
|
276
|
+
result = analyze([session])
|
|
277
|
+
filtered = filter_by_date_range(result, date(2026, 4, 15), date(2026, 4, 15))
|
|
278
|
+
assert date(2026, 4, 15) in filtered.by_day
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
# ---------------------------------------------------------------------------
|
|
282
|
+
# top_wasteful_sessions
|
|
283
|
+
# ---------------------------------------------------------------------------
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def test_top_wasteful_sessions_returns_sorted_by_ratio():
|
|
287
|
+
high_waste = SessionStats(
|
|
288
|
+
session_id="s1",
|
|
289
|
+
project_slug="slug",
|
|
290
|
+
project_name="proj",
|
|
291
|
+
model="claude-sonnet-4-6",
|
|
292
|
+
start_time=None,
|
|
293
|
+
input_tokens=10000,
|
|
294
|
+
output_tokens=10,
|
|
295
|
+
cache_write_tokens=0,
|
|
296
|
+
cache_read_tokens=0,
|
|
297
|
+
cost_usd=0.01,
|
|
298
|
+
turns=1,
|
|
299
|
+
)
|
|
300
|
+
low_waste = SessionStats(
|
|
301
|
+
session_id="s2",
|
|
302
|
+
project_slug="slug",
|
|
303
|
+
project_name="proj",
|
|
304
|
+
model="claude-sonnet-4-6",
|
|
305
|
+
start_time=None,
|
|
306
|
+
input_tokens=100,
|
|
307
|
+
output_tokens=100,
|
|
308
|
+
cache_write_tokens=0,
|
|
309
|
+
cache_read_tokens=0,
|
|
310
|
+
cost_usd=0.001,
|
|
311
|
+
turns=1,
|
|
312
|
+
)
|
|
313
|
+
result = AnalysisResult(
|
|
314
|
+
all_sessions=[low_waste, high_waste],
|
|
315
|
+
by_project={},
|
|
316
|
+
by_day={},
|
|
317
|
+
total_cost_usd=0.011,
|
|
318
|
+
total_tokens=10210,
|
|
319
|
+
)
|
|
320
|
+
top = top_wasteful_sessions(result, n=2)
|
|
321
|
+
assert top[0].session_id == "s1"
|
|
322
|
+
assert top[1].session_id == "s2"
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def test_top_wasteful_sessions_respects_n():
|
|
326
|
+
sessions = [
|
|
327
|
+
SessionStats(
|
|
328
|
+
session_id=f"s{i}",
|
|
329
|
+
project_slug="slug",
|
|
330
|
+
project_name="proj",
|
|
331
|
+
model="claude-sonnet-4-6",
|
|
332
|
+
start_time=None,
|
|
333
|
+
input_tokens=1000 * i,
|
|
334
|
+
output_tokens=10,
|
|
335
|
+
cache_write_tokens=0,
|
|
336
|
+
cache_read_tokens=0,
|
|
337
|
+
cost_usd=0.01,
|
|
338
|
+
turns=1,
|
|
339
|
+
)
|
|
340
|
+
for i in range(1, 6)
|
|
341
|
+
]
|
|
342
|
+
result = AnalysisResult(
|
|
343
|
+
all_sessions=sessions,
|
|
344
|
+
by_project={},
|
|
345
|
+
by_day={},
|
|
346
|
+
total_cost_usd=0.05,
|
|
347
|
+
total_tokens=sum(s.total_tokens for s in sessions),
|
|
348
|
+
)
|
|
349
|
+
top = top_wasteful_sessions(result, n=3)
|
|
350
|
+
assert len(top) == 3
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def test_top_wasteful_sessions_empty():
|
|
354
|
+
result = AnalysisResult(
|
|
355
|
+
all_sessions=[], by_project={}, by_day={}, total_cost_usd=0.0, total_tokens=0
|
|
356
|
+
)
|
|
357
|
+
assert top_wasteful_sessions(result) == []
|