metergraph-core 0.1.0__py3-none-any.whl
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.
- metergraph_core/__init__.py +21 -0
- metergraph_core/catalog.py +275 -0
- metergraph_core/data/prices.yaml +886 -0
- metergraph_core/loader.py +167 -0
- metergraph_core-0.1.0.dist-info/METADATA +110 -0
- metergraph_core-0.1.0.dist-info/RECORD +8 -0
- metergraph_core-0.1.0.dist-info/WHEEL +5 -0
- metergraph_core-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Reusable MeterGraph catalog and deterministic token-cost pricing core."""
|
|
2
|
+
|
|
3
|
+
from .catalog import Alias, CatalogSnapshot, CostResult, Price, ResolvedPrice
|
|
4
|
+
from .loader import (
|
|
5
|
+
CatalogError,
|
|
6
|
+
LoadedCatalog,
|
|
7
|
+
load_catalog,
|
|
8
|
+
parse_catalog,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"Alias",
|
|
13
|
+
"CatalogError",
|
|
14
|
+
"CatalogSnapshot",
|
|
15
|
+
"CostResult",
|
|
16
|
+
"LoadedCatalog",
|
|
17
|
+
"Price",
|
|
18
|
+
"ResolvedPrice",
|
|
19
|
+
"load_catalog",
|
|
20
|
+
"parse_catalog",
|
|
21
|
+
]
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"""Effective-dated model catalog and deterministic token-cost enrichment."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
|
|
6
|
+
from types import MappingProxyType
|
|
7
|
+
from typing import Any, Mapping
|
|
8
|
+
|
|
9
|
+
_MILLION = Decimal("1000000")
|
|
10
|
+
_COST_QUANTUM = Decimal("0.00000001")
|
|
11
|
+
_PROVIDER_ALIASES = {
|
|
12
|
+
"amazon-bedrock": "bedrock",
|
|
13
|
+
"aws": "bedrock",
|
|
14
|
+
"aws-bedrock": "bedrock",
|
|
15
|
+
"gemini": "google",
|
|
16
|
+
"google-genai": "google",
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True, slots=True)
|
|
21
|
+
class Alias:
|
|
22
|
+
model_id: str
|
|
23
|
+
canonical_id: str
|
|
24
|
+
pricing_channel: str
|
|
25
|
+
rules: Mapping[str, Any]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True, slots=True)
|
|
29
|
+
class Price:
|
|
30
|
+
id: str
|
|
31
|
+
model_id: str
|
|
32
|
+
pricing_channel: str
|
|
33
|
+
region: str
|
|
34
|
+
input_per_mtok: Decimal | None
|
|
35
|
+
output_per_mtok: Decimal | None
|
|
36
|
+
cache_read_per_mtok: Decimal | None
|
|
37
|
+
cache_write_5m_per_mtok: Decimal | None
|
|
38
|
+
cache_write_1h_per_mtok: Decimal | None
|
|
39
|
+
batch_input_per_mtok: Decimal | None
|
|
40
|
+
batch_output_per_mtok: Decimal | None
|
|
41
|
+
rules: Mapping[str, Any]
|
|
42
|
+
effective_from: datetime
|
|
43
|
+
effective_to: datetime | None
|
|
44
|
+
source_url: str
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass(frozen=True, slots=True)
|
|
48
|
+
class CostResult:
|
|
49
|
+
canonical_model: str | None
|
|
50
|
+
price_id: str | None
|
|
51
|
+
cost_usd: Decimal | None
|
|
52
|
+
status: str
|
|
53
|
+
reasons: tuple[str, ...] = ()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True, slots=True)
|
|
57
|
+
class ResolvedPrice:
|
|
58
|
+
"""An effective price selected for a planned model deployment."""
|
|
59
|
+
|
|
60
|
+
canonical_model: str
|
|
61
|
+
price: Price
|
|
62
|
+
rules: Mapping[str, Any]
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _decimal(value: Any) -> Decimal | None:
|
|
66
|
+
if value is None:
|
|
67
|
+
return None
|
|
68
|
+
try:
|
|
69
|
+
result = Decimal(str(value))
|
|
70
|
+
except (InvalidOperation, ValueError, TypeError):
|
|
71
|
+
return None
|
|
72
|
+
return result if result.is_finite() else None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _tokens(value: Any) -> int | None:
|
|
76
|
+
if value is None or isinstance(value, bool):
|
|
77
|
+
return None
|
|
78
|
+
try:
|
|
79
|
+
result = int(value)
|
|
80
|
+
except (ValueError, TypeError, OverflowError):
|
|
81
|
+
return None
|
|
82
|
+
return result if result >= 0 else None
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class CatalogSnapshot:
|
|
86
|
+
def __init__(
|
|
87
|
+
self,
|
|
88
|
+
aliases: Mapping[tuple[str, str], Alias],
|
|
89
|
+
prices: list[Price],
|
|
90
|
+
*,
|
|
91
|
+
region: str,
|
|
92
|
+
) -> None:
|
|
93
|
+
self._aliases = dict(aliases)
|
|
94
|
+
self._prices: dict[tuple[str, str], list[Price]] = {}
|
|
95
|
+
self._deployment_aliases: dict[tuple[str, str], Alias] = {}
|
|
96
|
+
self._region = region.strip().lower()
|
|
97
|
+
for (_, observed_model), alias in aliases.items():
|
|
98
|
+
channel = alias.pricing_channel.strip().lower()
|
|
99
|
+
for model in (observed_model, alias.canonical_id):
|
|
100
|
+
key = (model.strip().lower(), channel)
|
|
101
|
+
existing = self._deployment_aliases.get(key)
|
|
102
|
+
if existing is not None and (
|
|
103
|
+
existing.canonical_id != alias.canonical_id
|
|
104
|
+
or dict(existing.rules) != dict(alias.rules)
|
|
105
|
+
):
|
|
106
|
+
raise ValueError(
|
|
107
|
+
f"ambiguous deployment alias {model!r} for channel {channel!r}"
|
|
108
|
+
)
|
|
109
|
+
self._deployment_aliases[key] = alias
|
|
110
|
+
for price in prices:
|
|
111
|
+
self._prices.setdefault((price.model_id, price.pricing_channel), []).append(
|
|
112
|
+
price
|
|
113
|
+
)
|
|
114
|
+
for candidates in self._prices.values():
|
|
115
|
+
candidates.sort(key=lambda price: price.effective_from, reverse=True)
|
|
116
|
+
|
|
117
|
+
def _price_for(self, alias: Alias, at: datetime) -> Price | None:
|
|
118
|
+
at = at if at.tzinfo else at.replace(tzinfo=timezone.utc)
|
|
119
|
+
candidates = self._prices.get((alias.model_id, alias.pricing_channel), [])
|
|
120
|
+
for region in dict.fromkeys((self._region, "*", "global")):
|
|
121
|
+
for price in candidates:
|
|
122
|
+
if price.region.lower() != region:
|
|
123
|
+
continue
|
|
124
|
+
if price.effective_from <= at and (
|
|
125
|
+
price.effective_to is None or at < price.effective_to
|
|
126
|
+
):
|
|
127
|
+
return price
|
|
128
|
+
return None
|
|
129
|
+
|
|
130
|
+
def resolve_price(
|
|
131
|
+
self, *, model: Any, channel: Any, at: datetime
|
|
132
|
+
) -> ResolvedPrice | None:
|
|
133
|
+
"""Resolve pricing for an explicitly selected model and channel.
|
|
134
|
+
|
|
135
|
+
This is intended for planners and evaluation pipelines that know the
|
|
136
|
+
deployment channel before making a provider call. It never falls back
|
|
137
|
+
to pricing from a different channel.
|
|
138
|
+
"""
|
|
139
|
+
|
|
140
|
+
model_key = str(model or "").strip().lower()
|
|
141
|
+
channel_key = str(channel or "").strip().lower()
|
|
142
|
+
alias = self._deployment_aliases.get((model_key, channel_key))
|
|
143
|
+
if alias is None:
|
|
144
|
+
return None
|
|
145
|
+
price = self._price_for(alias, at)
|
|
146
|
+
if price is None:
|
|
147
|
+
return None
|
|
148
|
+
return ResolvedPrice(
|
|
149
|
+
canonical_model=alias.canonical_id,
|
|
150
|
+
price=price,
|
|
151
|
+
rules=MappingProxyType({**price.rules, **alias.rules}),
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
def cost(
|
|
155
|
+
self,
|
|
156
|
+
*,
|
|
157
|
+
provider: Any,
|
|
158
|
+
model: Any,
|
|
159
|
+
at: datetime,
|
|
160
|
+
input_tokens: Any,
|
|
161
|
+
output_tokens: Any,
|
|
162
|
+
cache_read_tokens: Any = None,
|
|
163
|
+
cache_write_tokens: Any = None,
|
|
164
|
+
batch: bool = False,
|
|
165
|
+
) -> CostResult:
|
|
166
|
+
provider_key = str(provider or "").strip().lower()
|
|
167
|
+
provider_key = _PROVIDER_ALIASES.get(provider_key, provider_key)
|
|
168
|
+
model_key = str(model or "").strip().lower()
|
|
169
|
+
alias = self._aliases.get((provider_key, model_key))
|
|
170
|
+
if alias is None:
|
|
171
|
+
return CostResult(None, None, None, "unpriced", ("unknown_model",))
|
|
172
|
+
price = self._price_for(alias, at)
|
|
173
|
+
if price is None:
|
|
174
|
+
return CostResult(
|
|
175
|
+
alias.canonical_id,
|
|
176
|
+
None,
|
|
177
|
+
None,
|
|
178
|
+
"unpriced",
|
|
179
|
+
("no_effective_price",),
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
reasons: list[str] = []
|
|
183
|
+
input_count = _tokens(input_tokens)
|
|
184
|
+
output_count = _tokens(output_tokens)
|
|
185
|
+
cache_read_count = _tokens(cache_read_tokens) or 0
|
|
186
|
+
cache_write_count = _tokens(cache_write_tokens) or 0
|
|
187
|
+
if input_count is None:
|
|
188
|
+
reasons.append("missing_input_tokens")
|
|
189
|
+
input_count = 0
|
|
190
|
+
if output_count is None:
|
|
191
|
+
reasons.append("missing_output_tokens")
|
|
192
|
+
output_count = 0
|
|
193
|
+
|
|
194
|
+
rules = {**price.rules, **alias.rules}
|
|
195
|
+
billable_input = input_count
|
|
196
|
+
deducted_input = 0
|
|
197
|
+
if rules.get("input_includes_cache_read"):
|
|
198
|
+
if cache_read_count > input_count:
|
|
199
|
+
reasons.append("cache_read_exceeds_input")
|
|
200
|
+
deducted_input = input_count
|
|
201
|
+
else:
|
|
202
|
+
deducted_input += cache_read_count
|
|
203
|
+
if rules.get("input_includes_cache_write"):
|
|
204
|
+
if cache_write_count > input_count - deducted_input:
|
|
205
|
+
reasons.append("cache_write_exceeds_input")
|
|
206
|
+
deducted_input = input_count
|
|
207
|
+
else:
|
|
208
|
+
deducted_input += cache_write_count
|
|
209
|
+
billable_input -= deducted_input
|
|
210
|
+
|
|
211
|
+
input_rate = price.input_per_mtok
|
|
212
|
+
output_rate = price.output_per_mtok
|
|
213
|
+
if batch:
|
|
214
|
+
if (
|
|
215
|
+
price.batch_input_per_mtok is None
|
|
216
|
+
or price.batch_output_per_mtok is None
|
|
217
|
+
):
|
|
218
|
+
reasons.append("batch_rate_unavailable")
|
|
219
|
+
else:
|
|
220
|
+
input_rate = price.batch_input_per_mtok
|
|
221
|
+
output_rate = price.batch_output_per_mtok
|
|
222
|
+
|
|
223
|
+
input_multiplier = Decimal("1")
|
|
224
|
+
output_multiplier = Decimal("1")
|
|
225
|
+
long_context = rules.get("long_context") or {}
|
|
226
|
+
threshold = _tokens(long_context.get("threshold"))
|
|
227
|
+
if threshold is not None and input_count > threshold:
|
|
228
|
+
input_multiplier = _decimal(
|
|
229
|
+
long_context.get("input_multiplier")
|
|
230
|
+
) or Decimal("1")
|
|
231
|
+
output_multiplier = _decimal(
|
|
232
|
+
long_context.get("output_multiplier")
|
|
233
|
+
) or Decimal("1")
|
|
234
|
+
|
|
235
|
+
cost = Decimal("0")
|
|
236
|
+
if input_rate is None:
|
|
237
|
+
if billable_input:
|
|
238
|
+
reasons.append("input_rate_unavailable")
|
|
239
|
+
else:
|
|
240
|
+
cost += Decimal(billable_input) * input_rate * input_multiplier / _MILLION
|
|
241
|
+
if output_rate is None:
|
|
242
|
+
if output_count:
|
|
243
|
+
reasons.append("output_rate_unavailable")
|
|
244
|
+
else:
|
|
245
|
+
cost += Decimal(output_count) * output_rate * output_multiplier / _MILLION
|
|
246
|
+
if cache_read_count:
|
|
247
|
+
if price.cache_read_per_mtok is None:
|
|
248
|
+
reasons.append("cache_read_rate_unavailable")
|
|
249
|
+
else:
|
|
250
|
+
cost += (
|
|
251
|
+
Decimal(cache_read_count)
|
|
252
|
+
* price.cache_read_per_mtok
|
|
253
|
+
* input_multiplier
|
|
254
|
+
/ _MILLION
|
|
255
|
+
)
|
|
256
|
+
if cache_write_count:
|
|
257
|
+
if price.cache_write_5m_per_mtok is None:
|
|
258
|
+
reasons.append("cache_write_rate_unavailable")
|
|
259
|
+
else:
|
|
260
|
+
cost += (
|
|
261
|
+
Decimal(cache_write_count)
|
|
262
|
+
* price.cache_write_5m_per_mtok
|
|
263
|
+
* input_multiplier
|
|
264
|
+
/ _MILLION
|
|
265
|
+
)
|
|
266
|
+
if rules.get("uncaptured_fees"):
|
|
267
|
+
reasons.append("uncaptured_fees")
|
|
268
|
+
|
|
269
|
+
return CostResult(
|
|
270
|
+
alias.canonical_id,
|
|
271
|
+
price.id,
|
|
272
|
+
cost.quantize(_COST_QUANTUM, rounding=ROUND_HALF_UP),
|
|
273
|
+
"partial" if reasons else "priced",
|
|
274
|
+
tuple(dict.fromkeys(reasons)),
|
|
275
|
+
)
|