@@ -347,6 +347,7 @@ func (s *Server) handleAnthropicMessages(w http.ResponseWriter, r *http.Request)
347347
348348 tokensIn , tokensOut := s .proxyAnthropic (w , r , bodyBytes , targetModel , body .Stream )
349349 if s .store != nil && (tokensIn > 0 || tokensOut > 0 ) {
350+ costUSD := s .calculateModelCost (targetModel , tokensIn , tokensOut )
350351 s .store .RecordDecision (& store.RoutingDecision {
351352 Timestamp : time .Now (),
352353 RequestID : fmt .Sprintf ("req_%d" , time .Now ().UnixNano ()),
@@ -358,6 +359,7 @@ func (s *Server) handleAnthropicMessages(w http.ResponseWriter, r *http.Request)
358359 Reason : reason ,
359360 TokensIn : tokensIn ,
360361 TokensOut : tokensOut ,
362+ CostUSD : costUSD ,
361363 HTTPStatus : 200 ,
362364 })
363365 }
@@ -429,6 +431,18 @@ func (s *Server) proxyAnthropic(w http.ResponseWriter, r *http.Request, bodyByte
429431 return s .extractAnthropicTokens (buf .Bytes ())
430432}
431433
434+ func (s * Server ) calculateModelCost (model string , tokensIn , tokensOut int ) float64 {
435+ for _ , p := range s .registry .List () {
436+ models , _ := p .ListModels (context .Background ())
437+ for _ , m := range models {
438+ if m .ID == model && (m .CostPer1KInput > 0 || m .CostPer1KOutput > 0 ) {
439+ return float64 (tokensIn )/ 1000 * m .CostPer1KInput + float64 (tokensOut )/ 1000 * m .CostPer1KOutput
440+ }
441+ }
442+ }
443+ return 0
444+ }
445+
432446func (s * Server ) extractAnthropicTokens (data []byte ) (int , int ) {
433447 var resp struct {
434448 Usage struct {
0 commit comments