diff --git a/hermes_cli/models.py b/hermes_cli/models.py index 0383f9ae5b..28cf578fbf 100644 --- a/hermes_cli/models.py +++ b/hermes_cli/models.py @@ -1695,13 +1695,19 @@ def _format_price_per_mtok(per_token_str: str) -> str: Always uses 2 decimal places so that prices align vertically when right-justified in a column (the decimal point stays in the same position). + Sub-cent prices (e.g. deep-discount cache-hit promos) extend precision + instead of collapsing to "$0.00": the smallest decimal place that makes + the value non-zero is found, then one extra digit is kept and trailing + zeros trimmed. + Examples: - "0.000003" → "$3.00" (per million tokens) - "0.00003" → "$30.00" - "0.00000015" → "$0.15" - "0.0000001" → "$0.10" - "0.00018" → "$180.00" - "0" → "free" + "0.000003" → "$3.00" (per million tokens) + "0.00003" → "$30.00" + "0.00000015" → "$0.15" + "0.0000001" → "$0.10" + "0.00018" → "$180.00" + "0.0000000018" → "$0.0018" (promo: $0.0018/Mtok) + "0" → "free" """ try: val = float(per_token_str) @@ -1710,7 +1716,15 @@ def _format_price_per_mtok(per_token_str: str) -> str: if val == 0: return "free" per_m = val * 1_000_000 - return f"${per_m:.2f}" + text = f"{per_m:.2f}" + if per_m < 0.01: + # Non-zero price below one cent per Mtok — widen precision until the + # value shows, keep one extra significant digit, trim trailing zeros. + prec = 3 + while prec < 12 and round(per_m, prec) == 0: + prec += 1 + text = f"{per_m:.{min(prec + 1, 12)}f}".rstrip("0").rstrip(".") + return f"${text}" def compute_sale_discount( diff --git a/tests/hermes_cli/test_models.py b/tests/hermes_cli/test_models.py index 17e52b6d2d..db14b3ed3c 100644 --- a/tests/hermes_cli/test_models.py +++ b/tests/hermes_cli/test_models.py @@ -448,3 +448,38 @@ class TestClaudeSonnet5InCuratedLists: assert "claude-sonnet-5" in _PROVIDER_MODELS["anthropic"] + + +class TestFormatPricePerMtok: + """_format_price_per_mtok: sub-cent prices must not collapse to 'free'/'$0.00'.""" + + def test_standard_prices_keep_two_decimals(self): + from hermes_cli.models import _format_price_per_mtok + assert _format_price_per_mtok("0.000003") == "$3.00" + assert _format_price_per_mtok("0.00003") == "$30.00" + assert _format_price_per_mtok("0.00000015") == "$0.15" + assert _format_price_per_mtok("0.00018") == "$180.00" + + def test_zero_is_free(self): + from hermes_cli.models import _format_price_per_mtok + assert _format_price_per_mtok("0") == "free" + assert _format_price_per_mtok("0.0") == "free" + + def test_invalid_is_question_mark(self): + from hermes_cli.models import _format_price_per_mtok + assert _format_price_per_mtok("garbage") == "?" + assert _format_price_per_mtok(None) == "?" + + def test_sub_cent_price_extends_precision(self): + from hermes_cli.models import _format_price_per_mtok + # DeepSeek V4 Flash 0731 promo cache-hit rate: $0.0018/Mtok. + assert _format_price_per_mtok("0.0000000018") == "$0.0018" + assert _format_price_per_mtok("0.000000001") == "$0.001" + assert _format_price_per_mtok("0.0000000049") == "$0.0049" + assert _format_price_per_mtok("0.000000005") == "$0.005" + # Tiny but non-zero must never render as free or $0.00. + assert _format_price_per_mtok("0.00000000001") == "$0.00001" + + def test_one_cent_boundary_stays_two_decimals(self): + from hermes_cli.models import _format_price_per_mtok + assert _format_price_per_mtok("0.00000001") == "$0.01"