관리자 사용량 근거를 정교화

This commit is contained in:
Yun Chan 2026-08-29 23:59:17 +09:00
parent ccdcfcd2f5
commit 707dba4f8f
10 changed files with 1136 additions and 209 deletions

View file

@ -66,6 +66,8 @@ def build_model_cost_report(usage: Mapping[str, Any]) -> dict[str, Any]:
by_provider = list(usage.get("by_provider") or [])
budget = dict(usage.get("budget") or {})
evaluator_cache = dict(usage.get("evaluator_cache") or {})
total_cost_basis = str(usage.get("cost_basis") or "provider_reported")
exact_cost_bases = {"provider_estimate", "provider_reported", "reference_rate"}
models: list[dict[str, Any]] = []
for item in by_provider:
@ -87,6 +89,12 @@ def build_model_cost_report(usage: Mapping[str, Any]) -> dict[str, Any]:
_number(row.get("recorded_cost_usd"), row_cost), 6
)
row_estimated_cost = round(_number(row.get("estimated_cost_usd")), 6)
row_cost_basis = str(row.get("cost_basis") or "provider_reported")
row_cost_complete = row_cost_basis not in {
"partial",
"partial_upper_bound",
"unavailable",
}
models.append(
{
"provider": str(row.get("provider") or "unknown"),
@ -100,13 +108,24 @@ def build_model_cost_report(usage: Mapping[str, Any]) -> dict[str, Any]:
"cost_usd": row_cost,
"recorded_cost_usd": row_recorded_cost,
"estimated_cost_usd": row_estimated_cost,
"cost_basis": str(row.get("cost_basis") or "provider_reported"),
"cost_basis": row_cost_basis,
"rate_label": row.get("rate_label"),
"rate_source_url": row.get("rate_source_url"),
"cost_share": _ratio(row_cost, total_cost),
"cost_share": (
_ratio(row_cost, total_cost)
if total_cost_basis in exact_cost_bases
and row_cost_basis in exact_cost_bases
else None
),
"token_share": _ratio(float(row_tokens), float(total_tokens)),
"cost_per_turn_usd": _cost_per_turn(row_cost, turns),
"cost_per_1k_tokens_usd": _cost_per_1k_tokens(row_cost, row_tokens),
"cost_per_turn_usd": (
_cost_per_turn(row_cost, turns) if row_cost_complete else None
),
"cost_per_1k_tokens_usd": (
_cost_per_1k_tokens(row_cost, row_tokens)
if row_cost_complete
else None
),
}
)
models.sort(key=lambda item: (-float(item["cost_usd"]), item["provider"], item["model"]))
@ -120,15 +139,28 @@ def build_model_cost_report(usage: Mapping[str, Any]) -> dict[str, Any]:
warnings.append("zero_cost_metered_usage")
if estimated_cost > 0:
warnings.append("reference_rate_cost")
if any(item["cost_basis"] == "unavailable" for item in models):
if any(item["cost_basis"] == "partial" for item in models):
warnings.append("partial_model_cost")
if any(item["cost_basis"] == "partial_upper_bound" for item in models):
warnings.append("partial_upper_bound_model_cost")
if any(item["cost_basis"] == "reference_upper_bound" for item in models):
warnings.append("reference_upper_bound_cost")
if any(
item["cost_basis"] in {"partial", "partial_upper_bound", "unavailable"}
for item in models
):
warnings.append("unavailable_model_cost")
if str(budget.get("status") or "") in {"warn", "exceeded"}:
if str(budget.get("status") or "") in {"warn", "exceeded", "indeterminate"}:
warnings.append(f"budget_{budget.get('status')}")
cache_hit_rate = _number(evaluator_cache.get("hit_rate"))
if bool(evaluator_cache.get("enabled")) and _integer(evaluator_cache.get("requests")) > 0:
if cache_hit_rate < 0.25:
warnings.append("low_evaluator_cache_hit_rate")
if models and models[0]["cost_share"] >= 0.8:
if (
models
and isinstance(models[0]["cost_share"], (float, int))
and models[0]["cost_share"] >= 0.8
):
warnings.append("dominant_model_cost")
return {
@ -152,14 +184,28 @@ def build_model_cost_report(usage: Mapping[str, Any]) -> dict[str, Any]:
"cost_usd": total_cost,
"recorded_cost_usd": recorded_cost,
"estimated_cost_usd": estimated_cost,
"cost_per_turn_usd": _cost_per_turn(total_cost, metered_turns),
"cost_per_1k_tokens_usd": _cost_per_1k_tokens(total_cost, total_tokens),
"cost_basis": total_cost_basis,
"cost_per_turn_usd": (
_cost_per_turn(total_cost, metered_turns)
if total_cost_basis not in {"partial", "partial_upper_bound", "unavailable"}
else None
),
"cost_per_1k_tokens_usd": (
_cost_per_1k_tokens(total_cost, total_tokens)
if total_cost_basis not in {"partial", "partial_upper_bound", "unavailable"}
else None
),
},
"budget": {
"status": str(budget.get("status") or "disabled"),
"limit_usd": round(_number(budget.get("limit_usd")), 6),
"used_ratio": round(_number(budget.get("used_ratio")), 6),
"remaining_usd": round(_number(budget.get("remaining_usd")), 6),
"remaining_usd": (
round(_number(budget.get("remaining_usd")), 6)
if budget.get("remaining_usd") is not None
else None
),
"cost_basis": str(budget.get("cost_basis") or total_cost_basis),
},
"evaluator_cache": {
"enabled": bool(evaluator_cache.get("enabled")),