fix: add per-image cost tracking for DALL-E and Imagen
CI / Lint (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Build (push) Has been cancelled

This commit is contained in:
2026-04-27 10:42:29 -04:00
parent b1a72f5a10
commit 7446f3463d
2 changed files with 37 additions and 1 deletions
+36
View File
@@ -564,15 +564,51 @@ func (s *Server) handleImageGenerations(c *gin.Context) {
promptTokens = 1
}
// Calculate per-image cost (not per-token like chat)
cost := imageGenCost(providerName, req.Model, req.Size, uint32(len(resp.Data)))
s.logRequest(startTime, clientID, providerName, req.Model, &models.Usage{
PromptTokens: promptTokens,
CompletionTokens: uint32(len(resp.Data)),
TotalTokens: promptTokens + uint32(len(resp.Data)),
}, nil, false)
// Update cost in DB — image gen is per-image, not per-token
if cost > 0 {
s.database.Exec("UPDATE llm_requests SET cost = ? WHERE id = (SELECT MAX(id) FROM llm_requests)", cost)
}
c.JSON(http.StatusOK, resp)
}
// imageGenCost returns per-image pricing for known image generation models.
func imageGenCost(provider, model string, size *string, n uint32) float64 {
if n == 0 {
return 0
}
modelLower := strings.ToLower(model)
var perImage float64
switch {
case strings.Contains(modelLower, "dall-e-3"):
perImage = 0.040 // standard 1024x1024
if size != nil {
s := *size
if s == "1024x1792" || s == "1792x1024" {
perImage = 0.080
}
}
case strings.Contains(modelLower, "dall-e-2"):
perImage = 0.020
case strings.Contains(modelLower, "imagen"):
perImage = 0.040 // approximate
default:
return 0
}
return perImage * float64(n)
}
func (s *Server) logRequest(start time.Time, clientID, provider, model string, usage *models.Usage, err error, hasImages bool) {
entry := RequestLog{
Timestamp: start,