MCPcopy Create free account
hub / github.com/github/gh-aw / findModelPricing

Function findModelPricing

pkg/cli/model_costs.go:79–141  ·  view source on GitHub ↗
(provider, model string)

Source from the content-addressed store, hash-verified

77}
78
79func findModelPricing(provider, model string) (map[string]float64, bool) {
80 initModelPrices()
81
82 normalizedProvider := normalizeCatalogProvider(provider)
83 normalizedModel := strings.ToLower(strings.TrimSpace(model))
84 comparableModel := normalizeComparableModelID(normalizedModel)
85 if normalizedModel == "" { //nolint:tolowerequalfold
86 return nil, false
87 }
88
89 fullID := normalizedModel
90 if !strings.Contains(fullID, "/") && normalizedProvider != "" {
91 fullID = normalizedProvider + "/" + normalizedModel
92 }
93 comparableFullID := normalizeComparableModelID(fullID)
94
95 for _, record := range modelPriceRecords {
96 if (fullID != "" && record.id == fullID) || (comparableFullID != "" && normalizeComparableModelID(record.id) == comparableFullID) {
97 modelCostsLog.Printf("Exact pricing match: provider=%s, model=%s -> %s", provider, model, record.id)
98 return record.pricing, true
99 }
100 }
101
102 var bestProviderScoped map[string]float64
103 bestProviderScopedLen := -1
104 var bestGeneric map[string]float64
105 bestGenericLen := -1
106
107 for _, record := range modelPriceRecords {
108 comparableRecordModel := normalizeComparableModelID(record.model)
109 if record.model == normalizedModel || comparableRecordModel == comparableModel {
110 if normalizedProvider != "" && record.provider == normalizedProvider {
111 return record.pricing, true
112 }
113 if bestGeneric == nil {
114 bestGeneric = record.pricing
115 }
116 continue
117 }
118
119 if strings.HasPrefix(normalizedModel, record.model) || strings.HasPrefix(comparableModel, comparableRecordModel) {
120 if normalizedProvider != "" && record.provider == normalizedProvider && len(record.model) > bestProviderScopedLen {
121 bestProviderScoped = record.pricing
122 bestProviderScopedLen = len(record.model)
123 }
124 if len(record.model) > bestGenericLen {
125 bestGeneric = record.pricing
126 bestGenericLen = len(record.model)
127 }
128 }
129 }
130
131 if bestProviderScoped != nil {
132 modelCostsLog.Printf("Provider-scoped prefix pricing match: provider=%s, model=%s", provider, model)
133 return bestProviderScoped, true
134 }
135 if bestGeneric != nil {
136 modelCostsLog.Printf("Generic prefix pricing match: provider=%s, model=%s", provider, model)

Callers 2

TestFindModelPricingFunction · 0.70

Calls 5

initModelPricesFunction · 0.85
normalizeCatalogProviderFunction · 0.85
ToLowerMethod · 0.80
PrintfMethod · 0.45

Tested by 1

TestFindModelPricingFunction · 0.56