fix(usage): close sub-cent gaps found in review

- Insights formatters now route aggregate estimated cost through the
  shared format_cost_label() instead of hardcoded 2dp — a sub-cent
  aggregate (one cheap DeepSeek session, ~$0.0046) no longer renders
  'Estimated: ~$0.00', the exact bug class this PR fixes (#79220).
- format_cost_label: positive amounts below $0.00005 render '~$<0.0001'
  instead of the zero-looking '~$0.0000' 4dp truncation artifact.
- Renamed _format_cost_label -> format_cost_label (now a cross-module
  shared helper).
- Tests: renamed test_gateway_format_hides_cost ->
  test_gateway_format_hides_cache_details and
  test_no_cost_section_when_all_zero ->
  test_unknown_bucket_shown_for_costless_session (names contradicted
  behavior); restored a real assertion in the custom-models test that
  had been weakened to a comment; added sub-cent-aggregate and 4dp-floor
  contract tests (mutation-checked).
This commit is contained in:
kshitij
2026-08-14 01:18:34 +05:30
parent ccaaca7e66
commit 2c068d7680
4 changed files with 77 additions and 21 deletions
+12 -4
View File
@@ -23,22 +23,30 @@ _NOUS_DEFAULT_BASE_URL = "https://inference-api.nousresearch.com/v1"
_SUBCENT_THRESHOLD = Decimal("0.01")
def _format_cost_label(amount: Decimal) -> str:
def format_cost_label(amount: Decimal) -> str:
"""Format a cost amount as a display label.
Scales precision to magnitude:
- Zero → "$0.00"
- Sub-cent (< $0.01) → "~$0.0046" (4 dp, always non-zero)
- Sub-cent (< $0.01) → "~$0.0046" (4 dp; below $0.00005 falls back
to "~$<0.0001" so the label never reads as zero)
- Normal → "~$1.23" (2 dp)
This fixes #79220 where sub-cent per-turn costs on cheap models
(DeepSeek, etc.) rendered as "$0.00" despite amount_usd carrying
full Decimal precision.
Shared by per-response cost labels (estimate_usage_cost) and the
insights cost-bucket formatters — keep both surfaces on this one
implementation so sub-cent honesty can't regress on one of them.
"""
if amount == _ZERO:
return "$0.00"
if amount < _SUBCENT_THRESHOLD:
return f"~${amount:.4f}"
label = f"~${amount:.4f}"
# 4dp truncation of a positive amount below $0.00005 would render
# "~$0.0000" — a zero-looking label, the exact #79220 dishonesty.
return label if label != "~$0.0000" else "~$<0.0001"
return f"~${amount:.2f}"
CostStatus = Literal["actual", "estimated", "included", "unknown"]
@@ -1402,7 +1410,7 @@ def estimate_usage_cost(
amount += Decimal(usage.request_count) * entry.request_cost
status: CostStatus = "estimated"
label = _format_cost_label(amount)
label = format_cost_label(amount)
if entry.source == "none" and amount == _ZERO:
status = "included"
label = "included"