Skip to content

bug(streaming-ui): sub-cent costs show raw Decimal arithmetic precision (e.g. $0.00711955 instead of $0.0071) #289

@kenotron

Description

@kenotron

Repo: amplifier-module-hooks-streaming-ui
File: amplifier_module_hooks_streaming_ui/__init__.pyformat_cost_usd()

Evidence

Real haiku session output:

└ Input: 12,530 (64% cached) | Output: 145 | Total: 12,675 | Cost: $0.00711955

Expected: $0.0071 (2 sig figs). Got: $0.00711955 (8 digits, 7 decimal places).

Root cause

format_cost_usd used input_exp = -cost.as_tuple().exponent to determine decimal places. This reads stored Decimal arithmetic precision, not display precision. When compute_cost multiplies token counts by rate constants like Decimal("3.000"), Python stores the full intermediate precision (8–12+ places), making input_exp far larger than needed.

Fix (already applied on feat/m0-cost-management)

# Remove input_exp entirely. Use exp_floor for 2 sig figs + strip trailing zeros.
decimal_places = -exp_floor + 1
result = f"${cost:.{decimal_places}f}"
return result.rstrip("0") or "$0.00"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions