tokenwise-cli 0.1.2__tar.gz → 0.1.3__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.
Files changed (33) hide show
  1. tokenwise_cli-0.1.3/.github/workflows/release.yml +114 -0
  2. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/PKG-INFO +1 -1
  3. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/pyproject.toml +1 -1
  4. tokenwise_cli-0.1.3/src/tokenwise/__init__.py +1 -0
  5. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/src/tokenwise/analyzer.py +1 -0
  6. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/src/tokenwise/parser.py +13 -1
  7. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/src/tokenwise/pricing.py +15 -2
  8. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/tests/test_analyzer.py +23 -0
  9. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/tests/test_parser.py +36 -0
  10. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/tests/test_pricing.py +29 -0
  11. tokenwise_cli-0.1.2/.github/workflows/release.yml +0 -96
  12. tokenwise_cli-0.1.2/src/tokenwise/__init__.py +0 -1
  13. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/.claude/settings.local.json +0 -0
  14. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/.github/workflows/ci.yml +0 -0
  15. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/.github/workflows/codeql.yml +0 -0
  16. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/.github/workflows/dependency-review.yml +0 -0
  17. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/.github/workflows/scorecard.yml +0 -0
  18. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/.gitignore +0 -0
  19. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/CONTRIBUTING.md +0 -0
  20. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/LICENSE +0 -0
  21. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/Makefile +0 -0
  22. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/README.md +0 -0
  23. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/SECURITY.md +0 -0
  24. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/THREAT_MODEL.md +0 -0
  25. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/src/tokenwise/cli.py +0 -0
  26. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/src/tokenwise/dashboard.py +0 -0
  27. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/src/tokenwise/security.py +0 -0
  28. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/src/tokenwise/storage.py +0 -0
  29. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/tests/__init__.py +0 -0
  30. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/tests/test_cli.py +0 -0
  31. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/tests/test_dashboard.py +0 -0
  32. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/tests/test_security.py +0 -0
  33. {tokenwise_cli-0.1.2 → tokenwise_cli-0.1.3}/tests/test_storage.py +0 -0
@@ -0,0 +1,114 @@
1
+ name: Release
2
+
3
+ # Triggered when a version tag is pushed, e.g.:
4
+ # 1. bump `version` in pyproject.toml and `__version__` in src/tokenwise/__init__.py
5
+ # 2. commit, then: git tag v0.1.3 && git push origin v0.1.3
6
+ # The tag must match the package version or the release stops before publishing.
7
+ on:
8
+ push:
9
+ tags:
10
+ - "v*.*.*"
11
+
12
+ permissions:
13
+ contents: read
14
+
15
+ jobs:
16
+ build:
17
+ name: Test and build distribution
18
+ runs-on: ubuntu-latest
19
+
20
+ steps:
21
+ - uses: actions/checkout@v4
22
+
23
+ - name: Set up Python 3.13
24
+ uses: actions/setup-python@v5
25
+ with:
26
+ python-version: "3.13"
27
+
28
+ - name: Check tag matches package version
29
+ run: |
30
+ tag="${GITHUB_REF_NAME#v}"
31
+ pkg=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])")
32
+ mod=$(python -c "import re; print(re.search(r'__version__ = \"(.+?)\"', open('src/tokenwise/__init__.py').read()).group(1))")
33
+ echo "tag=$tag pyproject=$pkg __version__=$mod"
34
+ if [ "$tag" != "$pkg" ] || [ "$tag" != "$mod" ]; then
35
+ echo "::error::Tag v$tag does not match pyproject.toml ($pkg) and __init__.py ($mod)"
36
+ exit 1
37
+ fi
38
+
39
+ - name: Install dependencies
40
+ run: pip install -e ".[dev]" build twine
41
+
42
+ - name: Run tests
43
+ run: pytest -q
44
+
45
+ - name: Build wheel and sdist
46
+ run: python -m build
47
+
48
+ - name: Check distribution metadata
49
+ run: twine check --strict dist/*
50
+
51
+ - name: Upload distribution artifacts
52
+ uses: actions/upload-artifact@v4
53
+ with:
54
+ name: dist
55
+ path: dist/
56
+
57
+ publish-pypi:
58
+ name: Publish to PyPI
59
+ needs: build
60
+ runs-on: ubuntu-latest
61
+ environment:
62
+ name: pypi
63
+ url: https://pypi.org/project/tokenwise-cli/${{ github.ref_name }}/
64
+
65
+ permissions:
66
+ id-token: write # OIDC Trusted Publishing — no API token stored anywhere
67
+
68
+ steps:
69
+ - name: Download distribution artifacts
70
+ uses: actions/download-artifact@v4
71
+ with:
72
+ name: dist
73
+ path: dist/
74
+
75
+ # One-time setup on PyPI (the project already exists, so add a publisher
76
+ # to it rather than a "pending" one):
77
+ # https://pypi.org/manage/project/tokenwise-cli/settings/publishing/
78
+ # - Owner: S0umith29
79
+ # - Repository name: tokenwise
80
+ # - Workflow name: release.yml
81
+ # - Environment name: pypi
82
+ - name: Publish to PyPI
83
+ uses: pypa/gh-action-pypi-publish@release/v1
84
+
85
+ github-release:
86
+ name: Sign and create GitHub Release
87
+ # Only after PyPI accepted the upload, so a failed publish leaves no release.
88
+ needs: publish-pypi
89
+ runs-on: ubuntu-latest
90
+
91
+ permissions:
92
+ contents: write # create the GitHub Release
93
+ id-token: write # Sigstore keyless signing
94
+
95
+ steps:
96
+ - name: Download distribution artifacts
97
+ uses: actions/download-artifact@v4
98
+ with:
99
+ name: dist
100
+ path: dist/
101
+
102
+ - name: Sign artifacts with Sigstore
103
+ uses: sigstore/gh-action-sigstore-python@v3
104
+ with:
105
+ inputs: dist/*.whl dist/*.tar.gz
106
+
107
+ - name: Create GitHub Release with signed artifacts
108
+ uses: softprops/action-gh-release@v2
109
+ with:
110
+ generate_release_notes: true
111
+ files: |
112
+ dist/*.whl
113
+ dist/*.tar.gz
114
+ dist/*.sigstore.json
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: tokenwise-cli
3
- Version: 0.1.2
3
+ Version: 0.1.3
4
4
  Summary: Track and reduce your Claude/Anthropic API costs from the terminal
5
5
  Project-URL: Homepage, https://github.com/S0umith29/tokenwise
6
6
  Project-URL: Bug Tracker, https://github.com/S0umith29/tokenwise/issues
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "tokenwise-cli"
7
- version = "0.1.2"
7
+ version = "0.1.3"
8
8
  description = "Track and reduce your Claude/Anthropic API costs from the terminal"
9
9
  readme = "README.md"
10
10
  license = { file = "LICENSE" }
@@ -0,0 +1 @@
1
+ __version__ = "0.1.3"
@@ -124,6 +124,7 @@ def _session_to_stats(session: Session) -> SessionStats:
124
124
  u.output_tokens,
125
125
  u.cache_creation_input_tokens,
126
126
  u.cache_read_input_tokens,
127
+ cache_write_1h_tokens=u.cache_write_1h_tokens,
127
128
  )
128
129
 
129
130
  return SessionStats(
@@ -15,8 +15,11 @@ CLAUDE_PROJECTS_DIR = Path.home() / ".claude" / "projects"
15
15
  class TokenUsage(BaseModel):
16
16
  input_tokens: int = 0
17
17
  output_tokens: int = 0
18
- cache_creation_input_tokens: int = 0
18
+ cache_creation_input_tokens: int = 0 # all cache writes (5-minute + 1-hour)
19
19
  cache_read_input_tokens: int = 0
20
+ # Portion of cache_creation_input_tokens written with the 1-hour TTL, which
21
+ # is billed at a higher rate than the default 5-minute TTL.
22
+ cache_write_1h_tokens: int = 0
20
23
 
21
24
 
22
25
  class AssistantTurn(BaseModel):
@@ -125,6 +128,14 @@ def parse_session(jsonl_path: Path, project_slug: str) -> Session:
125
128
  if input_tok == output_tok == cache_create == cache_read == 0:
126
129
  continue
127
130
 
131
+ breakdown = raw_usage.get("cache_creation")
132
+ write_1h = 0
133
+ if isinstance(breakdown, dict):
134
+ write_1h = breakdown.get("ephemeral_1h_input_tokens", 0) or 0
135
+ if not isinstance(write_1h, int):
136
+ write_1h = 0
137
+ write_1h = min(max(0, write_1h), cache_create)
138
+
128
139
  ts_str = record.get("timestamp", "")
129
140
  try:
130
141
  ts = datetime.fromisoformat(ts_str.replace("Z", "+00:00"))
@@ -147,6 +158,7 @@ def parse_session(jsonl_path: Path, project_slug: str) -> Session:
147
158
  output_tokens=output_tok,
148
159
  cache_creation_input_tokens=cache_create,
149
160
  cache_read_input_tokens=cache_read,
161
+ cache_write_1h_tokens=write_1h,
150
162
  ),
151
163
  )
152
164
  session.turns.append(turn)
@@ -1,7 +1,7 @@
1
1
  """
2
2
  Anthropic model pricing — last verified 2026-11-27.
3
3
  Prices are per 1 million tokens (USD), first-party API rates.
4
- Cache writes use the 5-minute TTL rate (1.25x input).
4
+ Cache writes: 5-minute TTL at 1.25x input (the `cw` column), 1-hour TTL at 2x input.
5
5
  Source: https://www.anthropic.com/pricing
6
6
  Run `tokenwise update-prices` stub when Anthropic updates their page.
7
7
  """
@@ -22,6 +22,11 @@ class ModelPrice:
22
22
  long_context_threshold: int | None = None
23
23
  long_context: ModelPrice | None = None
24
24
 
25
+ @property
26
+ def cache_write_1h(self) -> float:
27
+ """$/MTok for a prompt cache write with the 1-hour TTL (2x base input)."""
28
+ return self.input * 2
29
+
25
30
 
26
31
  def _p(i: float, o: float, cw: float, cr: float) -> ModelPrice:
27
32
  return ModelPrice(input=i, output=o, cache_write=cw, cache_read=cr)
@@ -104,14 +109,22 @@ def compute_cost(
104
109
  output_tokens: int,
105
110
  cache_write_tokens: int,
106
111
  cache_read_tokens: int,
112
+ cache_write_1h_tokens: int = 0,
107
113
  ) -> float:
114
+ """Cost in USD.
115
+
116
+ ``cache_write_tokens`` is the total of all cache writes; ``cache_write_1h_tokens``
117
+ is the part of it written with the 1-hour TTL (billed at 2x input instead of 1.25x).
118
+ """
108
119
  p = get_price(model)
120
+ write_1h = min(max(0, cache_write_1h_tokens), cache_write_tokens)
109
121
  prompt_tokens = input_tokens + cache_write_tokens + cache_read_tokens
110
122
  if p.long_context and p.long_context_threshold and prompt_tokens > p.long_context_threshold:
111
123
  p = p.long_context
112
124
  return (
113
125
  input_tokens * p.input / _MTok
114
126
  + output_tokens * p.output / _MTok
115
- + cache_write_tokens * p.cache_write / _MTok
127
+ + (cache_write_tokens - write_1h) * p.cache_write / _MTok
128
+ + write_1h * p.cache_write_1h / _MTok
116
129
  + cache_read_tokens * p.cache_read / _MTok
117
130
  )
@@ -413,3 +413,26 @@ def test_session_spanning_midnight_split_across_days():
413
413
  assert day2.total_tokens == 2_000_000
414
414
  assert abs(day2.total_cost_usd - 6.0) < 1e-9
415
415
  assert day2.by_project["slug"].input_tokens == 2_000_000
416
+
417
+
418
+ def test_session_cost_uses_1h_cache_write_rate():
419
+ from tokenwise.parser import AssistantTurn, TokenUsage
420
+
421
+ session = Session(
422
+ session_id="s",
423
+ project_slug="slug",
424
+ turns=[
425
+ AssistantTurn(
426
+ uuid="u",
427
+ message_id="m",
428
+ session_id="s",
429
+ project_slug="slug",
430
+ timestamp=datetime.fromisoformat("2026-04-15T10:00:00+00:00"),
431
+ model="claude-opus-5-5",
432
+ usage=TokenUsage(
433
+ cache_creation_input_tokens=1_000_000, cache_write_1h_tokens=1_000_000
434
+ ),
435
+ )
436
+ ],
437
+ )
438
+ assert abs(analyze([session]).total_cost_usd - 8.00) < 1e-9
@@ -306,3 +306,39 @@ def test_turn_with_unparseable_timestamp_is_skipped(tmp_path):
306
306
  path = _write_jsonl(tmp_path, "proj", lines)
307
307
  session = parse_session(path, "proj")
308
308
  assert [t.message_id for t in session.turns] == ["msg_2"]
309
+
310
+
311
+ def _with_cache_creation(breakdown) -> dict:
312
+ rec = _asst("msg_1", "2026-04-15T10:00:05Z")
313
+ usage = rec["message"]["usage"]
314
+ usage["cache_creation_input_tokens"] = 3000
315
+ if breakdown is not None:
316
+ usage["cache_creation"] = breakdown
317
+ return rec
318
+
319
+
320
+ def test_parses_1h_cache_write_breakdown(tmp_path):
321
+ rec = _with_cache_creation(
322
+ {"ephemeral_5m_input_tokens": 1000, "ephemeral_1h_input_tokens": 2000}
323
+ )
324
+ session = parse_session(_write_jsonl(tmp_path, "proj", [rec]), "proj")
325
+ usage = session.turns[0].usage
326
+ assert usage.cache_creation_input_tokens == 3000
327
+ assert usage.cache_write_1h_tokens == 2000
328
+
329
+
330
+ @pytest.mark.parametrize(
331
+ "breakdown",
332
+ [None, "garbage", {"ephemeral_1h_input_tokens": -5}, {"ephemeral_1h_input_tokens": "x"}],
333
+ )
334
+ def test_missing_or_bad_breakdown_treated_as_5m(tmp_path, breakdown):
335
+ session = parse_session(
336
+ _write_jsonl(tmp_path, "proj", [_with_cache_creation(breakdown)]), "proj"
337
+ )
338
+ assert session.turns[0].usage.cache_write_1h_tokens == 0
339
+
340
+
341
+ def test_1h_cache_writes_never_exceed_total(tmp_path):
342
+ rec = _with_cache_creation({"ephemeral_1h_input_tokens": 999_999})
343
+ session = parse_session(_write_jsonl(tmp_path, "proj", [rec]), "proj")
344
+ assert session.turns[0].usage.cache_write_1h_tokens == 3000
@@ -193,3 +193,32 @@ def test_compute_cost_long_context_counts_cached_prompt():
193
193
  # 10k fresh + 200k cache read = 210k prompt -> long-context rate
194
194
  cost = compute_cost("claude-haiku-5-5", 10_000, 0, 0, 200_000)
195
195
  assert abs(cost - (10_000 * 0.50 + 200_000 * 0.05) / _MTok) < 1e-12
196
+
197
+
198
+ # ---------------------------------------------------------------------------
199
+ # Cache write TTLs
200
+ # ---------------------------------------------------------------------------
201
+
202
+
203
+ def test_compute_cost_1h_cache_write_billed_at_2x_input():
204
+ cost = compute_cost("claude-opus-5-5", 0, 0, _MTok, 0, cache_write_1h_tokens=_MTok)
205
+ assert abs(cost - 8.00) < 1e-9 # 2 x $4 input
206
+
207
+
208
+ def test_compute_cost_mixed_cache_write_ttls():
209
+ # 3M written: 1M at 5-minute TTL ($5), 2M at 1-hour TTL (2 x $8)
210
+ cost = compute_cost("claude-opus-5-5", 0, 0, 3 * _MTok, 0, cache_write_1h_tokens=2 * _MTok)
211
+ assert abs(cost - 21.00) < 1e-9
212
+
213
+
214
+ def test_compute_cost_1h_tokens_clamped_to_total_writes():
215
+ cost = compute_cost("claude-opus-5-5", 0, 0, _MTok, 0, cache_write_1h_tokens=5 * _MTok)
216
+ assert abs(cost - 8.00) < 1e-9
217
+
218
+
219
+ @pytest.mark.parametrize(
220
+ ("model", "expected"),
221
+ [("claude-opus-5-5", 8.00), ("claude-sonnet-5-5", 4.00), ("claude-fable-5-2", 16.00)],
222
+ )
223
+ def test_cache_write_1h_matches_published_rates(model, expected):
224
+ assert get_price(model).cache_write_1h == expected
@@ -1,96 +0,0 @@
1
- name: Release
2
-
3
- # Triggered when a version tag is pushed (e.g. v0.1.0).
4
- on:
5
- push:
6
- tags:
7
- - "v*.*.*"
8
-
9
- permissions:
10
- contents: read
11
- # Required for OIDC Trusted Publishing — no API token needed in secrets.
12
- id-token: write
13
-
14
- jobs:
15
- build:
16
- name: Build distribution
17
- runs-on: ubuntu-latest
18
-
19
- steps:
20
- - uses: actions/checkout@v4
21
-
22
- - name: Set up Python 3.13
23
- uses: actions/setup-python@v5
24
- with:
25
- python-version: "3.13"
26
-
27
- - name: Install build tools
28
- run: pip install build
29
-
30
- - name: Build wheel and sdist
31
- run: python -m build
32
-
33
- - name: Upload distribution artifacts
34
- uses: actions/upload-artifact@v4
35
- with:
36
- name: dist
37
- path: dist/
38
-
39
- publish-pypi:
40
- name: Publish to PyPI
41
- needs: build
42
- runs-on: ubuntu-latest
43
- environment:
44
- name: pypi
45
- url: https://pypi.org/project/tokenwise/
46
-
47
- steps:
48
- - name: Download distribution artifacts
49
- uses: actions/download-artifact@v4
50
- with:
51
- name: dist
52
- path: dist/
53
-
54
- # Trusted Publishing (OIDC) — no API token required.
55
- #
56
- # To configure on PyPI:
57
- # 1. Go to https://pypi.org/manage/account/publishing/
58
- # 2. Add a new "pending publisher" with:
59
- # - PyPI project name: tokenwise
60
- # - Owner: sowmithkuppa
61
- # - Repository: tokenwise
62
- # - Workflow filename: release.yml
63
- # - Environment name: pypi
64
- # 3. That's it — no secret tokens needed.
65
- - name: Publish to PyPI
66
- uses: pypa/gh-action-pypi-publish@release/v1
67
- # No api_token needed — OIDC identity is exchanged automatically.
68
-
69
- sign-sigstore:
70
- name: Sign with Sigstore
71
- needs: build
72
- runs-on: ubuntu-latest
73
-
74
- permissions:
75
- contents: write # to upload the signed artifacts to the GitHub release
76
- id-token: write # for Sigstore OIDC signing
77
-
78
- steps:
79
- - name: Download distribution artifacts
80
- uses: actions/download-artifact@v4
81
- with:
82
- name: dist
83
- path: dist/
84
-
85
- - name: Sign artifacts with Sigstore
86
- uses: sigstore/gh-action-sigstore-python@v3
87
- with:
88
- inputs: dist/*.whl dist/*.tar.gz
89
-
90
- - name: Upload signed artifacts to GitHub Release
91
- uses: softprops/action-gh-release@v2
92
- with:
93
- files: |
94
- dist/*.whl
95
- dist/*.tar.gz
96
- dist/*.sigstore.json
@@ -1 +0,0 @@
1
- __version__ = "0.1.2"
File without changes
File without changes
File without changes
File without changes
File without changes