Skip to content

Commit 21791da

Browse files
committed
fix: prefer high confidence model opportunities
1 parent 3272256 commit 21791da

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/primer/server/services/finops_service.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -603,18 +603,22 @@ def _build_model_choice_opportunities(
603603
)
604604
)
605605

606-
opportunities.sort(
607-
key=lambda opportunity: (
608-
-opportunity.monthly_savings_estimate,
609-
{"high": 2, "medium": 1, "low": 0}.get(opportunity.confidence, 0),
610-
opportunity.workflow_archetype,
611-
)
612-
)
606+
opportunities.sort(key=_model_choice_opportunity_sort_key)
613607
opportunities = opportunities[:limit]
614608
total_savings = sum(opportunity.monthly_savings_estimate for opportunity in opportunities)
615609
return opportunities, total_savings
616610

617611

612+
def _model_choice_opportunity_sort_key(
613+
opportunity: ModelChoiceOpportunity,
614+
) -> tuple[float, int, str]:
615+
return (
616+
-opportunity.monthly_savings_estimate,
617+
-{"high": 2, "medium": 1, "low": 0}.get(opportunity.confidence, 0),
618+
opportunity.workflow_archetype,
619+
)
620+
621+
618622
# ---------------------------------------------------------------------------
619623
# Cost Forecasting
620624
# ---------------------------------------------------------------------------

tests/test_finops.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from primer.common.config import settings
1111
from primer.common.models import Engineer, SessionFacets, SessionWorkflowProfile, Team
1212
from primer.common.pricing import get_pricing
13+
from primer.common.schemas import ModelChoiceOpportunity
1314

1415
# ---------------------------------------------------------------------------
1516
# Helpers
@@ -766,6 +767,44 @@ def test_team_lead_access(self, client, db_session):
766767
r = client.get("/api/v1/finops/cost-modeling", headers={"x-api-key": key})
767768
assert r.status_code == 200
768769

770+
def test_model_choice_opportunities_sort_high_confidence_first(self):
771+
from primer.server.services.finops_service import _model_choice_opportunity_sort_key
772+
773+
high = ModelChoiceOpportunity(
774+
workflow_archetype="debugging",
775+
current_model="claude-opus-4-20250514",
776+
recommended_model="claude-sonnet-4-5-20250929",
777+
current_session_count=6,
778+
supporting_session_count=8,
779+
current_success_rate=0.82,
780+
recommended_success_rate=0.83,
781+
current_avg_cost=2.4,
782+
recommended_avg_cost=1.2,
783+
period_savings_estimate=3.5,
784+
monthly_savings_estimate=7.0,
785+
confidence="high",
786+
rationale="High-confidence opportunity.",
787+
)
788+
low = ModelChoiceOpportunity(
789+
workflow_archetype="feature_development",
790+
current_model="claude-opus-4-20250514",
791+
recommended_model="claude-sonnet-4-5-20250929",
792+
current_session_count=2,
793+
supporting_session_count=2,
794+
current_success_rate=0.82,
795+
recommended_success_rate=0.82,
796+
current_avg_cost=2.4,
797+
recommended_avg_cost=1.2,
798+
period_savings_estimate=3.5,
799+
monthly_savings_estimate=7.0,
800+
confidence="low",
801+
rationale="Low-confidence opportunity.",
802+
)
803+
804+
ordered = sorted([low, high], key=_model_choice_opportunity_sort_key)
805+
806+
assert ordered[0].confidence == "high"
807+
769808

770809
# ---------------------------------------------------------------------------
771810
# Cost Forecasting

0 commit comments

Comments
 (0)