"""Offline ledger from Qwen guide; dated Singapore rates, no API calls.""" from decimal import Decimal def token_bill(ordinary, cached, output): counts = (ordinary, cached, output) if any(type(n) is not int or n < 0 for n in counts): raise ValueError("Token counts must be non-negative integers") rates = (Decimal("0.15"), Decimal("0.016"), Decimal("0.47")) return sum((Decimal(n) * r / Decimal(1_000_000) for n, r in zip(counts, rates)), Decimal("0")) def accepted_cost(model_cost, review_minutes, hourly_rate, accepted): if type(accepted) is not int or accepted <= 0: raise ValueError("Accepted count must be a positive integer") money = tuple(Decimal(str(x)) for x in (model_cost, review_minutes, hourly_rate)) if any(not x.is_finite() or x < 0 for x in money): raise ValueError("Costs and review values must be finite and non-negative") model, minutes, rate = money return (model + minutes * rate / Decimal(60)) / Decimal(accepted) assert token_bill(12_000_000, 8_000_000, 2_000_000) == Decimal("2.868") assert token_bill(20_000_000, 0, 2_000_000) == Decimal("3.94") assert accepted_cost("2.868", 75, 24, 100) == Decimal("0.32868") assert token_bill(0, 0, 0) == 0 for invalid in (-1, 1.5, True): try: token_bill(invalid, 0, 0) except ValueError: pass else: raise AssertionError("Invalid token count was accepted") print("token bill:", token_bill(12_000_000, 8_000_000, 2_000_000)) print("accepted artifact cost:", accepted_cost("2.868", 75, 24, 100))