feat(gateway): 未知模型缺价发结构化告警——成本账缺项可观测(CR-M2-1)
This commit is contained in:
29
packages/llm_gateway/tests/test_pricing.py
Normal file
29
packages/llm_gateway/tests/test_pricing.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""pricing 单元测试(CR-M2-1:未知模型缺价应可观测)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import structlog
|
||||
from ww_llm_gateway.pricing import cost_minor
|
||||
|
||||
|
||||
def test_known_model_computes_positive_cost() -> None:
|
||||
# Arrange / Act
|
||||
cost, currency = cost_minor("deepseek", "deepseek-chat", 1_000_000, 1_000_000)
|
||||
|
||||
# Assert
|
||||
assert cost > 0
|
||||
assert currency == "USD"
|
||||
|
||||
|
||||
def test_unknown_model_warns_and_still_returns_zero_cost() -> None:
|
||||
# CR-M2-1:未知 (provider, model) 成本仍计 0(照常记账),但必须发结构化 warning
|
||||
# 点名该 model——缺价条目应在成本账路径可见,而非静默吞成 0。
|
||||
with structlog.testing.capture_logs() as logs:
|
||||
cost, currency = cost_minor("mystery", "ghost-model", 1000, 2000)
|
||||
|
||||
assert cost == 0
|
||||
assert currency == "USD"
|
||||
warning = next(entry for entry in logs if entry["event"] == "pricing_unknown_model")
|
||||
assert warning["log_level"] == "warning"
|
||||
assert warning["provider"] == "mystery"
|
||||
assert warning["model"] == "ghost-model"
|
||||
@@ -9,6 +9,10 @@ from __future__ import annotations
|
||||
import math
|
||||
from dataclasses import dataclass
|
||||
|
||||
import structlog
|
||||
|
||||
log = structlog.get_logger(__name__)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Price:
|
||||
@@ -26,6 +30,9 @@ _PRICING: dict[tuple[str, str], Price] = {
|
||||
def cost_minor(provider: str, model: str, input_tokens: int, output_tokens: int) -> tuple[int, str]:
|
||||
price = _PRICING.get((provider, model))
|
||||
if price is None:
|
||||
# 缺价条目应在成本账路径可见(CR-M2-1):成本仍计 0(照常记账不阻断调用),
|
||||
# 但发 warning 点名该 (provider, model),否则漏配价格会静默吞成 0 成本。
|
||||
log.warning("pricing_unknown_model", provider=provider, model=model)
|
||||
return 0, "USD"
|
||||
cost = math.ceil(
|
||||
input_tokens / 1_000_000 * price.in_per_mtok
|
||||
|
||||
Reference in New Issue
Block a user