service_usage.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package ccm
  2. import (
  3. "encoding/json"
  4. "math"
  5. "os"
  6. "regexp"
  7. "sync"
  8. "time"
  9. "github.com/sagernet/sing-box/log"
  10. E "github.com/sagernet/sing/common/exceptions"
  11. )
  12. type UsageStats struct {
  13. RequestCount int `json:"request_count"`
  14. MessagesCount int `json:"messages_count"`
  15. InputTokens int64 `json:"input_tokens"`
  16. OutputTokens int64 `json:"output_tokens"`
  17. CacheReadInputTokens int64 `json:"cache_read_input_tokens"`
  18. CacheCreationInputTokens int64 `json:"cache_creation_input_tokens"`
  19. }
  20. type CostCombination struct {
  21. Model string `json:"model"`
  22. ContextWindow int `json:"context_window"`
  23. Total UsageStats `json:"total"`
  24. ByUser map[string]UsageStats `json:"by_user"`
  25. }
  26. type AggregatedUsage struct {
  27. LastUpdated time.Time `json:"last_updated"`
  28. Combinations []CostCombination `json:"combinations"`
  29. mutex sync.Mutex
  30. filePath string
  31. logger log.ContextLogger
  32. lastSaveTime time.Time
  33. pendingSave bool
  34. saveTimer *time.Timer
  35. saveMutex sync.Mutex
  36. }
  37. type UsageStatsJSON struct {
  38. RequestCount int `json:"request_count"`
  39. MessagesCount int `json:"messages_count"`
  40. InputTokens int64 `json:"input_tokens"`
  41. OutputTokens int64 `json:"output_tokens"`
  42. CacheReadInputTokens int64 `json:"cache_read_input_tokens"`
  43. CacheCreationInputTokens int64 `json:"cache_creation_input_tokens"`
  44. CostUSD float64 `json:"cost_usd"`
  45. }
  46. type CostCombinationJSON struct {
  47. Model string `json:"model"`
  48. ContextWindow int `json:"context_window"`
  49. Total UsageStatsJSON `json:"total"`
  50. ByUser map[string]UsageStatsJSON `json:"by_user"`
  51. }
  52. type CostsSummaryJSON struct {
  53. TotalUSD float64 `json:"total_usd"`
  54. ByUser map[string]float64 `json:"by_user"`
  55. }
  56. type AggregatedUsageJSON struct {
  57. LastUpdated time.Time `json:"last_updated"`
  58. Costs CostsSummaryJSON `json:"costs"`
  59. Combinations []CostCombinationJSON `json:"combinations"`
  60. }
  61. type ModelPricing struct {
  62. InputPrice float64
  63. OutputPrice float64
  64. CacheReadPrice float64
  65. CacheWritePrice float64
  66. }
  67. type modelFamily struct {
  68. pattern *regexp.Regexp
  69. standardPricing ModelPricing
  70. premiumPricing *ModelPricing
  71. }
  72. var (
  73. opus4Pricing = ModelPricing{
  74. InputPrice: 15.0,
  75. OutputPrice: 75.0,
  76. CacheReadPrice: 1.5,
  77. CacheWritePrice: 18.75,
  78. }
  79. sonnet4StandardPricing = ModelPricing{
  80. InputPrice: 3.0,
  81. OutputPrice: 15.0,
  82. CacheReadPrice: 0.3,
  83. CacheWritePrice: 3.75,
  84. }
  85. sonnet4PremiumPricing = ModelPricing{
  86. InputPrice: 6.0,
  87. OutputPrice: 22.5,
  88. CacheReadPrice: 0.6,
  89. CacheWritePrice: 7.5,
  90. }
  91. haiku4Pricing = ModelPricing{
  92. InputPrice: 1.0,
  93. OutputPrice: 5.0,
  94. CacheReadPrice: 0.1,
  95. CacheWritePrice: 1.25,
  96. }
  97. haiku35Pricing = ModelPricing{
  98. InputPrice: 0.8,
  99. OutputPrice: 4.0,
  100. CacheReadPrice: 0.08,
  101. CacheWritePrice: 1.0,
  102. }
  103. sonnet35Pricing = ModelPricing{
  104. InputPrice: 3.0,
  105. OutputPrice: 15.0,
  106. CacheReadPrice: 0.3,
  107. CacheWritePrice: 3.75,
  108. }
  109. modelFamilies = []modelFamily{
  110. {
  111. pattern: regexp.MustCompile(`^claude-(?:opus-4-|4-opus-|opus-4-1-)`),
  112. standardPricing: opus4Pricing,
  113. premiumPricing: nil,
  114. },
  115. {
  116. pattern: regexp.MustCompile(`^claude-3-7-sonnet-`),
  117. standardPricing: sonnet4StandardPricing,
  118. premiumPricing: &sonnet4PremiumPricing,
  119. },
  120. {
  121. pattern: regexp.MustCompile(`^claude-(?:sonnet-4-|4-sonnet-)`),
  122. standardPricing: sonnet4StandardPricing,
  123. premiumPricing: &sonnet4PremiumPricing,
  124. },
  125. {
  126. pattern: regexp.MustCompile(`^claude-haiku-4-`),
  127. standardPricing: haiku4Pricing,
  128. premiumPricing: nil,
  129. },
  130. {
  131. pattern: regexp.MustCompile(`^claude-3-5-haiku-`),
  132. standardPricing: haiku35Pricing,
  133. premiumPricing: nil,
  134. },
  135. {
  136. pattern: regexp.MustCompile(`^claude-3-5-sonnet-`),
  137. standardPricing: sonnet35Pricing,
  138. premiumPricing: nil,
  139. },
  140. }
  141. )
  142. func getPricing(model string, contextWindow int) ModelPricing {
  143. isPremium := contextWindow >= contextWindowPremium
  144. for _, family := range modelFamilies {
  145. if family.pattern.MatchString(model) {
  146. if isPremium && family.premiumPricing != nil {
  147. return *family.premiumPricing
  148. }
  149. return family.standardPricing
  150. }
  151. }
  152. return sonnet4StandardPricing
  153. }
  154. func calculateCost(stats UsageStats, model string, contextWindow int) float64 {
  155. pricing := getPricing(model, contextWindow)
  156. cost := (float64(stats.InputTokens)*pricing.InputPrice +
  157. float64(stats.OutputTokens)*pricing.OutputPrice +
  158. float64(stats.CacheReadInputTokens)*pricing.CacheReadPrice +
  159. float64(stats.CacheCreationInputTokens)*pricing.CacheWritePrice) / 1_000_000
  160. return math.Round(cost*100) / 100
  161. }
  162. func (u *AggregatedUsage) ToJSON() *AggregatedUsageJSON {
  163. u.mutex.Lock()
  164. defer u.mutex.Unlock()
  165. result := &AggregatedUsageJSON{
  166. LastUpdated: u.LastUpdated,
  167. Combinations: make([]CostCombinationJSON, len(u.Combinations)),
  168. Costs: CostsSummaryJSON{
  169. TotalUSD: 0,
  170. ByUser: make(map[string]float64),
  171. },
  172. }
  173. for i, combo := range u.Combinations {
  174. totalCost := calculateCost(combo.Total, combo.Model, combo.ContextWindow)
  175. result.Costs.TotalUSD += totalCost
  176. comboJSON := CostCombinationJSON{
  177. Model: combo.Model,
  178. ContextWindow: combo.ContextWindow,
  179. Total: UsageStatsJSON{
  180. RequestCount: combo.Total.RequestCount,
  181. MessagesCount: combo.Total.MessagesCount,
  182. InputTokens: combo.Total.InputTokens,
  183. OutputTokens: combo.Total.OutputTokens,
  184. CacheReadInputTokens: combo.Total.CacheReadInputTokens,
  185. CacheCreationInputTokens: combo.Total.CacheCreationInputTokens,
  186. CostUSD: totalCost,
  187. },
  188. ByUser: make(map[string]UsageStatsJSON),
  189. }
  190. for user, userStats := range combo.ByUser {
  191. userCost := calculateCost(userStats, combo.Model, combo.ContextWindow)
  192. result.Costs.ByUser[user] += userCost
  193. comboJSON.ByUser[user] = UsageStatsJSON{
  194. RequestCount: userStats.RequestCount,
  195. MessagesCount: userStats.MessagesCount,
  196. InputTokens: userStats.InputTokens,
  197. OutputTokens: userStats.OutputTokens,
  198. CacheReadInputTokens: userStats.CacheReadInputTokens,
  199. CacheCreationInputTokens: userStats.CacheCreationInputTokens,
  200. CostUSD: userCost,
  201. }
  202. }
  203. result.Combinations[i] = comboJSON
  204. }
  205. result.Costs.TotalUSD = math.Round(result.Costs.TotalUSD*100) / 100
  206. for user, cost := range result.Costs.ByUser {
  207. result.Costs.ByUser[user] = math.Round(cost*100) / 100
  208. }
  209. return result
  210. }
  211. func (u *AggregatedUsage) Load() error {
  212. u.mutex.Lock()
  213. defer u.mutex.Unlock()
  214. data, err := os.ReadFile(u.filePath)
  215. if err != nil {
  216. if os.IsNotExist(err) {
  217. return nil
  218. }
  219. return err
  220. }
  221. var temp struct {
  222. LastUpdated time.Time `json:"last_updated"`
  223. Combinations []CostCombination `json:"combinations"`
  224. }
  225. err = json.Unmarshal(data, &temp)
  226. if err != nil {
  227. return err
  228. }
  229. u.LastUpdated = temp.LastUpdated
  230. u.Combinations = temp.Combinations
  231. for i := range u.Combinations {
  232. if u.Combinations[i].ByUser == nil {
  233. u.Combinations[i].ByUser = make(map[string]UsageStats)
  234. }
  235. }
  236. return nil
  237. }
  238. func (u *AggregatedUsage) Save() error {
  239. jsonData := u.ToJSON()
  240. data, err := json.MarshalIndent(jsonData, "", " ")
  241. if err != nil {
  242. return err
  243. }
  244. tmpFile := u.filePath + ".tmp"
  245. err = os.WriteFile(tmpFile, data, 0o644)
  246. if err != nil {
  247. return err
  248. }
  249. defer os.Remove(tmpFile)
  250. err = os.Rename(tmpFile, u.filePath)
  251. if err == nil {
  252. u.saveMutex.Lock()
  253. u.lastSaveTime = time.Now()
  254. u.saveMutex.Unlock()
  255. }
  256. return err
  257. }
  258. func (u *AggregatedUsage) AddUsage(model string, contextWindow int, messagesCount int, inputTokens, outputTokens, cacheReadTokens, cacheCreationTokens int64, user string) error {
  259. if model == "" {
  260. return E.New("model cannot be empty")
  261. }
  262. if contextWindow <= 0 {
  263. return E.New("contextWindow must be positive")
  264. }
  265. u.mutex.Lock()
  266. defer u.mutex.Unlock()
  267. u.LastUpdated = time.Now()
  268. // Find or create combination
  269. var combo *CostCombination
  270. for i := range u.Combinations {
  271. if u.Combinations[i].Model == model && u.Combinations[i].ContextWindow == contextWindow {
  272. combo = &u.Combinations[i]
  273. break
  274. }
  275. }
  276. if combo == nil {
  277. newCombo := CostCombination{
  278. Model: model,
  279. ContextWindow: contextWindow,
  280. Total: UsageStats{},
  281. ByUser: make(map[string]UsageStats),
  282. }
  283. u.Combinations = append(u.Combinations, newCombo)
  284. combo = &u.Combinations[len(u.Combinations)-1]
  285. }
  286. // Update total stats
  287. combo.Total.RequestCount++
  288. combo.Total.MessagesCount += messagesCount
  289. combo.Total.InputTokens += inputTokens
  290. combo.Total.OutputTokens += outputTokens
  291. combo.Total.CacheReadInputTokens += cacheReadTokens
  292. combo.Total.CacheCreationInputTokens += cacheCreationTokens
  293. // Update per-user stats if user is specified
  294. if user != "" {
  295. userStats := combo.ByUser[user]
  296. userStats.RequestCount++
  297. userStats.MessagesCount += messagesCount
  298. userStats.InputTokens += inputTokens
  299. userStats.OutputTokens += outputTokens
  300. userStats.CacheReadInputTokens += cacheReadTokens
  301. userStats.CacheCreationInputTokens += cacheCreationTokens
  302. combo.ByUser[user] = userStats
  303. }
  304. go u.scheduleSave()
  305. return nil
  306. }
  307. func (u *AggregatedUsage) scheduleSave() {
  308. const saveInterval = time.Minute
  309. u.saveMutex.Lock()
  310. defer u.saveMutex.Unlock()
  311. timeSinceLastSave := time.Since(u.lastSaveTime)
  312. if timeSinceLastSave >= saveInterval {
  313. go u.saveAsync()
  314. return
  315. }
  316. if u.pendingSave {
  317. return
  318. }
  319. u.pendingSave = true
  320. remainingTime := saveInterval - timeSinceLastSave
  321. u.saveTimer = time.AfterFunc(remainingTime, func() {
  322. u.saveMutex.Lock()
  323. u.pendingSave = false
  324. u.saveMutex.Unlock()
  325. u.saveAsync()
  326. })
  327. }
  328. func (u *AggregatedUsage) saveAsync() {
  329. err := u.Save()
  330. if err != nil {
  331. if u.logger != nil {
  332. u.logger.Error("save usage statistics: ", err)
  333. }
  334. }
  335. }
  336. func (u *AggregatedUsage) cancelPendingSave() {
  337. u.saveMutex.Lock()
  338. defer u.saveMutex.Unlock()
  339. if u.saveTimer != nil {
  340. u.saveTimer.Stop()
  341. u.saveTimer = nil
  342. }
  343. u.pendingSave = false
  344. }