MCPcopy Create free account
hub / github.com/InferCore/InferCore / optimizeByCost

Method optimizeByCost

internal/router/rule_router.go:109–150  ·  view source on GitHub ↗
(tenant config.TenantConfig, req types.AIRequest, base types.RouteDecision, state types.RuntimeState)

Source from the content-addressed store, hash-verified

107}
108
109func (r *RuleRouter) optimizeByCost(tenant config.TenantConfig, req types.AIRequest, base types.RouteDecision, state types.RuntimeState) types.RouteDecision {
110 if r.costEngine == nil {
111 return base
112 }
113 if state.OverloadDegrade {
114 return base
115 }
116
117 best := base
118 bestCost := base.EstimatedCost
119
120 for _, backend := range r.cfg.Backends {
121 if !backendSelectable(state, backend.Name) {
122 continue
123 }
124 if !supportsTask(backend, req.TaskType) {
125 continue
126 }
127
128 estimate := r.costEngine.Estimate(req, types.BackendMetadata{
129 Name: backend.Name,
130 Type: backend.Type,
131 Capabilities: backend.Capabilities,
132 CostUnit: backend.Cost.Unit,
133 MaxConcurrency: backend.MaxConcurrency,
134 })
135 if tenant.BudgetPerRequest > 0 && estimate.EstimatedTotal > tenant.BudgetPerRequest {
136 continue
137 }
138
139 if estimate.EstimatedTotal < bestCost {
140 best = types.RouteDecision{
141 BackendName: backend.Name,
142 Reason: "cost-optimized-from-" + base.Reason,
143 EstimatedCost: estimate.EstimatedTotal,
144 }
145 bestCost = estimate.EstimatedTotal
146 }
147 }
148
149 return best
150}
151
152func supportsTask(backend config.BackendConfig, taskType string) bool {
153 if taskType == "" {

Callers 1

SelectRouteMethod · 0.95

Calls 3

backendSelectableFunction · 0.85
supportsTaskFunction · 0.85
EstimateMethod · 0.65

Tested by

no test coverage detected