cctally 1.108.0 → 1.110.0
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.
- package/CHANGELOG.md +147 -0
- package/README.md +6 -6
- package/bin/_cctally_account.py +4 -0
- package/bin/_cctally_alerts.py +6 -2
- package/bin/_cctally_cache.py +1652 -166
- package/bin/_cctally_config.py +3 -2
- package/bin/_cctally_core.py +594 -33
- package/bin/_cctally_dashboard.py +1585 -201
- package/bin/_cctally_dashboard_conversation.py +494 -48
- package/bin/_cctally_dashboard_envelope.py +167 -11
- package/bin/_cctally_dashboard_share.py +154 -36
- package/bin/_cctally_dashboard_sources.py +2615 -422
- package/bin/_cctally_db.py +678 -5
- package/bin/_cctally_diagnosis_sources.py +238 -26
- package/bin/_cctally_doctor.py +99 -18
- package/bin/_cctally_five_hour.py +1295 -40
- package/bin/_cctally_forecast.py +124 -28
- package/bin/_cctally_journal.py +408 -38
- package/bin/_cctally_milestone_history.py +150 -14
- package/bin/_cctally_parser.py +5 -0
- package/bin/_cctally_percent_breakdown.py +6 -1
- package/bin/_cctally_project.py +48 -76
- package/bin/_cctally_quota.py +39 -7
- package/bin/_cctally_quota_model.py +17 -2
- package/bin/_cctally_record.py +1647 -443
- package/bin/_cctally_rederive.py +899 -30
- package/bin/_cctally_refresh.py +2 -1
- package/bin/_cctally_share.py +24 -5
- package/bin/_cctally_source_analytics.py +1035 -78
- package/bin/_cctally_statusline.py +83 -19
- package/bin/_cctally_store.py +61 -4
- package/bin/_cctally_transcript.py +27 -2
- package/bin/_cctally_tui.py +264 -76
- package/bin/_cctally_weekrefs.py +28 -5
- package/bin/_lib_blocks.py +127 -20
- package/bin/_lib_cache_report.py +20 -5
- package/bin/_lib_codex_conversation_query.py +297 -64
- package/bin/_lib_codex_hooks.py +102 -0
- package/bin/_lib_codex_metadata.py +207 -0
- package/bin/_lib_conversation.py +119 -0
- package/bin/_lib_conversation_dispatch.py +31 -1
- package/bin/_lib_conversation_query.py +315 -51
- package/bin/_lib_conversation_retention.py +413 -25
- package/bin/_lib_dashboard_sources.py +267 -7
- package/bin/_lib_diagnosis.py +10 -0
- package/bin/_lib_diff_kernel.py +12 -1
- package/bin/_lib_doctor.py +230 -8
- package/bin/_lib_ingest_frontier.py +1366 -49
- package/bin/_lib_journal.py +19 -2
- package/bin/_lib_merge_gate.py +307 -0
- package/bin/_lib_pricing.py +588 -29
- package/bin/_lib_record.py +130 -12
- package/bin/_lib_rederive.py +903 -2
- package/bin/_lib_render.py +9 -3
- package/bin/_lib_retained_size.py +27 -2
- package/bin/_lib_segment_summary.py +66 -8
- package/bin/_lib_share_templates.py +28 -18
- package/bin/_lib_snapshot_cache.py +115 -1
- package/bin/_lib_source_retry.py +320 -0
- package/bin/_lib_statusline_candidates.py +400 -36
- package/bin/_lib_tick_stats.py +119 -0
- package/bin/_lib_view_models.py +256 -76
- package/bin/cctally +69 -0
- package/dashboard/static/assets/ConversationsView-BB9vUiL6.js +72 -0
- package/dashboard/static/assets/{DoctorModal-CWn3U4Wl.js → DoctorModal-DFY2iPDf.js} +1 -1
- package/dashboard/static/assets/ModalRoot-DvRKHbQE.js +1 -0
- package/dashboard/static/assets/ProjectsDrillPanel-DAv39uSo.js +1 -0
- package/dashboard/static/assets/SourceDetailModal-DSAWo2Fp.js +1 -0
- package/dashboard/static/assets/{UpdateModal-D3m8GG6V.js → UpdateModal-R_9dOVnh.js} +3 -3
- package/dashboard/static/assets/dashboardStream.shared-worker-DbG6Ef0T.js +1 -0
- package/dashboard/static/assets/index-COhxe3vU.css +1 -0
- package/dashboard/static/assets/index-DdU62sov.js +13 -0
- package/dashboard/static/assets/outlineNavigation-BOCLcRa6.js +9 -0
- package/dashboard/static/assets/useKeymap-CW7Up0Ay.js +1 -0
- package/dashboard/static/dashboard.html +3 -3
- package/package.json +4 -1
- package/dashboard/static/assets/ConversationsView-BOSaBRtu.js +0 -72
- package/dashboard/static/assets/ModalRoot-SR070V6c.js +0 -1
- package/dashboard/static/assets/ProjectsDrillPanel-QLI9i5mZ.js +0 -1
- package/dashboard/static/assets/SourceDetailModal-pv0wlFxl.js +0 -1
- package/dashboard/static/assets/dashboardStream.shared-worker-1XTMV3nr.js +0 -1
- package/dashboard/static/assets/index-BHu4mxd8.css +0 -1
- package/dashboard/static/assets/index-CIWsbux3.js +0 -13
- package/dashboard/static/assets/outlineNavigation-CJvKqmLV.js +0 -9
- package/dashboard/static/assets/useKeymap-ffqsS5G0.js +0 -1
package/bin/_lib_pricing.py
CHANGED
|
@@ -17,11 +17,17 @@ Spec: docs/superpowers/specs/2026-05-13-bin-cctally-split-design.md
|
|
|
17
17
|
"""
|
|
18
18
|
from __future__ import annotations
|
|
19
19
|
|
|
20
|
+
import contextlib as _contextlib
|
|
21
|
+
import contextvars as _contextvars
|
|
22
|
+
import dataclasses
|
|
20
23
|
import datetime as dt
|
|
21
24
|
import re
|
|
22
25
|
import sys
|
|
26
|
+
import threading as _threading
|
|
23
27
|
from typing import Any
|
|
24
28
|
|
|
29
|
+
_sys = sys
|
|
30
|
+
|
|
25
31
|
|
|
26
32
|
def _eprint(*args: Any) -> None:
|
|
27
33
|
print(*args, file=sys.stderr)
|
|
@@ -62,7 +68,7 @@ def _chip_for_model(name: str) -> str:
|
|
|
62
68
|
# fingerprint a store recorded and refuses a write from an older process. That
|
|
63
69
|
# comparison is day-granular by construction, so two revisions sharing a date
|
|
64
70
|
# compare equal and the older process is authorized to write.
|
|
65
|
-
PRICING_SNAPSHOT_DATE = "2026-09-
|
|
71
|
+
PRICING_SNAPSHOT_DATE = "2026-09-23"
|
|
66
72
|
PRICING_STALENESS_DAYS = 60 # release pre-flight WARNs past this age
|
|
67
73
|
|
|
68
74
|
|
|
@@ -100,6 +106,162 @@ def pricing_fingerprint_is_comparable(value) -> bool:
|
|
|
100
106
|
return True
|
|
101
107
|
return parse_pricing_fingerprint(value) is not None
|
|
102
108
|
|
|
109
|
+
|
|
110
|
+
class PricingFingerprintObservation:
|
|
111
|
+
"""ONE read of a store's recorded pricing fingerprint, in four states
|
|
112
|
+
(#728).
|
|
113
|
+
|
|
114
|
+
``absent`` — the read succeeded and no value is recorded.
|
|
115
|
+
``present`` — read and parsed; ``parsed_date`` is orderable.
|
|
116
|
+
``malformed`` — read successfully, but the value is not an ISO date.
|
|
117
|
+
``degraded`` — the read ITSELF failed; nothing is known about the store.
|
|
118
|
+
|
|
119
|
+
Four rather than three, because ``present(parsed_date, raw)`` cannot also
|
|
120
|
+
carry an unparseable value: a ``present`` observation whose ``parsed_date``
|
|
121
|
+
is None would either raise on the comparison or be silently read as
|
|
122
|
+
absent. And ``degraded`` is separate from ``absent`` because the whole
|
|
123
|
+
defect this replaces was a failed ``SELECT`` degrading to None and then
|
|
124
|
+
reading as "a fresh store with nothing to protect", which authorized every
|
|
125
|
+
writer over a store it had not managed to read.
|
|
126
|
+
|
|
127
|
+
Frozen, so a consumer cannot downgrade a degraded read to an absent one by
|
|
128
|
+
assignment.
|
|
129
|
+
|
|
130
|
+
Written by hand rather than with ``@dataclasses.dataclass(frozen=True)``,
|
|
131
|
+
and that is load-bearing for this module rather than a style choice. This
|
|
132
|
+
file declares ``from __future__ import annotations``, so every field
|
|
133
|
+
annotation is a string, and `dataclasses` resolves a string annotation via
|
|
134
|
+
``sys.modules.get(cls.__module__).__dict__`` while building the class. This
|
|
135
|
+
module is deliberately a pure stdlib leaf that several callers load by
|
|
136
|
+
executing the file under a private name that is never registered in
|
|
137
|
+
``sys.modules`` — `tests/test_claude_fast_pricing.py` is one — and for
|
|
138
|
+
those the lookup returns None and the class body raises ``AttributeError``
|
|
139
|
+
at import. A hand-rolled frozen class has no such dependency.
|
|
140
|
+
"""
|
|
141
|
+
|
|
142
|
+
__slots__ = ("state", "parsed_date", "raw", "error_kind")
|
|
143
|
+
|
|
144
|
+
def __init__(self, state, parsed_date=None, raw=None, error_kind=None):
|
|
145
|
+
object.__setattr__(self, "state", state)
|
|
146
|
+
object.__setattr__(self, "parsed_date", parsed_date)
|
|
147
|
+
object.__setattr__(self, "raw", raw)
|
|
148
|
+
object.__setattr__(self, "error_kind", error_kind)
|
|
149
|
+
|
|
150
|
+
def __setattr__(self, name, value):
|
|
151
|
+
raise dataclasses.FrozenInstanceError(f"cannot assign to field {name!r}")
|
|
152
|
+
|
|
153
|
+
def __delattr__(self, name):
|
|
154
|
+
raise dataclasses.FrozenInstanceError(f"cannot delete field {name!r}")
|
|
155
|
+
|
|
156
|
+
def __copy__(self):
|
|
157
|
+
# An immutable value shares rather than duplicates. Required as well as
|
|
158
|
+
# correct: `__slots__` plus a refusing `__setattr__` means the generic
|
|
159
|
+
# `copy._reconstruct` path re-assigns each slot and raises, which
|
|
160
|
+
# `dataclasses.asdict` hits the moment one of these becomes a field of
|
|
161
|
+
# a dataclass — `DoctorState` carries two.
|
|
162
|
+
return self
|
|
163
|
+
|
|
164
|
+
def __deepcopy__(self, memo):
|
|
165
|
+
return self
|
|
166
|
+
|
|
167
|
+
def __reduce__(self):
|
|
168
|
+
return (self.__class__, self._key())
|
|
169
|
+
|
|
170
|
+
def _key(self):
|
|
171
|
+
return (self.state, self.parsed_date, self.raw, self.error_kind)
|
|
172
|
+
|
|
173
|
+
def __eq__(self, other):
|
|
174
|
+
if not isinstance(other, PricingFingerprintObservation):
|
|
175
|
+
return NotImplemented
|
|
176
|
+
return self._key() == other._key()
|
|
177
|
+
|
|
178
|
+
def __hash__(self):
|
|
179
|
+
return hash(self._key())
|
|
180
|
+
|
|
181
|
+
def __repr__(self):
|
|
182
|
+
return (
|
|
183
|
+
"PricingFingerprintObservation("
|
|
184
|
+
f"state={self.state!r}, parsed_date={self.parsed_date!r}, "
|
|
185
|
+
f"raw={self.raw!r}, error_kind={self.error_kind!r})"
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def classify_pricing_fingerprint(
|
|
190
|
+
*, found: bool, raw: Any, error_kind: "str | None",
|
|
191
|
+
) -> PricingFingerprintObservation:
|
|
192
|
+
"""Interpret ONE fingerprint read. Pure: the caller performs the SELECT and
|
|
193
|
+
reports what happened; this decides what it means.
|
|
194
|
+
|
|
195
|
+
``error_kind`` set means the read itself failed, which is NOT the same as a
|
|
196
|
+
store with nothing recorded — it wins over any value the partial read
|
|
197
|
+
returned, because a torn observation is not evidence about the store.
|
|
198
|
+
|
|
199
|
+
``parse_pricing_fingerprint`` stays the sole date parser: the guard, doctor
|
|
200
|
+
and this classifier must never disagree about which values are orderable,
|
|
201
|
+
and two parses of that one contract have already drifted apart once.
|
|
202
|
+
"""
|
|
203
|
+
if error_kind is not None:
|
|
204
|
+
return PricingFingerprintObservation("degraded", None, raw, error_kind)
|
|
205
|
+
if not found or not raw:
|
|
206
|
+
return PricingFingerprintObservation("absent")
|
|
207
|
+
parsed = parse_pricing_fingerprint(raw)
|
|
208
|
+
if parsed is None:
|
|
209
|
+
return PricingFingerprintObservation("malformed", None, raw)
|
|
210
|
+
return PricingFingerprintObservation("present", parsed, raw)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _coerce_process_date(process_date: Any) -> "dt.date | None":
|
|
214
|
+
"""The writing process's own snapshot date as an orderable ``date``.
|
|
215
|
+
|
|
216
|
+
Accepts the date directly or the recorded string form, and returns None for
|
|
217
|
+
anything that cannot be ordered — which every predicate below refuses, so
|
|
218
|
+
the guard fails closed on BOTH sides rather than only on the stored one.
|
|
219
|
+
"""
|
|
220
|
+
if isinstance(process_date, dt.datetime):
|
|
221
|
+
return process_date.date()
|
|
222
|
+
if isinstance(process_date, dt.date):
|
|
223
|
+
return process_date
|
|
224
|
+
return parse_pricing_fingerprint(process_date)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _ordered_ok(
|
|
228
|
+
obs: PricingFingerprintObservation, process_date: Any,
|
|
229
|
+
) -> bool:
|
|
230
|
+
"""Day-granular ordering: absent is older than anything, present compares,
|
|
231
|
+
and neither malformed nor degraded is orderable at all."""
|
|
232
|
+
if obs.state == "absent":
|
|
233
|
+
return True
|
|
234
|
+
if obs.state != "present":
|
|
235
|
+
return False
|
|
236
|
+
resolved = _coerce_process_date(process_date)
|
|
237
|
+
if resolved is None or obs.parsed_date is None:
|
|
238
|
+
return False
|
|
239
|
+
return obs.parsed_date <= resolved
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def may_write_materialized_cost(
|
|
243
|
+
obs: PricingFingerprintObservation, process_date: Any,
|
|
244
|
+
) -> bool:
|
|
245
|
+
"""Whether this process may materialize cost over the observed store.
|
|
246
|
+
|
|
247
|
+
A refused write costs a stale rollup, so ``absent`` still fails open: a
|
|
248
|
+
store that genuinely recorded nothing has nothing to protect.
|
|
249
|
+
"""
|
|
250
|
+
return _ordered_ok(obs, process_date)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def may_reset_rebuild_target(
|
|
254
|
+
obs: PricingFingerprintObservation, process_date: Any,
|
|
255
|
+
) -> bool:
|
|
256
|
+
"""Whether this process may CLEAR a rebuild target before replacing it.
|
|
257
|
+
|
|
258
|
+
Same ordering, and this is the path that must fail closed: a refused clear
|
|
259
|
+
costs nothing, while a clear performed on the strength of a read that never
|
|
260
|
+
happened destroys a rollup this process may then be refused permission to
|
|
261
|
+
re-derive.
|
|
262
|
+
"""
|
|
263
|
+
return _ordered_ok(obs, process_date)
|
|
264
|
+
|
|
103
265
|
# Canonical machine-readable pricing source (Claude values + Codex values).
|
|
104
266
|
LITELLM_PRICES_URL = (
|
|
105
267
|
"https://raw.githubusercontent.com/BerriAI/litellm/main/"
|
|
@@ -239,13 +401,17 @@ PRICING_DRIFT_ALLOWLIST: list[dict] = [
|
|
|
239
401
|
# page on 2026-09-02. Anthropic's /v1/models lists claude-fable-5-1, so
|
|
240
402
|
# until now real Fable 5.1 usage was priced at zero with only a warning. 1M
|
|
241
403
|
# context at standard pricing, so NO above-200k tier.
|
|
404
|
+
# 2026-09-22: added claude-opus-5-5 at Anthropic's published $4/$20 per
|
|
405
|
+
# MTok, $5 five-minute cache write, $0.20 cache read, and 2x fast rate.
|
|
406
|
+
# Its 1M context uses standard rates throughout. The pricing fingerprint
|
|
407
|
+
# advances to 2026-09-23 because another revision already used 2026-09-22.
|
|
242
408
|
# Anthropic prices a cache WRITE by TTL: 1.25x base input for a 5-minute write,
|
|
243
409
|
# 2x for a 1-hour write. Both WRITE multipliers are documented as applying
|
|
244
410
|
# consistently across all supported models, so the 1h rate is DERIVED from
|
|
245
411
|
# input_cost_per_token rather than stored per-model — a model added later
|
|
246
412
|
# cannot silently miss it (#195). Cache READS are NOT uniform and are NOT
|
|
247
413
|
# derived: they are 0.1x base input on most models but 0.025x on Claude Fable
|
|
248
|
-
# 5.1 and Claude Mythos 5.1, so the read rate stays stored per-model in
|
|
414
|
+
# 5.1 and Claude Mythos 5.1 and 0.05x on Claude Opus 5.5, so the read rate stays stored per-model in
|
|
249
415
|
# `cache_read_input_token_cost`, which represents a model-specific rate.
|
|
250
416
|
CACHE_WRITE_1H_MULTIPLIER = 2.0
|
|
251
417
|
|
|
@@ -436,6 +602,13 @@ CLAUDE_MODEL_PRICING: dict[str, dict[str, Any]] = {
|
|
|
436
602
|
"cache_creation_input_token_cost": 6.25e-06,
|
|
437
603
|
"cache_read_input_token_cost": 5e-07,
|
|
438
604
|
},
|
|
605
|
+
"claude-opus-5-5": {
|
|
606
|
+
# Source: https://platform.claude.com/docs/en/models/opus-5-5/overview
|
|
607
|
+
"input_cost_per_token": 4e-06,
|
|
608
|
+
"output_cost_per_token": 2e-05,
|
|
609
|
+
"cache_creation_input_token_cost": 5e-06,
|
|
610
|
+
"cache_read_input_token_cost": 2e-07,
|
|
611
|
+
},
|
|
439
612
|
"claude-sonnet-4-20250514": {
|
|
440
613
|
"input_cost_per_token": 3e-06,
|
|
441
614
|
"output_cost_per_token": 1.5e-05,
|
|
@@ -484,7 +657,7 @@ CLAUDE_MODEL_PRICING: dict[str, dict[str, Any]] = {
|
|
|
484
657
|
# deliberately have no fallback multiplier: only a retained authoritative
|
|
485
658
|
# `usage.speed == "fast"` row on one of these exact model IDs is premium-priced.
|
|
486
659
|
# The 4.6/4.7 entries are historical retention rules; current new fast requests
|
|
487
|
-
# are supported only on Opus 5 and Opus 4.8.
|
|
660
|
+
# are supported only on Opus 5.5, Opus 5 and Opus 4.8.
|
|
488
661
|
CLAUDE_FAST_MULTIPLIER_OVERRIDES: dict[str, float] = {
|
|
489
662
|
"claude-opus-4-6": 6.0,
|
|
490
663
|
"claude-opus-4-6-20260205": 6.0,
|
|
@@ -492,6 +665,7 @@ CLAUDE_FAST_MULTIPLIER_OVERRIDES: dict[str, float] = {
|
|
|
492
665
|
"claude-opus-4-7-20260416": 6.0,
|
|
493
666
|
"claude-opus-4-8": 2.0,
|
|
494
667
|
"claude-opus-5": 2.0,
|
|
668
|
+
"claude-opus-5-5": 2.0,
|
|
495
669
|
}
|
|
496
670
|
|
|
497
671
|
|
|
@@ -505,7 +679,7 @@ def _strip_anthropic_model_prefix(model: str) -> str:
|
|
|
505
679
|
|
|
506
680
|
def _claude_fast_multiplier(model: str) -> float:
|
|
507
681
|
"""Fast-tier multiplier for a retained Claude model (standard = 1.0)."""
|
|
508
|
-
return
|
|
682
|
+
return current_pricing_snapshot().fast_multipliers["claude"].get(
|
|
509
683
|
_strip_anthropic_model_prefix(model), 1.0
|
|
510
684
|
)
|
|
511
685
|
|
|
@@ -518,12 +692,13 @@ _unknown_model_warnings: set[str] = set()
|
|
|
518
692
|
# Codex (OpenAI) API pricing snapshot:
|
|
519
693
|
# - Source: https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json
|
|
520
694
|
# - Captured: 2026-07-19 — the last FULL Codex sync. PRICING_SNAPSHOT_DATE has
|
|
521
|
-
# since moved for
|
|
695
|
+
# since moved for six targeted syncs (2026-07-24, the Claude-side opus-5
|
|
522
696
|
# sync; 2026-07-31, the gpt-5.6-terra/-luna correction logged below;
|
|
523
|
-
# 2026-08-13, the Claude-side Sonnet/Mythos sync above;
|
|
524
|
-
# gpt-5.6-cyber addition logged below;
|
|
525
|
-
# addition
|
|
526
|
-
# were NOT re-verified on those
|
|
697
|
+
# 2026-08-13, the Claude-side Sonnet/Mythos sync above; 2026-08-25, the
|
|
698
|
+
# gpt-5.6-cyber addition logged below; 2026-09-05, the gpt-6-astra
|
|
699
|
+
# addition; and 2026-09-22, the gpt-6-sol addition logged below). Codex
|
|
700
|
+
# values outside those four Codex corrections were NOT re-verified on those
|
|
701
|
+
# days.
|
|
527
702
|
# - As of the 2026-07-19 sync this carries every openai-provider
|
|
528
703
|
# gpt-5* model the LiteLLM snapshot lists, so `pricing-check`'s scope finds
|
|
529
704
|
# nothing missing. Models absent from this table still fall back to `gpt-5`
|
|
@@ -567,6 +742,19 @@ _unknown_model_warnings: set[str] = set()
|
|
|
567
742
|
# tokens use the published $20.00 / $2.00 / $75.00 long-context card, and
|
|
568
743
|
# Fast mode is 2x the applicable rates. Verified against OpenAI's model page
|
|
569
744
|
# and the live LiteLLM snapshot; max_input_tokens is 922,000.
|
|
745
|
+
# 2026-09-22: added gpt-6-sol at OpenAI's published Standard $2.00 input /
|
|
746
|
+
# $0.20 cached input / $10.00 output per MTok. Prompts above 272,000 input
|
|
747
|
+
# tokens use $4.00 / $0.40 / $15.00; Fast mode is 2x the applicable rates.
|
|
748
|
+
# Verified against OpenAI's model and pricing pages and the live LiteLLM
|
|
749
|
+
# openai-provider card; max_input_tokens is 922,000.
|
|
750
|
+
# 2026-09-22: added gpt-6-luna at OpenAI's published Standard $0.10 input /
|
|
751
|
+
# $0.01 cached input / $0.50 output per MTok; above 272K, $0.20 / $0.02 /
|
|
752
|
+
# $0.75. Fast mode is 2x. OpenAI documents the Daybreak Red alias as the
|
|
753
|
+
# existing gpt-5.6-cyber card.
|
|
754
|
+
# 2026-09-23: adopted LiteLLM's historical gpt-5.5-cyber OpenAI-provider
|
|
755
|
+
# card at $12.50 input / $1.25 cached input / $75.00 output per MTok.
|
|
756
|
+
# The current OpenAI table lists its 5.6 successor instead. LiteLLM does
|
|
757
|
+
# not publish an above-272K rate for 5.5 Cyber, so do not infer that tier.
|
|
570
758
|
#
|
|
571
759
|
# Billing rules:
|
|
572
760
|
# - reasoning_output_tokens is billed at the *output* rate (matches
|
|
@@ -695,6 +883,13 @@ CODEX_MODEL_PRICING: dict[str, dict[str, Any]] = {
|
|
|
695
883
|
},
|
|
696
884
|
# No cache_creation field: the Codex cost kernel never reads one, so
|
|
697
885
|
# carrying LiteLLM's would only give `diff_pricing` a value to compare.
|
|
886
|
+
"gpt-5.5-cyber": {
|
|
887
|
+
# Historical OpenAI-provider card retained by LiteLLM; unlike the
|
|
888
|
+
# 5.6 successor, no long-context fields are published for this ID.
|
|
889
|
+
"input_cost_per_token": 1.25e-05,
|
|
890
|
+
"cache_read_input_token_cost": 1.25e-06,
|
|
891
|
+
"output_cost_per_token": 7.5e-05,
|
|
892
|
+
},
|
|
698
893
|
"gpt-5.6-cyber": {
|
|
699
894
|
"input_cost_per_token": 1.25e-05,
|
|
700
895
|
"cache_read_input_token_cost": 1.25e-06,
|
|
@@ -714,6 +909,26 @@ CODEX_MODEL_PRICING: dict[str, dict[str, Any]] = {
|
|
|
714
909
|
"cache_read_input_token_cost_above_272k_tokens": 2e-06,
|
|
715
910
|
"output_cost_per_token_above_272k_tokens": 7.5e-05,
|
|
716
911
|
},
|
|
912
|
+
"gpt-6-sol": {
|
|
913
|
+
# Source: https://developers.openai.com/api/docs/models/gpt-6-sol
|
|
914
|
+
# Standard $2.00/M input, $0.20/M cached input, $10.00/M output;
|
|
915
|
+
# above 272K input, rates are 2x input/cache and 1.5x output.
|
|
916
|
+
"input_cost_per_token": 2e-06,
|
|
917
|
+
"cache_read_input_token_cost": 2e-07,
|
|
918
|
+
"output_cost_per_token": 1e-05,
|
|
919
|
+
"input_cost_per_token_above_272k_tokens": 4e-06,
|
|
920
|
+
"cache_read_input_token_cost_above_272k_tokens": 4e-07,
|
|
921
|
+
"output_cost_per_token_above_272k_tokens": 1.5e-05,
|
|
922
|
+
},
|
|
923
|
+
"gpt-6-luna": {
|
|
924
|
+
# Source: https://developers.openai.com/api/docs/models/gpt-6-luna
|
|
925
|
+
"input_cost_per_token": 1e-07,
|
|
926
|
+
"cache_read_input_token_cost": 1e-08,
|
|
927
|
+
"output_cost_per_token": 5e-07,
|
|
928
|
+
"input_cost_per_token_above_272k_tokens": 2e-07,
|
|
929
|
+
"cache_read_input_token_cost_above_272k_tokens": 2e-08,
|
|
930
|
+
"output_cost_per_token_above_272k_tokens": 7.5e-07,
|
|
931
|
+
},
|
|
717
932
|
# ── Issue #123: full gpt-5.x LiteLLM sync (2026-05-30 snapshot) ──
|
|
718
933
|
# Exact model_prices_and_context_window.json values for every
|
|
719
934
|
# openai-provider gpt-5* model `pricing-check`'s scope flags but the
|
|
@@ -910,6 +1125,7 @@ CODEX_MODEL_ALIASES: dict[str, str] = {
|
|
|
910
1125
|
"codex-auto-review": "gpt-5.5",
|
|
911
1126
|
"gpt-5.6": "gpt-5.6-sol",
|
|
912
1127
|
"gpt-daybreak-blue-latest": "gpt-5.6-sol",
|
|
1128
|
+
"gpt-daybreak-red-latest": "gpt-5.6-cyber",
|
|
913
1129
|
}
|
|
914
1130
|
|
|
915
1131
|
# Per-model fast-tier price multipliers, ported from ryoppippi/ccusage
|
|
@@ -928,14 +1144,16 @@ CODEX_FAST_MULTIPLIER_FALLBACK = 2.0
|
|
|
928
1144
|
|
|
929
1145
|
def _codex_fast_multiplier(model: str) -> float:
|
|
930
1146
|
"""Fast-tier price multiplier for a Codex model (standard tier = 1.0)."""
|
|
931
|
-
|
|
932
|
-
|
|
1147
|
+
snapshot = current_pricing_snapshot()
|
|
1148
|
+
return snapshot.fast_multipliers["codex"].get(
|
|
1149
|
+
_canonical_codex_model(model),
|
|
1150
|
+
snapshot.fast_multipliers["codex_fallback"],
|
|
933
1151
|
)
|
|
934
1152
|
|
|
935
1153
|
|
|
936
1154
|
def _canonical_codex_model(model: str) -> str:
|
|
937
1155
|
"""Return the priced model name for a known Codex runtime alias."""
|
|
938
|
-
return
|
|
1156
|
+
return current_pricing_snapshot().aliases.get(model, model)
|
|
939
1157
|
|
|
940
1158
|
|
|
941
1159
|
def _codex_config_requests_fast_service_tier(content: str) -> bool:
|
|
@@ -967,16 +1185,18 @@ def _resolve_codex_pricing(model: str) -> tuple[dict[str, Any] | None, bool]:
|
|
|
967
1185
|
only if the fallback model itself is missing from the pricing dict
|
|
968
1186
|
(programming error; warn once).
|
|
969
1187
|
"""
|
|
970
|
-
|
|
1188
|
+
snapshot = current_pricing_snapshot()
|
|
1189
|
+
direct = snapshot.codex_pricing.get(_canonical_codex_model(model))
|
|
971
1190
|
if direct is not None:
|
|
972
1191
|
return direct, False
|
|
973
|
-
fallback =
|
|
1192
|
+
fallback = snapshot.codex_pricing.get(snapshot.fallback_model)
|
|
974
1193
|
return fallback, True
|
|
975
1194
|
|
|
976
1195
|
|
|
977
1196
|
def _is_codex_fallback(model: str) -> bool:
|
|
978
1197
|
"""True iff `model` would resolve via the LEGACY_FALLBACK_MODEL path."""
|
|
979
|
-
return _canonical_codex_model(model)
|
|
1198
|
+
return (_canonical_codex_model(model)
|
|
1199
|
+
not in current_pricing_snapshot().codex_pricing)
|
|
980
1200
|
|
|
981
1201
|
|
|
982
1202
|
def _resolve_model_pricing(model: str, warn: bool = True) -> dict[str, Any] | None:
|
|
@@ -989,12 +1209,13 @@ def _resolve_model_pricing(model: str, warn: bool = True) -> dict[str, Any] | No
|
|
|
989
1209
|
effect, and don't poison `_unknown_model_warnings` (which would suppress a
|
|
990
1210
|
later genuine cost-path warning for the same model).
|
|
991
1211
|
"""
|
|
992
|
-
|
|
1212
|
+
table = current_pricing_snapshot().claude_pricing
|
|
1213
|
+
pricing = table.get(model)
|
|
993
1214
|
if pricing is not None:
|
|
994
1215
|
return pricing
|
|
995
1216
|
stripped = _strip_anthropic_model_prefix(model)
|
|
996
1217
|
if stripped != model:
|
|
997
|
-
pricing =
|
|
1218
|
+
pricing = table.get(stripped)
|
|
998
1219
|
if pricing is not None:
|
|
999
1220
|
return pricing
|
|
1000
1221
|
if warn and model not in _unknown_model_warnings:
|
|
@@ -1055,13 +1276,16 @@ def _cache_create_cost(pricing: dict, flat: int, h_raw, tiered) -> float:
|
|
|
1055
1276
|
return tiered(flat, "cache_creation_input_token_cost",
|
|
1056
1277
|
"cache_creation_input_token_cost_above_200k_tokens")
|
|
1057
1278
|
|
|
1058
|
-
|
|
1059
|
-
|
|
1279
|
+
snapshot = current_pricing_snapshot()
|
|
1280
|
+
threshold = snapshot.tier_thresholds["claude"]
|
|
1281
|
+
multiplier_1h = snapshot.cache_write_1h_multiplier
|
|
1282
|
+
below = min(flat, threshold)
|
|
1283
|
+
above = max(0, flat - threshold)
|
|
1060
1284
|
frac = h / flat
|
|
1061
1285
|
base = pricing.get("input_cost_per_token", 0.0)
|
|
1062
|
-
r1h = base *
|
|
1286
|
+
r1h = base * multiplier_1h
|
|
1063
1287
|
r1h_200k = pricing.get("input_cost_per_token_above_200k_tokens", base) \
|
|
1064
|
-
*
|
|
1288
|
+
* multiplier_1h
|
|
1065
1289
|
r5m = pricing.get("cache_creation_input_token_cost", 0.0)
|
|
1066
1290
|
r5m_200k = pricing.get("cache_creation_input_token_cost_above_200k_tokens", r5m)
|
|
1067
1291
|
return ((below * frac) * r1h + (above * frac) * r1h_200k
|
|
@@ -1089,9 +1313,10 @@ def _calculate_entry_cost(
|
|
|
1089
1313
|
tiered_rate = pricing.get(tiered_key)
|
|
1090
1314
|
if tokens <= 0:
|
|
1091
1315
|
return 0.0
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1316
|
+
threshold = current_pricing_snapshot().tier_thresholds["claude"]
|
|
1317
|
+
if tokens > threshold and tiered_rate is not None:
|
|
1318
|
+
below = min(tokens, threshold)
|
|
1319
|
+
above = tokens - threshold
|
|
1095
1320
|
return below * base_rate + above * tiered_rate
|
|
1096
1321
|
return tokens * base_rate
|
|
1097
1322
|
|
|
@@ -1161,8 +1386,9 @@ def _calculate_codex_entry_cost(
|
|
|
1161
1386
|
The reasoning_output_tokens parameter is accepted for API stability but
|
|
1162
1387
|
not used directly — its contribution is already billed inside output_tokens.
|
|
1163
1388
|
|
|
1164
|
-
|
|
1165
|
-
|
|
1389
|
+
A request with more than 272k inclusive input tokens uses the long-context
|
|
1390
|
+
rates for its entire request, including cached input and output. The
|
|
1391
|
+
corresponding _above_272k_tokens key must be present in the pricing entry.
|
|
1166
1392
|
"""
|
|
1167
1393
|
del reasoning_output_tokens # already billed inside output_tokens
|
|
1168
1394
|
pricing, is_fallback = _resolve_codex_pricing(model)
|
|
@@ -1174,6 +1400,8 @@ def _calculate_codex_entry_cost(
|
|
|
1174
1400
|
if is_fallback:
|
|
1175
1401
|
_warn_unknown_codex_model(model) # one-shot per unique model name
|
|
1176
1402
|
|
|
1403
|
+
long_context = input_tokens > current_pricing_snapshot().tier_thresholds["codex"]
|
|
1404
|
+
|
|
1177
1405
|
def _tiered(tokens: int, base_key: str, tiered_key: str) -> float:
|
|
1178
1406
|
if tokens <= 0:
|
|
1179
1407
|
return 0.0
|
|
@@ -1181,9 +1409,8 @@ def _calculate_codex_entry_cost(
|
|
|
1181
1409
|
if not base_rate:
|
|
1182
1410
|
return 0.0
|
|
1183
1411
|
tiered_rate = pricing.get(tiered_key)
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
return tokens * base_rate
|
|
1412
|
+
rate = tiered_rate if long_context and tiered_rate is not None else base_rate
|
|
1413
|
+
return tokens * rate
|
|
1187
1414
|
|
|
1188
1415
|
non_cached_input = max(0, input_tokens - cached_input_tokens)
|
|
1189
1416
|
|
|
@@ -1218,3 +1445,335 @@ def _short_model_name(model: str) -> str:
|
|
|
1218
1445
|
if re.match(r".*-\d{8}$", name):
|
|
1219
1446
|
name = name[:-9]
|
|
1220
1447
|
return name
|
|
1448
|
+
|
|
1449
|
+
|
|
1450
|
+
# ---------------------------------------------------------------------------
|
|
1451
|
+
# #714 — the published pricing snapshot, and the reader that replaces it
|
|
1452
|
+
#
|
|
1453
|
+
# Every CLI invocation re-imports this module, so only a long-lived process —
|
|
1454
|
+
# the dashboard — can hold pricing an installer has already replaced on disk.
|
|
1455
|
+
# Two rules shape everything below.
|
|
1456
|
+
#
|
|
1457
|
+
# A PARTIAL SWAP IS WORSE THAN NO SWAP. `PRICING_SNAPSHOT_DATE` is what gates
|
|
1458
|
+
# the materialized-cost write refusal in `_cctally_cache`, so replacing the
|
|
1459
|
+
# date alone clears the refusal and resumes writing cost computed from the old
|
|
1460
|
+
# tables. A candidate is therefore validated whole and adopted whole, or it is
|
|
1461
|
+
# rejected and the live snapshot is retained unchanged.
|
|
1462
|
+
#
|
|
1463
|
+
# NEVER `importlib.reload`. It executes arbitrary module code from a file an
|
|
1464
|
+
# installer has just written; it mutates a live module dict incrementally, so a
|
|
1465
|
+
# request in flight can see half a table; and it would not even fix the bug,
|
|
1466
|
+
# because reloading this module rebinds none of the import-time copies listed
|
|
1467
|
+
# in `PRICING_EXPORT_BINDINGS`. `ast.literal_eval` cannot execute code.
|
|
1468
|
+
|
|
1469
|
+
|
|
1470
|
+
class PricingCandidateRejected(Exception):
|
|
1471
|
+
"""A candidate pricing file was not adopted. Carries the reason."""
|
|
1472
|
+
|
|
1473
|
+
|
|
1474
|
+
class PricingSnapshot:
|
|
1475
|
+
"""One complete, immutable pricing revision.
|
|
1476
|
+
|
|
1477
|
+
Every pricing-affecting value lives here — not only the two tables. A
|
|
1478
|
+
tier threshold, the Codex fallback model, an alias or a fast multiplier
|
|
1479
|
+
left behind by a partial swap prices part of an entry from the previous
|
|
1480
|
+
revision, which is the same defect as a stale table and harder to see.
|
|
1481
|
+
|
|
1482
|
+
Hand-written frozen rather than ``@dataclasses.dataclass(frozen=True)``,
|
|
1483
|
+
for the reason `PricingFingerprintObservation` above states at length:
|
|
1484
|
+
this file declares ``from __future__ import annotations``, `dataclasses`
|
|
1485
|
+
resolves a string annotation through
|
|
1486
|
+
``sys.modules.get(cls.__module__).__dict__``, and several callers load
|
|
1487
|
+
this module by executing the file under a private name that is never
|
|
1488
|
+
registered in ``sys.modules``. For those the lookup returns None and the
|
|
1489
|
+
class body raises ``AttributeError`` at import.
|
|
1490
|
+
"""
|
|
1491
|
+
|
|
1492
|
+
__slots__ = ("snapshot_date", "claude_pricing", "codex_pricing",
|
|
1493
|
+
"aliases", "tier_thresholds", "fallback_model",
|
|
1494
|
+
"cache_write_1h_multiplier", "fast_multipliers")
|
|
1495
|
+
|
|
1496
|
+
def __init__(self, *, snapshot_date, claude_pricing, codex_pricing,
|
|
1497
|
+
aliases, tier_thresholds, fallback_model,
|
|
1498
|
+
cache_write_1h_multiplier, fast_multipliers):
|
|
1499
|
+
for name, value in (
|
|
1500
|
+
("snapshot_date", snapshot_date),
|
|
1501
|
+
("claude_pricing", claude_pricing),
|
|
1502
|
+
("codex_pricing", codex_pricing),
|
|
1503
|
+
("aliases", aliases),
|
|
1504
|
+
("tier_thresholds", tier_thresholds),
|
|
1505
|
+
("fallback_model", fallback_model),
|
|
1506
|
+
("cache_write_1h_multiplier", cache_write_1h_multiplier),
|
|
1507
|
+
("fast_multipliers", fast_multipliers),
|
|
1508
|
+
):
|
|
1509
|
+
object.__setattr__(self, name, value)
|
|
1510
|
+
|
|
1511
|
+
def __setattr__(self, name, value):
|
|
1512
|
+
raise dataclasses.FrozenInstanceError(
|
|
1513
|
+
f"cannot assign to field {name!r}")
|
|
1514
|
+
|
|
1515
|
+
def __delattr__(self, name):
|
|
1516
|
+
raise dataclasses.FrozenInstanceError(
|
|
1517
|
+
f"cannot delete field {name!r}")
|
|
1518
|
+
|
|
1519
|
+
def __repr__(self):
|
|
1520
|
+
return (f"PricingSnapshot(snapshot_date={self.snapshot_date!r}, "
|
|
1521
|
+
f"claude_models={len(self.claude_pricing)}, "
|
|
1522
|
+
f"codex_models={len(self.codex_pricing)})")
|
|
1523
|
+
|
|
1524
|
+
|
|
1525
|
+
#: The complete inventory of import-time copies, made explicit so it can be
|
|
1526
|
+
#: tested rather than asserted. `(module name, attribute, field accessor)`.
|
|
1527
|
+
#: `bin/cctally` copies the tables at import and both dashboard source modules
|
|
1528
|
+
#: read those copies, so a swap that missed one would price from a stale table
|
|
1529
|
+
#: with the refusal latch already cleared.
|
|
1530
|
+
PRICING_EXPORT_BINDINGS = (
|
|
1531
|
+
("_lib_pricing", "PRICING_SNAPSHOT_DATE", lambda s: s.snapshot_date),
|
|
1532
|
+
("_lib_pricing", "CLAUDE_MODEL_PRICING", lambda s: s.claude_pricing),
|
|
1533
|
+
("_lib_pricing", "CODEX_MODEL_PRICING", lambda s: s.codex_pricing),
|
|
1534
|
+
("_lib_pricing", "CODEX_MODEL_ALIASES", lambda s: s.aliases),
|
|
1535
|
+
("_lib_pricing", "TIERED_THRESHOLD",
|
|
1536
|
+
lambda s: s.tier_thresholds["claude"]),
|
|
1537
|
+
("_lib_pricing", "CODEX_TIERED_THRESHOLD",
|
|
1538
|
+
lambda s: s.tier_thresholds["codex"]),
|
|
1539
|
+
("_lib_pricing", "CODEX_LEGACY_FALLBACK_MODEL", lambda s: s.fallback_model),
|
|
1540
|
+
("_lib_pricing", "CACHE_WRITE_1H_MULTIPLIER",
|
|
1541
|
+
lambda s: s.cache_write_1h_multiplier),
|
|
1542
|
+
("_lib_pricing", "CLAUDE_FAST_MULTIPLIER_OVERRIDES",
|
|
1543
|
+
lambda s: s.fast_multipliers["claude"]),
|
|
1544
|
+
("_lib_pricing", "CODEX_FAST_MULTIPLIER_OVERRIDES",
|
|
1545
|
+
lambda s: s.fast_multipliers["codex"]),
|
|
1546
|
+
("_lib_pricing", "CODEX_FAST_MULTIPLIER_FALLBACK",
|
|
1547
|
+
lambda s: s.fast_multipliers["codex_fallback"]),
|
|
1548
|
+
("cctally", "PRICING_SNAPSHOT_DATE", lambda s: s.snapshot_date),
|
|
1549
|
+
("cctally", "CLAUDE_MODEL_PRICING", lambda s: s.claude_pricing),
|
|
1550
|
+
("cctally", "CODEX_MODEL_PRICING", lambda s: s.codex_pricing),
|
|
1551
|
+
("cctally", "TIERED_THRESHOLD", lambda s: s.tier_thresholds["claude"]),
|
|
1552
|
+
("cctally", "CODEX_TIERED_THRESHOLD",
|
|
1553
|
+
lambda s: s.tier_thresholds["codex"]),
|
|
1554
|
+
("cctally", "CODEX_LEGACY_FALLBACK_MODEL", lambda s: s.fallback_model),
|
|
1555
|
+
("cctally", "CACHE_WRITE_1H_MULTIPLIER",
|
|
1556
|
+
lambda s: s.cache_write_1h_multiplier),
|
|
1557
|
+
("cctally", "CLAUDE_FAST_MULTIPLIER_OVERRIDES",
|
|
1558
|
+
lambda s: s.fast_multipliers["claude"]),
|
|
1559
|
+
("cctally", "CODEX_FAST_MULTIPLIER_OVERRIDES",
|
|
1560
|
+
lambda s: s.fast_multipliers["codex"]),
|
|
1561
|
+
("cctally", "CODEX_FAST_MULTIPLIER_FALLBACK",
|
|
1562
|
+
lambda s: s.fast_multipliers["codex_fallback"]),
|
|
1563
|
+
# `_cctally_cache` binds the date as a module global on purpose, so a test
|
|
1564
|
+
# can monkeypatch it; that affordance is preserved and the binding is
|
|
1565
|
+
# simply refreshed here too.
|
|
1566
|
+
("_cctally_cache", "PRICING_SNAPSHOT_DATE", lambda s: s.snapshot_date),
|
|
1567
|
+
# `_lib_cache_report` unpacks the multiplier and the Claude tier threshold
|
|
1568
|
+
# at import. The threshold used to be a literal `200_000` in that file with
|
|
1569
|
+
# nothing keeping it equal to `TIERED_THRESHOLD` here, which made it a
|
|
1570
|
+
# pricing value no reload could replace; it is read from this module now
|
|
1571
|
+
# and refreshed through this entry.
|
|
1572
|
+
("_lib_cache_report", "CACHE_WRITE_1H_MULTIPLIER",
|
|
1573
|
+
lambda s: s.cache_write_1h_multiplier),
|
|
1574
|
+
("_lib_cache_report", "DEFAULT_TIERED_THRESHOLD",
|
|
1575
|
+
lambda s: s.tier_thresholds["claude"]),
|
|
1576
|
+
)
|
|
1577
|
+
|
|
1578
|
+
#: The names `read_pricing_snapshot_from_source` requires, each mapped to the
|
|
1579
|
+
#: `PricingSnapshot` field it fills. A missing OR non-literal name rejects the
|
|
1580
|
+
#: whole candidate.
|
|
1581
|
+
_CANDIDATE_ASSIGNMENTS = (
|
|
1582
|
+
"PRICING_SNAPSHOT_DATE",
|
|
1583
|
+
"CLAUDE_MODEL_PRICING",
|
|
1584
|
+
"CODEX_MODEL_PRICING",
|
|
1585
|
+
"CODEX_MODEL_ALIASES",
|
|
1586
|
+
"TIERED_THRESHOLD",
|
|
1587
|
+
"CODEX_TIERED_THRESHOLD",
|
|
1588
|
+
"CODEX_LEGACY_FALLBACK_MODEL",
|
|
1589
|
+
"CACHE_WRITE_1H_MULTIPLIER",
|
|
1590
|
+
"CLAUDE_FAST_MULTIPLIER_OVERRIDES",
|
|
1591
|
+
"CODEX_FAST_MULTIPLIER_OVERRIDES",
|
|
1592
|
+
"CODEX_FAST_MULTIPLIER_FALLBACK",
|
|
1593
|
+
)
|
|
1594
|
+
|
|
1595
|
+
_PRICING_PUBLISH_LOCK = _threading.Lock()
|
|
1596
|
+
_PRICING_CONTEXT = _contextvars.ContextVar("cctally_pricing_snapshot",
|
|
1597
|
+
default=None)
|
|
1598
|
+
|
|
1599
|
+
|
|
1600
|
+
def _snapshot_from_module_globals() -> PricingSnapshot:
|
|
1601
|
+
return PricingSnapshot(
|
|
1602
|
+
snapshot_date=PRICING_SNAPSHOT_DATE,
|
|
1603
|
+
claude_pricing=CLAUDE_MODEL_PRICING,
|
|
1604
|
+
codex_pricing=CODEX_MODEL_PRICING,
|
|
1605
|
+
aliases=CODEX_MODEL_ALIASES,
|
|
1606
|
+
tier_thresholds={"claude": TIERED_THRESHOLD,
|
|
1607
|
+
"codex": CODEX_TIERED_THRESHOLD},
|
|
1608
|
+
fallback_model=CODEX_LEGACY_FALLBACK_MODEL,
|
|
1609
|
+
cache_write_1h_multiplier=CACHE_WRITE_1H_MULTIPLIER,
|
|
1610
|
+
fast_multipliers={
|
|
1611
|
+
"claude": CLAUDE_FAST_MULTIPLIER_OVERRIDES,
|
|
1612
|
+
"codex": CODEX_FAST_MULTIPLIER_OVERRIDES,
|
|
1613
|
+
"codex_fallback": CODEX_FAST_MULTIPLIER_FALLBACK,
|
|
1614
|
+
},
|
|
1615
|
+
)
|
|
1616
|
+
|
|
1617
|
+
|
|
1618
|
+
_LIVE_PRICING_SNAPSHOT = _snapshot_from_module_globals()
|
|
1619
|
+
|
|
1620
|
+
|
|
1621
|
+
def current_pricing_snapshot() -> PricingSnapshot:
|
|
1622
|
+
"""The snapshot this caller must price from.
|
|
1623
|
+
|
|
1624
|
+
A caller inside `pricing_snapshot_context()` keeps the revision it entered
|
|
1625
|
+
with, so a request, a dashboard snapshot build or a sync pass already
|
|
1626
|
+
under way is never assembled from two revisions. Everyone else reads the
|
|
1627
|
+
live pointer, which is one immutable object replaced by one assignment.
|
|
1628
|
+
"""
|
|
1629
|
+
captured = _PRICING_CONTEXT.get()
|
|
1630
|
+
return _LIVE_PRICING_SNAPSHOT if captured is None else captured
|
|
1631
|
+
|
|
1632
|
+
|
|
1633
|
+
@_contextlib.contextmanager
|
|
1634
|
+
def pricing_snapshot_context(snapshot: "PricingSnapshot | None" = None):
|
|
1635
|
+
"""Pin one revision for the duration of a request, build or sync pass."""
|
|
1636
|
+
pinned = current_pricing_snapshot() if snapshot is None else snapshot
|
|
1637
|
+
token = _PRICING_CONTEXT.set(pinned)
|
|
1638
|
+
try:
|
|
1639
|
+
yield pinned
|
|
1640
|
+
finally:
|
|
1641
|
+
_PRICING_CONTEXT.reset(token)
|
|
1642
|
+
|
|
1643
|
+
|
|
1644
|
+
def publish_pricing_snapshot(snapshot: PricingSnapshot) -> None:
|
|
1645
|
+
"""Swap the live pointer and refresh every import-time copy.
|
|
1646
|
+
|
|
1647
|
+
The pointer swap is one assignment of one frozen object, so a concurrent
|
|
1648
|
+
reader observes the old revision whole or the new one whole. The binding
|
|
1649
|
+
refresh that follows exists for call sites that still read a copied name;
|
|
1650
|
+
every cost kernel in this module reads `current_pricing_snapshot()`
|
|
1651
|
+
instead, so correctness does not depend on the refresh reaching a module
|
|
1652
|
+
that has not been imported.
|
|
1653
|
+
"""
|
|
1654
|
+
global _LIVE_PRICING_SNAPSHOT
|
|
1655
|
+
with _PRICING_PUBLISH_LOCK:
|
|
1656
|
+
_LIVE_PRICING_SNAPSHOT = snapshot
|
|
1657
|
+
for module_name, attr, accessor in PRICING_EXPORT_BINDINGS:
|
|
1658
|
+
module = _sys.modules.get(module_name)
|
|
1659
|
+
if module is None:
|
|
1660
|
+
continue
|
|
1661
|
+
try:
|
|
1662
|
+
setattr(module, attr, accessor(snapshot))
|
|
1663
|
+
except Exception: # noqa: BLE001
|
|
1664
|
+
# A module that refuses an attribute set must not stop the
|
|
1665
|
+
# rest of the inventory being refreshed.
|
|
1666
|
+
_eprint(f"[pricing] could not refresh {module_name}.{attr}")
|
|
1667
|
+
|
|
1668
|
+
|
|
1669
|
+
def adopt_pricing_candidate(candidate: PricingSnapshot, *,
|
|
1670
|
+
force: bool = False) -> bool:
|
|
1671
|
+
"""Publish `candidate` iff it is strictly newer than the live snapshot.
|
|
1672
|
+
|
|
1673
|
+
`_lib_pricing` documents that a pricing revision always ADVANCES
|
|
1674
|
+
`PRICING_SNAPSHOT_DATE`, so a candidate that does not is a rollback, a
|
|
1675
|
+
corrupt read or the same revision read twice; each of those is a reason to
|
|
1676
|
+
keep what is running. `force` exists for tests restoring a captured
|
|
1677
|
+
snapshot and for nothing else.
|
|
1678
|
+
"""
|
|
1679
|
+
if force:
|
|
1680
|
+
publish_pricing_snapshot(candidate)
|
|
1681
|
+
return True
|
|
1682
|
+
live = _LIVE_PRICING_SNAPSHOT
|
|
1683
|
+
new_date = parse_pricing_fingerprint(candidate.snapshot_date)
|
|
1684
|
+
live_date = parse_pricing_fingerprint(live.snapshot_date)
|
|
1685
|
+
if new_date is None:
|
|
1686
|
+
return False
|
|
1687
|
+
if live_date is not None and new_date <= live_date:
|
|
1688
|
+
return False
|
|
1689
|
+
publish_pricing_snapshot(candidate)
|
|
1690
|
+
return True
|
|
1691
|
+
|
|
1692
|
+
|
|
1693
|
+
def _file_signature(path) -> tuple:
|
|
1694
|
+
st = path.stat()
|
|
1695
|
+
return (st.st_dev, st.st_ino, st.st_size, st.st_mtime_ns)
|
|
1696
|
+
|
|
1697
|
+
|
|
1698
|
+
def read_pricing_snapshot_from_source(path) -> PricingSnapshot:
|
|
1699
|
+
"""Build a candidate from the literal assignments in a pricing file.
|
|
1700
|
+
|
|
1701
|
+
`ast.parse` plus `ast.literal_eval`, never `importlib.reload` and never
|
|
1702
|
+
`exec`: the file is one an installer has just written and this process
|
|
1703
|
+
must not run it.
|
|
1704
|
+
|
|
1705
|
+
Raises `PricingCandidateRejected` — never returns a partial snapshot — on
|
|
1706
|
+
a missing name, a name whose value is not a literal, a date that does not
|
|
1707
|
+
parse, or a file whose inode/size/mtime signature changes while it is
|
|
1708
|
+
being read, which is an installer replacing it mid-parse.
|
|
1709
|
+
"""
|
|
1710
|
+
import ast
|
|
1711
|
+
import pathlib as _pathlib
|
|
1712
|
+
|
|
1713
|
+
path = _pathlib.Path(path)
|
|
1714
|
+
try:
|
|
1715
|
+
before = _file_signature(path)
|
|
1716
|
+
source = path.read_bytes()
|
|
1717
|
+
after = _file_signature(path)
|
|
1718
|
+
except OSError as exc:
|
|
1719
|
+
raise PricingCandidateRejected(
|
|
1720
|
+
f"pricing candidate could not be read: {exc}") from exc
|
|
1721
|
+
if before != after:
|
|
1722
|
+
raise PricingCandidateRejected(
|
|
1723
|
+
"pricing candidate signature changed while it was being read")
|
|
1724
|
+
|
|
1725
|
+
try:
|
|
1726
|
+
tree = ast.parse(source.decode("utf-8"), filename=str(path))
|
|
1727
|
+
except (SyntaxError, UnicodeDecodeError) as exc:
|
|
1728
|
+
raise PricingCandidateRejected(
|
|
1729
|
+
f"pricing candidate does not parse: {exc}") from exc
|
|
1730
|
+
|
|
1731
|
+
found: "dict[str, Any]" = {}
|
|
1732
|
+
for node in tree.body:
|
|
1733
|
+
targets = []
|
|
1734
|
+
if isinstance(node, ast.Assign):
|
|
1735
|
+
targets = [t for t in node.targets if isinstance(t, ast.Name)]
|
|
1736
|
+
value = node.value
|
|
1737
|
+
elif isinstance(node, ast.AnnAssign) and isinstance(node.target,
|
|
1738
|
+
ast.Name):
|
|
1739
|
+
targets = [node.target]
|
|
1740
|
+
value = node.value
|
|
1741
|
+
else:
|
|
1742
|
+
continue
|
|
1743
|
+
for target in targets:
|
|
1744
|
+
if target.id not in _CANDIDATE_ASSIGNMENTS or value is None:
|
|
1745
|
+
continue
|
|
1746
|
+
try:
|
|
1747
|
+
found[target.id] = ast.literal_eval(value)
|
|
1748
|
+
except (ValueError, SyntaxError, TypeError) as exc:
|
|
1749
|
+
raise PricingCandidateRejected(
|
|
1750
|
+
f"{target.id} is no longer a literal, so the in-process "
|
|
1751
|
+
f"pricing reload cannot read it: {exc}") from exc
|
|
1752
|
+
|
|
1753
|
+
missing = [name for name in _CANDIDATE_ASSIGNMENTS if name not in found]
|
|
1754
|
+
if missing:
|
|
1755
|
+
raise PricingCandidateRejected(
|
|
1756
|
+
"pricing candidate is incomplete; adopting a partial revision "
|
|
1757
|
+
"would clear the write refusal and price from the old tables. "
|
|
1758
|
+
f"Missing: {', '.join(missing)}")
|
|
1759
|
+
|
|
1760
|
+
if parse_pricing_fingerprint(found["PRICING_SNAPSHOT_DATE"]) is None:
|
|
1761
|
+
raise PricingCandidateRejected(
|
|
1762
|
+
"pricing candidate has no orderable PRICING_SNAPSHOT_DATE: "
|
|
1763
|
+
f"{found['PRICING_SNAPSHOT_DATE']!r}")
|
|
1764
|
+
|
|
1765
|
+
return PricingSnapshot(
|
|
1766
|
+
snapshot_date=found["PRICING_SNAPSHOT_DATE"],
|
|
1767
|
+
claude_pricing=found["CLAUDE_MODEL_PRICING"],
|
|
1768
|
+
codex_pricing=found["CODEX_MODEL_PRICING"],
|
|
1769
|
+
aliases=found["CODEX_MODEL_ALIASES"],
|
|
1770
|
+
tier_thresholds={"claude": found["TIERED_THRESHOLD"],
|
|
1771
|
+
"codex": found["CODEX_TIERED_THRESHOLD"]},
|
|
1772
|
+
fallback_model=found["CODEX_LEGACY_FALLBACK_MODEL"],
|
|
1773
|
+
cache_write_1h_multiplier=found["CACHE_WRITE_1H_MULTIPLIER"],
|
|
1774
|
+
fast_multipliers={
|
|
1775
|
+
"claude": found["CLAUDE_FAST_MULTIPLIER_OVERRIDES"],
|
|
1776
|
+
"codex": found["CODEX_FAST_MULTIPLIER_OVERRIDES"],
|
|
1777
|
+
"codex_fallback": found["CODEX_FAST_MULTIPLIER_FALLBACK"],
|
|
1778
|
+
},
|
|
1779
|
+
)
|