Files

42 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""提供商价格表与成本换算ARCH §4.8)。
价格以「每百万 token 的最小货币单位(如分/cent」表示随 provider 配置维护;
未知 (provider, model) 则成本计 0仍记账便于观测
"""
from __future__ import annotations
import math
from dataclasses import dataclass
import structlog
log = structlog.get_logger(__name__)
@dataclass(frozen=True)
class Price:
in_per_mtok: int
out_per_mtok: int
currency: str
# 近似价(可后续移入 config / provider 配置维护)
_PRICING: dict[tuple[str, str], Price] = {
("deepseek", "deepseek-chat"): Price(in_per_mtok=27, out_per_mtok=110, currency="USD"),
}
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
+ output_tokens / 1_000_000 * price.out_per_mtok
)
return cost, price.currency