Add warning when model price is not available in pricing.json
- Modified get_model_prices() in pricing.py to return is_default flag - Updated CLI to display warning in all output modes (default, details, JSON) - Warning format: '**model price not available**' on new line - Updated all tests to handle 4-tuple return value - Works with refactored provider-based pricing structure Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -249,22 +249,26 @@ allowlist = ["agent-cost-estimator"]
|
||||
|
||||
|
||||
def effective_rates(args, pricing, meta):
|
||||
"""Return (rates, key, currency) after table lookup and CLI overrides."""
|
||||
"""Return (rates, key, currency, is_default) after table lookup and CLI overrides."""
|
||||
result = get_model_prices(pricing, args.model or meta.model_name,
|
||||
meta.alias)
|
||||
# Handle both old (2-tuple) and new (3-tuple) return values
|
||||
if len(result) == 3:
|
||||
# Handle different return value counts for backward compatibility
|
||||
if len(result) == 4:
|
||||
rates, key, currency, is_default = result
|
||||
elif len(result) == 3:
|
||||
rates, key, currency = result
|
||||
is_default = False
|
||||
else:
|
||||
rates, key = result
|
||||
currency = pricing.get("currency", "USD")
|
||||
is_default = False
|
||||
overrides = {
|
||||
"input_price": args.input_price,
|
||||
"cached_input_price": args.cached_input_price,
|
||||
"output_price": args.output_price,
|
||||
}
|
||||
rates.update({k: v for k, v in overrides.items() if v is not None})
|
||||
return rates, key, currency
|
||||
return rates, key, currency, is_default
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
@@ -309,7 +313,17 @@ def main(argv=None):
|
||||
meta = harness.read_meta(session_dir)
|
||||
usage = harness.collect_usage(session_dir)
|
||||
pricing = load_pricing(pricing_path)
|
||||
rates, key, currency = effective_rates(args, pricing, meta)
|
||||
result = effective_rates(args, pricing, meta)
|
||||
# Handle different return value counts
|
||||
if len(result) == 4:
|
||||
rates, key, currency, is_default = result
|
||||
elif len(result) == 3:
|
||||
rates, key, currency = result
|
||||
is_default = False
|
||||
else:
|
||||
rates, key = result
|
||||
currency = "USD"
|
||||
is_default = False
|
||||
except (FileNotFoundError, TypeError, ValueError) as exc:
|
||||
print(f"error: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
@@ -364,6 +378,7 @@ def main(argv=None):
|
||||
},
|
||||
"usage_source": usage.usage_source,
|
||||
"cost_usd": cost,
|
||||
"model_price_unavailable": is_default,
|
||||
"last_turn": {
|
||||
"label": last_turn.label if last_turn else None,
|
||||
"cost_usd": turn_cost,
|
||||
@@ -386,13 +401,17 @@ def main(argv=None):
|
||||
return 0
|
||||
if not args.details:
|
||||
# default: provider:model | session:$xxx | turn:$yyy
|
||||
print(f"{key} | session:${cost['total_usd']:.2f} | "
|
||||
f"turn:${turn_cost['total_usd']:.2f}")
|
||||
output = f"{key} | session:${cost['total_usd']:.2f} | turn:${turn_cost['total_usd']:.2f}"
|
||||
if is_default:
|
||||
output += "\n **model price not available**"
|
||||
print(output)
|
||||
return 0
|
||||
label = f" ({meta.title})" if meta.title else ""
|
||||
print(f"harness: {harness.name}")
|
||||
print(f"session: {session_dir.name}{label}")
|
||||
print(f"model: {key}") # provider:model from pricing lookup
|
||||
if is_default:
|
||||
print(" **model price not available**")
|
||||
print(f"model calls: {usage.calls} "
|
||||
f"(window on disk; pruned calls not counted)")
|
||||
print(f"usage source: {usage.usage_source} "
|
||||
|
||||
@@ -192,7 +192,7 @@ def _lookup_in_providers(data: dict, provider: str | None, model_name: str, alia
|
||||
|
||||
|
||||
def get_model_prices(pricing, model_name=None, alias=None, provider=None):
|
||||
"""Return (rates dict, matched key, currency) for the model.
|
||||
"""Return (rates dict, matched key, currency, is_default) for the model.
|
||||
|
||||
Lookup order:
|
||||
1. If provider specified: provider.model_name, provider.alias, provider.default
|
||||
@@ -203,7 +203,8 @@ def get_model_prices(pricing, model_name=None, alias=None, provider=None):
|
||||
Raises ValueError when nothing matches and there is no
|
||||
"default".
|
||||
|
||||
Returns tuple of (rates_dict, matched_key, currency_str)
|
||||
Returns tuple of (rates_dict, matched_key, currency_str, is_default_bool)
|
||||
is_default is True if we fell back to a default model.
|
||||
"""
|
||||
providers = pricing.get("providers")
|
||||
if not providers:
|
||||
@@ -213,7 +214,9 @@ def get_model_prices(pricing, model_name=None, alias=None, provider=None):
|
||||
if rates:
|
||||
prov_name = _extract_model_from_key(matched_key)[0]
|
||||
currency = _get_currency(pricing, prov_name, model_name)
|
||||
return rates, matched_key, currency
|
||||
# Check if we matched a "default" model
|
||||
is_default = matched_key.endswith(":default")
|
||||
return rates, matched_key, currency, is_default
|
||||
|
||||
raise ValueError(
|
||||
f"no pricing for model {model_name!r} or {alias!r} and no "
|
||||
|
||||
@@ -146,15 +146,17 @@ def test_pricing_lookup_order(tmp_path):
|
||||
"output_price": 7},
|
||||
})
|
||||
data = pricing.load_pricing(path)
|
||||
rates, key, currency = pricing.get_model_prices(data, "zai-glm-5-3", "glm-5-3")
|
||||
rates, key, currency, is_default = pricing.get_model_prices(data, "zai-glm-5-3", "glm-5-3")
|
||||
assert key == "default:zai-glm-5-3"
|
||||
assert rates["input_price"] == 3
|
||||
rates, key, currency = pricing.get_model_prices(data, None, "glm-5-3")
|
||||
rates, key, currency, is_default = pricing.get_model_prices(data, None, "glm-5-3")
|
||||
assert key == "default:glm-5-3"
|
||||
assert rates["input_price"] == 2
|
||||
rates, key, currency = pricing.get_model_prices(data, "unknown", "also-unknown")
|
||||
rates, key, currency, is_default = pricing.get_model_prices(data, "unknown", "also-unknown")
|
||||
assert key == "default:default"
|
||||
assert rates["input_price"] == 1
|
||||
# Verify fallback to default is detected
|
||||
assert is_default == True
|
||||
|
||||
|
||||
def test_pricing_validation(tmp_path):
|
||||
@@ -435,8 +437,10 @@ def test_cli_default_compact_line(tmp_path, capsys):
|
||||
turn_cost = (500_000 * 1.5 + 500_000 * 0.15
|
||||
+ 100_000 * 7.5) / 1e6
|
||||
# Output format is now: provider:model | session:$X | turn:$Y
|
||||
assert out == (f"default:default | session:${session_cost:.2f} | "
|
||||
f"turn:${turn_cost:.2f}")
|
||||
# Model 'm1' is not in pricing, so it falls back to default with warning
|
||||
expected = (f"default:default | session:${session_cost:.2f} | "
|
||||
f"turn:${turn_cost:.2f}\n **model price not available**")
|
||||
assert out == expected
|
||||
|
||||
|
||||
def test_cli_details_shows_the_full_table(tmp_path, capsys):
|
||||
|
||||
Reference in New Issue
Block a user