llmcostledger 0.1.0__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.
- llmcostledger-0.1.0/LICENSE +21 -0
- llmcostledger-0.1.0/PKG-INFO +238 -0
- llmcostledger-0.1.0/README.md +220 -0
- llmcostledger-0.1.0/pyproject.toml +30 -0
- llmcostledger-0.1.0/setup.cfg +4 -0
- llmcostledger-0.1.0/src/llmcostledger/__init__.py +66 -0
- llmcostledger-0.1.0/src/llmcostledger/backfill.py +473 -0
- llmcostledger-0.1.0/src/llmcostledger/breakeven.py +190 -0
- llmcostledger-0.1.0/src/llmcostledger/canonical.py +55 -0
- llmcostledger-0.1.0/src/llmcostledger/cli.py +449 -0
- llmcostledger-0.1.0/src/llmcostledger/data/snapshot_seed.json +194 -0
- llmcostledger-0.1.0/src/llmcostledger/diff_engine.py +269 -0
- llmcostledger-0.1.0/src/llmcostledger/drift_bridge.py +266 -0
- llmcostledger-0.1.0/src/llmcostledger/errors.py +89 -0
- llmcostledger-0.1.0/src/llmcostledger/hf_export.py +440 -0
- llmcostledger-0.1.0/src/llmcostledger/io_guard.py +111 -0
- llmcostledger-0.1.0/src/llmcostledger/models.py +97 -0
- llmcostledger-0.1.0/src/llmcostledger/normalizer.py +353 -0
- llmcostledger-0.1.0/src/llmcostledger/pricing.py +163 -0
- llmcostledger-0.1.0/src/llmcostledger/quarantine.py +283 -0
- llmcostledger-0.1.0/src/llmcostledger/sdk.py +762 -0
- llmcostledger-0.1.0/src/llmcostledger/site_builder.py +2100 -0
- llmcostledger-0.1.0/src/llmcostledger/sources.py +404 -0
- llmcostledger-0.1.0/src/llmcostledger/svg_badges.py +221 -0
- llmcostledger-0.1.0/src/llmcostledger/templates.py +564 -0
- llmcostledger-0.1.0/src/llmcostledger/vram.py +223 -0
- llmcostledger-0.1.0/src/llmcostledger.egg-info/PKG-INFO +238 -0
- llmcostledger-0.1.0/src/llmcostledger.egg-info/SOURCES.txt +35 -0
- llmcostledger-0.1.0/src/llmcostledger.egg-info/dependency_links.txt +1 -0
- llmcostledger-0.1.0/src/llmcostledger.egg-info/entry_points.txt +2 -0
- llmcostledger-0.1.0/src/llmcostledger.egg-info/requires.txt +7 -0
- llmcostledger-0.1.0/src/llmcostledger.egg-info/top_level.txt +1 -0
- llmcostledger-0.1.0/tests/test_cli_and_sdk.py +1413 -0
- llmcostledger-0.1.0/tests/test_foundation_and_normalizer.py +250 -0
- llmcostledger-0.1.0/tests/test_math_and_backfill.py +1012 -0
- llmcostledger-0.1.0/tests/test_site_and_badges.py +1173 -0
- llmcostledger-0.1.0/tests/test_sync_diff_quarantine.py +989 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Redul Hasan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: llmcostledger
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LLM cost ledger: deterministic pricing, VRAM and self-host break-even engine with version-tolerant schema normalization
|
|
5
|
+
Author: Redul Hasan
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://llmcostledger.github.io
|
|
8
|
+
Project-URL: Repository, https://github.com/llmcostledger/llmcostledger.github.io
|
|
9
|
+
Requires-Python: >=3.11
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Provides-Extra: dev
|
|
13
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
14
|
+
Requires-Dist: ruff>=0.4; extra == "dev"
|
|
15
|
+
Provides-Extra: hf
|
|
16
|
+
Requires-Dist: pyarrow>=15.0; extra == "hf"
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# llmcostledger
|
|
20
|
+
|
|
21
|
+
A cost ledger for large language model usage: deterministic pricing, VRAM
|
|
22
|
+
budgeting, self-host break-even, a dual-source price feed, a static site with a
|
|
23
|
+
JSON API, and a dataset export.
|
|
24
|
+
|
|
25
|
+
The package has no runtime dependencies. Everything runs on the Python 3.11
|
|
26
|
+
standard library, and a curated snapshot ships inside the wheel, so a fresh
|
|
27
|
+
`pip install` answers questions with no network access.
|
|
28
|
+
|
|
29
|
+
## What is in the box
|
|
30
|
+
|
|
31
|
+
| Part | Module | What it does |
|
|
32
|
+
| --- | --- | --- |
|
|
33
|
+
| Canonical encoding | `llmcostledger.canonical` | Sorted-key JSON bytes, stable SHA-256, refuses NaN/Infinity |
|
|
34
|
+
| Bounded IO | `llmcostledger.io_guard` | Byte-capped HTTP fetches, atomic file writes, bounded reads |
|
|
35
|
+
| Normalizer | `llmcostledger.normalizer` | Version-tolerant parsing of provider records |
|
|
36
|
+
| Pricing | `llmcostledger.pricing` | Cache-hit share, retries, blended effective cost |
|
|
37
|
+
| VRAM | `llmcostledger.vram` | Weights, KV cache and total VRAM per quantization |
|
|
38
|
+
| Break-even | `llmcostledger.breakeven` | GPU rental versus API cost at a token volume |
|
|
39
|
+
| Sources | `llmcostledger.sources` | OpenRouter + LiteLLM merge with LiteLLM precedence |
|
|
40
|
+
| Diff engine | `llmcostledger.diff_engine` | Price drops and hikes, context changes, deprecations |
|
|
41
|
+
| Quarantine | `llmcostledger.quarantine` | Blocks >35% price drops and >10x price jumps |
|
|
42
|
+
| Drift bridge | `llmcostledger.drift_bridge` | Pulls a public drift feed through the bounded HTTP guard |
|
|
43
|
+
| Site builder | `llmcostledger.site_builder` | Static HTML, JSON API, RSS, sitemap, SVG badges |
|
|
44
|
+
| CLI | `llmcostledger.cli` | `calc`, `vram`, `breakeven`, `list`, `diff`, `build-site`, `verify` |
|
|
45
|
+
| SDK | `llmcostledger.sdk` | Seed loading, reports, verification, bounded live sync |
|
|
46
|
+
| Dataset export | `llmcostledger.hf_export` | JSONL always, Parquet when `pyarrow` is installed |
|
|
47
|
+
|
|
48
|
+
## Install
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
python -m pip install . # from a checkout
|
|
52
|
+
python -m pip install "llmcostledger[hf]" # adds pyarrow for Parquet export
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Python 3.11 or newer. `dependencies = []` in `pyproject.toml`;
|
|
56
|
+
`pyarrow` and `pytest` are optional extras, never required at runtime.
|
|
57
|
+
|
|
58
|
+
## Command line
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
llmcostledger calc --model openai/gpt-4o --input-tokens 8000 --output-tokens 500
|
|
62
|
+
llmcostledger calc --model openai/gpt-4o --cache-hit-rate 0.6 --retry-rate 0.05
|
|
63
|
+
llmcostledger vram --model meta/llama-3.3-70b-instruct --quantization int4 --context 8192
|
|
64
|
+
llmcostledger breakeven --model meta/llama-3.3-70b-instruct --daily-tokens 100000000 --gpu rtx-4090 --quant fp16 --context 8192
|
|
65
|
+
llmcostledger list --json
|
|
66
|
+
llmcostledger diff --previous old_snapshot.json --current new_snapshot.json
|
|
67
|
+
llmcostledger build-site --snapshot data/latest_snapshot.json --output _site
|
|
68
|
+
llmcostledger verify --snapshot data/latest_snapshot.json
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Every command except `diff` accepts `--snapshot path.json` to read a specific
|
|
72
|
+
snapshot instead of the bundled seed; `diff` takes `--previous` and `--current`
|
|
73
|
+
snapshot files instead. Typed failures exit 1 with a message on stderr; usage
|
|
74
|
+
errors exit 2.
|
|
75
|
+
|
|
76
|
+
`breakeven` prices the smallest whole GPU count that holds the model at the
|
|
77
|
+
requested `--quant` and `--context`, exactly like the site calculator. A model
|
|
78
|
+
with no published architecture profile (for example `openai/gpt-4o`) has no
|
|
79
|
+
self-hosting side to price: the command reports it as API-only rather than
|
|
80
|
+
inventing a one-GPU cost.
|
|
81
|
+
|
|
82
|
+
## Python SDK
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from datetime import UTC, datetime
|
|
86
|
+
|
|
87
|
+
from llmcostledger import sdk
|
|
88
|
+
|
|
89
|
+
snapshot = sdk.load_seed_snapshot() # no network, no files needed
|
|
90
|
+
spec = sdk.find_model(snapshot, "gpt-4o") # canonical slug, alias or display name
|
|
91
|
+
report = sdk.cost_report(spec, input_tokens=8000, output_tokens=500, cache_hit_rate=0.6)
|
|
92
|
+
print(report["request_cost_usd"])
|
|
93
|
+
|
|
94
|
+
live = sdk.sync_latest(now=datetime.now(UTC)) # bounded GET + versioned disk cache
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`sync_latest` reads `https://llmcostledger.github.io/api/v1/latest.json` with a
|
|
98
|
+
byte cap, a timeout and an empty header set, and caches the parsed snapshot on
|
|
99
|
+
disk so a repeated call works offline.
|
|
100
|
+
|
|
101
|
+
## Static site and JSON API
|
|
102
|
+
|
|
103
|
+
`llmcostledger build-site --output _site` writes:
|
|
104
|
+
|
|
105
|
+
- `index.html`, `models/<slug>.html`, `calculator/index.html`, `changelog/index.html`, `drift/index.html`
|
|
106
|
+
- `api/v1/latest.json`, `api/v1/changelog.json` and `api/models.json`
|
|
107
|
+
- `feed.xml`, `rss.xml`, `sitemap.xml`, `robots.txt`
|
|
108
|
+
- `assets/site.css`, and three SVG badges per model under `badges/`: price per 1M tokens, context window, last sync date
|
|
109
|
+
|
|
110
|
+
The calculator page is a static simulator: `calculator/index.html?model=<slug>`
|
|
111
|
+
computes cost, VRAM, KV cache and self-host break-even in the browser from the
|
|
112
|
+
snapshot embedded in the page. The site is rebuilt from the committed snapshot on
|
|
113
|
+
every deploy, so the published JSON API and the pages never disagree.
|
|
114
|
+
|
|
115
|
+
Per-model JSON lives at `api/v1/models/<file_slug>.json`, where `<file_slug>`
|
|
116
|
+
is the sanitized file segment for the model, not the raw canonical slug:
|
|
117
|
+
the live catalog's GPT-4o record `gpt-4o` is served from
|
|
118
|
+
`api/v1/models/gpt-4o.json`, and the seed catalog's `openai/gpt-4o` from
|
|
119
|
+
`api/v1/models/openai-gpt-4o.json`. The
|
|
120
|
+
`endpoints.model_template` value in `latest.json` uses the same `{file_slug}`
|
|
121
|
+
placeholder, and each record in `latest.json` carries its exact `urls.json`
|
|
122
|
+
path, so consumers never need to guess the mapping.
|
|
123
|
+
|
|
124
|
+
## Dataset export
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
python -m llmcostledger.hf_export --snapshot data/latest_snapshot.json --output hf_dataset
|
|
128
|
+
python -m llmcostledger.hf_export --output hf_dataset --no-parquet --json
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The export writes `snapshot.jsonl` (one canonical JSON object per line), a
|
|
132
|
+
`README.md` dataset card and `dataset_manifest.json`. When `pyarrow` is
|
|
133
|
+
importable it also writes `snapshot.parquet` with an explicit Arrow schema
|
|
134
|
+
(floats as `float64`, token counts as `int64`, everything else `string`) and the
|
|
135
|
+
manifest records `"formats": ["jsonl", "parquet"]`. Without `pyarrow` the JSONL
|
|
136
|
+
column set is the export, and the manifest records why Parquet was not produced.
|
|
137
|
+
|
|
138
|
+
## GitHub Actions
|
|
139
|
+
|
|
140
|
+
Four workflows live in `.github/workflows/`. None of them needs a personal
|
|
141
|
+
access token, a PyPI password or a stored registry credential.
|
|
142
|
+
|
|
143
|
+
| Workflow | Trigger | What it does | Permissions |
|
|
144
|
+
| --- | --- | --- | --- |
|
|
145
|
+
| `deploy_pages.yml` | push to `main`, manual, `workflow_call` | Verifies the snapshot, builds `_site`, deploys with `actions/deploy-pages@v4` | `contents: read`, `pages: write`, `id-token: write` |
|
|
146
|
+
| `daily_sync.yml` | cron `17 6 * * *`, manual with `force_override` | Fetches both feeds, quarantines anomalies, diffs, verifies, commits the accepted snapshot, dispatches the deploy | `contents: write`, `actions: write`, `issues: write` |
|
|
147
|
+
| `publish_pypi.yml` | release published, manual | Runs the suite, builds sdist and wheel, publishes with OIDC trusted publishing | `contents: read` (build), `id-token: write` (publish) |
|
|
148
|
+
| `sync_hf.yml` | cron `43 7 * * 1`, manual | Exports the dataset, uploads it when `HF_TOKEN` is set; non-blocking | `contents: read` |
|
|
149
|
+
|
|
150
|
+
### Daily sync and the quarantine
|
|
151
|
+
|
|
152
|
+
The sync job reads the two public price feeds with a 16 MB byte cap and a 30 s
|
|
153
|
+
timeout each, merges them through the alias resolver (LiteLLM wins on a
|
|
154
|
+
conflict), and runs the anomaly quarantine. The quarantine blocks a snapshot when
|
|
155
|
+
the model count drops by more than 35%, when the median input price across the
|
|
156
|
+
models present in both snapshots increases by a factor of more than 10, or when any
|
|
157
|
+
matched model's input price drops by more than 35%, unless the manual run sets
|
|
158
|
+
`force_override`.
|
|
159
|
+
|
|
160
|
+
The workflow is the only clock adapter: it stamps `as_of_date` and `snapshot_id`
|
|
161
|
+
from `date -u` and passes them in, so the library never reads the wall clock. A
|
|
162
|
+
blocked run fails with the reason in the job summary and writes no snapshot. An
|
|
163
|
+
accepted run rotates `data/latest_snapshot.json` into `data/previous_snapshot.json`,
|
|
164
|
+
commits the new snapshot to `main`, and dispatches the Pages deploy.
|
|
165
|
+
|
|
166
|
+
### Dead-Man's Switch
|
|
167
|
+
|
|
168
|
+
After every run, including failed ones, the workflow asks the Actions API for the
|
|
169
|
+
last 30 runs of `daily_sync.yml`. If the current run failed and the failure run
|
|
170
|
+
reaches three in a row, it opens an issue labelled `dead-mans-switch` (or comments
|
|
171
|
+
on the existing one) with the run link. The next passing run closes that issue.
|
|
172
|
+
The alert path needs no extra secret because `GITHUB_TOKEN` is scoped by the
|
|
173
|
+
`issues: write` permission already declared on the job.
|
|
174
|
+
|
|
175
|
+
### Hugging Face sync
|
|
176
|
+
|
|
177
|
+
`sync_hf.yml` exports the dataset on a weekly schedule and uploads it to
|
|
178
|
+
`llmcostledger/llm-pricing-snapshot` when the repository secret `HF_TOKEN` is
|
|
179
|
+
set. The job and the upload step are both marked `continue-on-error: true`, the
|
|
180
|
+
outcome is written to the run summary, and nothing downstream depends on it: a
|
|
181
|
+
missing token or a failed upload leaves the daily sync and the Pages deploy
|
|
182
|
+
untouched.
|
|
183
|
+
|
|
184
|
+
## Promotion and rollback
|
|
185
|
+
|
|
186
|
+
Builds are artifacts, data is committed, and every deploy is reproducible from a
|
|
187
|
+
commit, so a rollback is a revert rather than a repair.
|
|
188
|
+
|
|
189
|
+
- **Pages.** Deployments are listed under the `github-pages` environment, which
|
|
190
|
+
keeps the previous one. To roll back: `git revert <data commit>` and push, which
|
|
191
|
+
redeploys, or run `gh workflow run deploy_pages.yml --ref <branch-or-tag>`.
|
|
192
|
+
- **Snapshot data.** Every accepted snapshot keeps its predecessor in
|
|
193
|
+
`data/previous_snapshot.json`. Reverting the data commit restores both files.
|
|
194
|
+
- **PyPI.** Published versions are immutable. Fix forward with a higher patch
|
|
195
|
+
version; if a release is broken, yank it in the PyPI UI instead of deleting it.
|
|
196
|
+
- **Container.** Images are addressed by tag; redeploying the previous tag or
|
|
197
|
+
digest is the rollback. The base image is pinned by tag and digest, so a rebuild
|
|
198
|
+
resolves to the same filesystem.
|
|
199
|
+
|
|
200
|
+
## Container
|
|
201
|
+
|
|
202
|
+
```bash
|
|
203
|
+
docker build -t llmcostledger:0.1.0 .
|
|
204
|
+
docker run --rm llmcostledger:0.1.0 calc --model openai/gpt-4o
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
A two-stage build installs the package into a virtual environment in the builder
|
|
208
|
+
stage and copies only that environment into the runtime stage. The runtime runs
|
|
209
|
+
as uid 10001, and the image health check is `llmcostledger verify`, which reads
|
|
210
|
+
the bundled seed snapshot and exits non-zero when a sanity check fails.
|
|
211
|
+
|
|
212
|
+
## Local checks
|
|
213
|
+
|
|
214
|
+
The canonical commands are run from the repository root:
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
PYTHONPATH=./src python -m pytest ./tests --basetemp=./.pytest_tmp -v
|
|
218
|
+
ruff check ./src ./tests
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
`./repro_fresh.sh [python-binary]` repeats the checks from a clean copy: it
|
|
222
|
+
creates its own virtual environment, installs the package into a throwaway target
|
|
223
|
+
with `--no-index`, then runs the suite, the CLI and the dataset export from a
|
|
224
|
+
directory outside the repository. Optional extras already installed for the given
|
|
225
|
+
interpreter are put on the path so the Parquet path runs too. It never modifies
|
|
226
|
+
the checkout and prints the work directory it keeps.
|
|
227
|
+
|
|
228
|
+
## Repository layout
|
|
229
|
+
|
|
230
|
+
```
|
|
231
|
+
pyproject.toml
|
|
232
|
+
src/llmcostledger/ package modules and data/snapshot_seed.json
|
|
233
|
+
tests/ pytest suite
|
|
234
|
+
.github/workflows/ the four workflows above
|
|
235
|
+
data/ snapshots written by daily_sync (data/ appears after the first sync)
|
|
236
|
+
Dockerfile two-stage, non-root image
|
|
237
|
+
repro_fresh.sh offline reproduction script
|
|
238
|
+
```
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# llmcostledger
|
|
2
|
+
|
|
3
|
+
A cost ledger for large language model usage: deterministic pricing, VRAM
|
|
4
|
+
budgeting, self-host break-even, a dual-source price feed, a static site with a
|
|
5
|
+
JSON API, and a dataset export.
|
|
6
|
+
|
|
7
|
+
The package has no runtime dependencies. Everything runs on the Python 3.11
|
|
8
|
+
standard library, and a curated snapshot ships inside the wheel, so a fresh
|
|
9
|
+
`pip install` answers questions with no network access.
|
|
10
|
+
|
|
11
|
+
## What is in the box
|
|
12
|
+
|
|
13
|
+
| Part | Module | What it does |
|
|
14
|
+
| --- | --- | --- |
|
|
15
|
+
| Canonical encoding | `llmcostledger.canonical` | Sorted-key JSON bytes, stable SHA-256, refuses NaN/Infinity |
|
|
16
|
+
| Bounded IO | `llmcostledger.io_guard` | Byte-capped HTTP fetches, atomic file writes, bounded reads |
|
|
17
|
+
| Normalizer | `llmcostledger.normalizer` | Version-tolerant parsing of provider records |
|
|
18
|
+
| Pricing | `llmcostledger.pricing` | Cache-hit share, retries, blended effective cost |
|
|
19
|
+
| VRAM | `llmcostledger.vram` | Weights, KV cache and total VRAM per quantization |
|
|
20
|
+
| Break-even | `llmcostledger.breakeven` | GPU rental versus API cost at a token volume |
|
|
21
|
+
| Sources | `llmcostledger.sources` | OpenRouter + LiteLLM merge with LiteLLM precedence |
|
|
22
|
+
| Diff engine | `llmcostledger.diff_engine` | Price drops and hikes, context changes, deprecations |
|
|
23
|
+
| Quarantine | `llmcostledger.quarantine` | Blocks >35% price drops and >10x price jumps |
|
|
24
|
+
| Drift bridge | `llmcostledger.drift_bridge` | Pulls a public drift feed through the bounded HTTP guard |
|
|
25
|
+
| Site builder | `llmcostledger.site_builder` | Static HTML, JSON API, RSS, sitemap, SVG badges |
|
|
26
|
+
| CLI | `llmcostledger.cli` | `calc`, `vram`, `breakeven`, `list`, `diff`, `build-site`, `verify` |
|
|
27
|
+
| SDK | `llmcostledger.sdk` | Seed loading, reports, verification, bounded live sync |
|
|
28
|
+
| Dataset export | `llmcostledger.hf_export` | JSONL always, Parquet when `pyarrow` is installed |
|
|
29
|
+
|
|
30
|
+
## Install
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
python -m pip install . # from a checkout
|
|
34
|
+
python -m pip install "llmcostledger[hf]" # adds pyarrow for Parquet export
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Python 3.11 or newer. `dependencies = []` in `pyproject.toml`;
|
|
38
|
+
`pyarrow` and `pytest` are optional extras, never required at runtime.
|
|
39
|
+
|
|
40
|
+
## Command line
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
llmcostledger calc --model openai/gpt-4o --input-tokens 8000 --output-tokens 500
|
|
44
|
+
llmcostledger calc --model openai/gpt-4o --cache-hit-rate 0.6 --retry-rate 0.05
|
|
45
|
+
llmcostledger vram --model meta/llama-3.3-70b-instruct --quantization int4 --context 8192
|
|
46
|
+
llmcostledger breakeven --model meta/llama-3.3-70b-instruct --daily-tokens 100000000 --gpu rtx-4090 --quant fp16 --context 8192
|
|
47
|
+
llmcostledger list --json
|
|
48
|
+
llmcostledger diff --previous old_snapshot.json --current new_snapshot.json
|
|
49
|
+
llmcostledger build-site --snapshot data/latest_snapshot.json --output _site
|
|
50
|
+
llmcostledger verify --snapshot data/latest_snapshot.json
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Every command except `diff` accepts `--snapshot path.json` to read a specific
|
|
54
|
+
snapshot instead of the bundled seed; `diff` takes `--previous` and `--current`
|
|
55
|
+
snapshot files instead. Typed failures exit 1 with a message on stderr; usage
|
|
56
|
+
errors exit 2.
|
|
57
|
+
|
|
58
|
+
`breakeven` prices the smallest whole GPU count that holds the model at the
|
|
59
|
+
requested `--quant` and `--context`, exactly like the site calculator. A model
|
|
60
|
+
with no published architecture profile (for example `openai/gpt-4o`) has no
|
|
61
|
+
self-hosting side to price: the command reports it as API-only rather than
|
|
62
|
+
inventing a one-GPU cost.
|
|
63
|
+
|
|
64
|
+
## Python SDK
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from datetime import UTC, datetime
|
|
68
|
+
|
|
69
|
+
from llmcostledger import sdk
|
|
70
|
+
|
|
71
|
+
snapshot = sdk.load_seed_snapshot() # no network, no files needed
|
|
72
|
+
spec = sdk.find_model(snapshot, "gpt-4o") # canonical slug, alias or display name
|
|
73
|
+
report = sdk.cost_report(spec, input_tokens=8000, output_tokens=500, cache_hit_rate=0.6)
|
|
74
|
+
print(report["request_cost_usd"])
|
|
75
|
+
|
|
76
|
+
live = sdk.sync_latest(now=datetime.now(UTC)) # bounded GET + versioned disk cache
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
`sync_latest` reads `https://llmcostledger.github.io/api/v1/latest.json` with a
|
|
80
|
+
byte cap, a timeout and an empty header set, and caches the parsed snapshot on
|
|
81
|
+
disk so a repeated call works offline.
|
|
82
|
+
|
|
83
|
+
## Static site and JSON API
|
|
84
|
+
|
|
85
|
+
`llmcostledger build-site --output _site` writes:
|
|
86
|
+
|
|
87
|
+
- `index.html`, `models/<slug>.html`, `calculator/index.html`, `changelog/index.html`, `drift/index.html`
|
|
88
|
+
- `api/v1/latest.json`, `api/v1/changelog.json` and `api/models.json`
|
|
89
|
+
- `feed.xml`, `rss.xml`, `sitemap.xml`, `robots.txt`
|
|
90
|
+
- `assets/site.css`, and three SVG badges per model under `badges/`: price per 1M tokens, context window, last sync date
|
|
91
|
+
|
|
92
|
+
The calculator page is a static simulator: `calculator/index.html?model=<slug>`
|
|
93
|
+
computes cost, VRAM, KV cache and self-host break-even in the browser from the
|
|
94
|
+
snapshot embedded in the page. The site is rebuilt from the committed snapshot on
|
|
95
|
+
every deploy, so the published JSON API and the pages never disagree.
|
|
96
|
+
|
|
97
|
+
Per-model JSON lives at `api/v1/models/<file_slug>.json`, where `<file_slug>`
|
|
98
|
+
is the sanitized file segment for the model, not the raw canonical slug:
|
|
99
|
+
the live catalog's GPT-4o record `gpt-4o` is served from
|
|
100
|
+
`api/v1/models/gpt-4o.json`, and the seed catalog's `openai/gpt-4o` from
|
|
101
|
+
`api/v1/models/openai-gpt-4o.json`. The
|
|
102
|
+
`endpoints.model_template` value in `latest.json` uses the same `{file_slug}`
|
|
103
|
+
placeholder, and each record in `latest.json` carries its exact `urls.json`
|
|
104
|
+
path, so consumers never need to guess the mapping.
|
|
105
|
+
|
|
106
|
+
## Dataset export
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
python -m llmcostledger.hf_export --snapshot data/latest_snapshot.json --output hf_dataset
|
|
110
|
+
python -m llmcostledger.hf_export --output hf_dataset --no-parquet --json
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The export writes `snapshot.jsonl` (one canonical JSON object per line), a
|
|
114
|
+
`README.md` dataset card and `dataset_manifest.json`. When `pyarrow` is
|
|
115
|
+
importable it also writes `snapshot.parquet` with an explicit Arrow schema
|
|
116
|
+
(floats as `float64`, token counts as `int64`, everything else `string`) and the
|
|
117
|
+
manifest records `"formats": ["jsonl", "parquet"]`. Without `pyarrow` the JSONL
|
|
118
|
+
column set is the export, and the manifest records why Parquet was not produced.
|
|
119
|
+
|
|
120
|
+
## GitHub Actions
|
|
121
|
+
|
|
122
|
+
Four workflows live in `.github/workflows/`. None of them needs a personal
|
|
123
|
+
access token, a PyPI password or a stored registry credential.
|
|
124
|
+
|
|
125
|
+
| Workflow | Trigger | What it does | Permissions |
|
|
126
|
+
| --- | --- | --- | --- |
|
|
127
|
+
| `deploy_pages.yml` | push to `main`, manual, `workflow_call` | Verifies the snapshot, builds `_site`, deploys with `actions/deploy-pages@v4` | `contents: read`, `pages: write`, `id-token: write` |
|
|
128
|
+
| `daily_sync.yml` | cron `17 6 * * *`, manual with `force_override` | Fetches both feeds, quarantines anomalies, diffs, verifies, commits the accepted snapshot, dispatches the deploy | `contents: write`, `actions: write`, `issues: write` |
|
|
129
|
+
| `publish_pypi.yml` | release published, manual | Runs the suite, builds sdist and wheel, publishes with OIDC trusted publishing | `contents: read` (build), `id-token: write` (publish) |
|
|
130
|
+
| `sync_hf.yml` | cron `43 7 * * 1`, manual | Exports the dataset, uploads it when `HF_TOKEN` is set; non-blocking | `contents: read` |
|
|
131
|
+
|
|
132
|
+
### Daily sync and the quarantine
|
|
133
|
+
|
|
134
|
+
The sync job reads the two public price feeds with a 16 MB byte cap and a 30 s
|
|
135
|
+
timeout each, merges them through the alias resolver (LiteLLM wins on a
|
|
136
|
+
conflict), and runs the anomaly quarantine. The quarantine blocks a snapshot when
|
|
137
|
+
the model count drops by more than 35%, when the median input price across the
|
|
138
|
+
models present in both snapshots increases by a factor of more than 10, or when any
|
|
139
|
+
matched model's input price drops by more than 35%, unless the manual run sets
|
|
140
|
+
`force_override`.
|
|
141
|
+
|
|
142
|
+
The workflow is the only clock adapter: it stamps `as_of_date` and `snapshot_id`
|
|
143
|
+
from `date -u` and passes them in, so the library never reads the wall clock. A
|
|
144
|
+
blocked run fails with the reason in the job summary and writes no snapshot. An
|
|
145
|
+
accepted run rotates `data/latest_snapshot.json` into `data/previous_snapshot.json`,
|
|
146
|
+
commits the new snapshot to `main`, and dispatches the Pages deploy.
|
|
147
|
+
|
|
148
|
+
### Dead-Man's Switch
|
|
149
|
+
|
|
150
|
+
After every run, including failed ones, the workflow asks the Actions API for the
|
|
151
|
+
last 30 runs of `daily_sync.yml`. If the current run failed and the failure run
|
|
152
|
+
reaches three in a row, it opens an issue labelled `dead-mans-switch` (or comments
|
|
153
|
+
on the existing one) with the run link. The next passing run closes that issue.
|
|
154
|
+
The alert path needs no extra secret because `GITHUB_TOKEN` is scoped by the
|
|
155
|
+
`issues: write` permission already declared on the job.
|
|
156
|
+
|
|
157
|
+
### Hugging Face sync
|
|
158
|
+
|
|
159
|
+
`sync_hf.yml` exports the dataset on a weekly schedule and uploads it to
|
|
160
|
+
`llmcostledger/llm-pricing-snapshot` when the repository secret `HF_TOKEN` is
|
|
161
|
+
set. The job and the upload step are both marked `continue-on-error: true`, the
|
|
162
|
+
outcome is written to the run summary, and nothing downstream depends on it: a
|
|
163
|
+
missing token or a failed upload leaves the daily sync and the Pages deploy
|
|
164
|
+
untouched.
|
|
165
|
+
|
|
166
|
+
## Promotion and rollback
|
|
167
|
+
|
|
168
|
+
Builds are artifacts, data is committed, and every deploy is reproducible from a
|
|
169
|
+
commit, so a rollback is a revert rather than a repair.
|
|
170
|
+
|
|
171
|
+
- **Pages.** Deployments are listed under the `github-pages` environment, which
|
|
172
|
+
keeps the previous one. To roll back: `git revert <data commit>` and push, which
|
|
173
|
+
redeploys, or run `gh workflow run deploy_pages.yml --ref <branch-or-tag>`.
|
|
174
|
+
- **Snapshot data.** Every accepted snapshot keeps its predecessor in
|
|
175
|
+
`data/previous_snapshot.json`. Reverting the data commit restores both files.
|
|
176
|
+
- **PyPI.** Published versions are immutable. Fix forward with a higher patch
|
|
177
|
+
version; if a release is broken, yank it in the PyPI UI instead of deleting it.
|
|
178
|
+
- **Container.** Images are addressed by tag; redeploying the previous tag or
|
|
179
|
+
digest is the rollback. The base image is pinned by tag and digest, so a rebuild
|
|
180
|
+
resolves to the same filesystem.
|
|
181
|
+
|
|
182
|
+
## Container
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
docker build -t llmcostledger:0.1.0 .
|
|
186
|
+
docker run --rm llmcostledger:0.1.0 calc --model openai/gpt-4o
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
A two-stage build installs the package into a virtual environment in the builder
|
|
190
|
+
stage and copies only that environment into the runtime stage. The runtime runs
|
|
191
|
+
as uid 10001, and the image health check is `llmcostledger verify`, which reads
|
|
192
|
+
the bundled seed snapshot and exits non-zero when a sanity check fails.
|
|
193
|
+
|
|
194
|
+
## Local checks
|
|
195
|
+
|
|
196
|
+
The canonical commands are run from the repository root:
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
PYTHONPATH=./src python -m pytest ./tests --basetemp=./.pytest_tmp -v
|
|
200
|
+
ruff check ./src ./tests
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
`./repro_fresh.sh [python-binary]` repeats the checks from a clean copy: it
|
|
204
|
+
creates its own virtual environment, installs the package into a throwaway target
|
|
205
|
+
with `--no-index`, then runs the suite, the CLI and the dataset export from a
|
|
206
|
+
directory outside the repository. Optional extras already installed for the given
|
|
207
|
+
interpreter are put on the path so the Parquet path runs too. It never modifies
|
|
208
|
+
the checkout and prints the work directory it keeps.
|
|
209
|
+
|
|
210
|
+
## Repository layout
|
|
211
|
+
|
|
212
|
+
```
|
|
213
|
+
pyproject.toml
|
|
214
|
+
src/llmcostledger/ package modules and data/snapshot_seed.json
|
|
215
|
+
tests/ pytest suite
|
|
216
|
+
.github/workflows/ the four workflows above
|
|
217
|
+
data/ snapshots written by daily_sync (data/ appears after the first sync)
|
|
218
|
+
Dockerfile two-stage, non-root image
|
|
219
|
+
repro_fresh.sh offline reproduction script
|
|
220
|
+
```
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "llmcostledger"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "LLM cost ledger: deterministic pricing, VRAM and self-host break-even engine with version-tolerant schema normalization"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "MIT" }
|
|
11
|
+
authors = [{ name = "Redul Hasan" }]
|
|
12
|
+
requires-python = ">=3.11"
|
|
13
|
+
dependencies = []
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Homepage = "https://llmcostledger.github.io"
|
|
17
|
+
Repository = "https://github.com/llmcostledger/llmcostledger.github.io"
|
|
18
|
+
|
|
19
|
+
[project.scripts]
|
|
20
|
+
llmcostledger = "llmcostledger.cli:main"
|
|
21
|
+
|
|
22
|
+
[project.optional-dependencies]
|
|
23
|
+
dev = ["pytest>=8.0", "ruff>=0.4"]
|
|
24
|
+
hf = ["pyarrow>=15.0"]
|
|
25
|
+
|
|
26
|
+
[tool.setuptools.packages.find]
|
|
27
|
+
where = ["src"]
|
|
28
|
+
|
|
29
|
+
[tool.setuptools.package-data]
|
|
30
|
+
llmcostledger = ["data/*.json"]
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Package init for llmcostledger."""
|
|
2
|
+
|
|
3
|
+
from .errors import (
|
|
4
|
+
AnchorSanityError,
|
|
5
|
+
BackfillError,
|
|
6
|
+
BadgeError,
|
|
7
|
+
BoundedIOError,
|
|
8
|
+
BreakEvenError,
|
|
9
|
+
CanonicalizationError,
|
|
10
|
+
CostLedgerError,
|
|
11
|
+
DiffError,
|
|
12
|
+
DriftBridgeError,
|
|
13
|
+
ExportError,
|
|
14
|
+
PricingError,
|
|
15
|
+
QuarantineConfigError,
|
|
16
|
+
QuarantineTrippedError,
|
|
17
|
+
SchemaValidationError,
|
|
18
|
+
SdkError,
|
|
19
|
+
SimulatorQueryError,
|
|
20
|
+
SiteBuildError,
|
|
21
|
+
SubprocessTimeoutError,
|
|
22
|
+
SyncError,
|
|
23
|
+
VramError,
|
|
24
|
+
)
|
|
25
|
+
from .models import (
|
|
26
|
+
BreakEvenResult,
|
|
27
|
+
DailySnapshot,
|
|
28
|
+
ModelSpec,
|
|
29
|
+
OpenWeightProfile,
|
|
30
|
+
PriceChange,
|
|
31
|
+
QuarantineReport,
|
|
32
|
+
VramEstimate,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
__version__ = "0.1.0"
|
|
36
|
+
|
|
37
|
+
__all__ = [
|
|
38
|
+
"AnchorSanityError",
|
|
39
|
+
"BackfillError",
|
|
40
|
+
"BadgeError",
|
|
41
|
+
"BoundedIOError",
|
|
42
|
+
"BreakEvenError",
|
|
43
|
+
"BreakEvenResult",
|
|
44
|
+
"CanonicalizationError",
|
|
45
|
+
"CostLedgerError",
|
|
46
|
+
"DailySnapshot",
|
|
47
|
+
"DiffError",
|
|
48
|
+
"DriftBridgeError",
|
|
49
|
+
"ExportError",
|
|
50
|
+
"ModelSpec",
|
|
51
|
+
"OpenWeightProfile",
|
|
52
|
+
"PriceChange",
|
|
53
|
+
"PricingError",
|
|
54
|
+
"QuarantineConfigError",
|
|
55
|
+
"QuarantineReport",
|
|
56
|
+
"QuarantineTrippedError",
|
|
57
|
+
"SchemaValidationError",
|
|
58
|
+
"SdkError",
|
|
59
|
+
"SimulatorQueryError",
|
|
60
|
+
"SiteBuildError",
|
|
61
|
+
"SubprocessTimeoutError",
|
|
62
|
+
"SyncError",
|
|
63
|
+
"VramError",
|
|
64
|
+
"VramEstimate",
|
|
65
|
+
"__version__",
|
|
66
|
+
]
|