-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathratelimit.go
More file actions
406 lines (343 loc) · 9.46 KB
/
Copy pathratelimit.go
File metadata and controls
406 lines (343 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package tok
import (
"fmt"
"math"
"strings"
"sync"
"time"
)
// UsageEntry represents a single recorded usage event.
type UsageEntry struct {
Tokens int
CostUSD float64
Timestamp time.Time
Provider string
Model string
}
// Alert represents a usage threshold alert.
type Alert struct {
Level string // "warning", "critical", "limit_reached"
Message string
Timestamp time.Time
Threshold float64 // what % triggered it
}
// UsageSummary provides a snapshot of current usage across all windows.
type UsageSummary struct {
HourlyTokens int
HourlyRemaining int
DailyTokens int
DailyRemaining int
SessionTokens int
SessionRemaining int
DailyCostUSD float64
CostRemaining float64
HourlyPct float64
DailyPct float64
}
// UsageTracker tracks API usage across sessions and prevents surprise bills.
type UsageTracker struct {
DailyLimit int
HourlyLimit int
SessionLimit int
CostLimitUSD float64
hourlyUsage []UsageEntry
dailyUsage []UsageEntry
sessionUsage int
mu sync.Mutex
Alerts []Alert
// Track which thresholds have already fired to avoid duplicates.
// Keys are like "hourly_50", "daily_80", "cost_100", etc.
firedThresholds map[string]bool
}
// NewUsageTracker creates a UsageTracker with sensible defaults.
func NewUsageTracker() *UsageTracker {
return &UsageTracker{
DailyLimit: 1_000_000,
HourlyLimit: 200_000,
SessionLimit: 500_000,
CostLimitUSD: 10.00,
hourlyUsage: make([]UsageEntry, 0),
dailyUsage: make([]UsageEntry, 0),
firedThresholds: make(map[string]bool),
}
}
// Record adds a usage entry and checks thresholds.
func (u *UsageTracker) Record(tokens int, costUSD float64, provider, model string) {
u.mu.Lock()
defer u.mu.Unlock()
entry := UsageEntry{
Tokens: tokens,
CostUSD: costUSD,
Timestamp: time.Now(),
Provider: provider,
Model: model,
}
u.hourlyUsage = append(u.hourlyUsage, entry)
u.dailyUsage = append(u.dailyUsage, entry)
u.sessionUsage += tokens
u.checkThresholdsLocked()
}
// CanProceed checks all limits and returns whether another request is allowed.
// If not, it returns false and a reason string.
func (u *UsageTracker) CanProceed() (bool, string) {
u.mu.Lock()
defer u.mu.Unlock()
u.pruneOldLocked()
hourlyTokens := u.hourlyTokensLocked()
if hourlyTokens >= u.HourlyLimit {
return false, fmt.Sprintf("hourly token limit reached (%d/%d)", hourlyTokens, u.HourlyLimit)
}
dailyTokens := u.dailyTokensLocked()
if dailyTokens >= u.DailyLimit {
return false, fmt.Sprintf("daily token limit reached (%d/%d)", dailyTokens, u.DailyLimit)
}
if u.sessionUsage >= u.SessionLimit {
return false, fmt.Sprintf("session token limit reached (%d/%d)", u.sessionUsage, u.SessionLimit)
}
dailyCost := u.dailyCostLocked()
if dailyCost >= u.CostLimitUSD {
return false, fmt.Sprintf("daily cost limit reached ($%.2f/$%.2f)", dailyCost, u.CostLimitUSD)
}
return true, ""
}
// GetUsage returns a snapshot of current usage.
func (u *UsageTracker) GetUsage() UsageSummary {
u.mu.Lock()
defer u.mu.Unlock()
u.pruneOldLocked()
hourlyTokens := u.hourlyTokensLocked()
dailyTokens := u.dailyTokensLocked()
dailyCost := u.dailyCostLocked()
hourlyRemaining := u.HourlyLimit - hourlyTokens
if hourlyRemaining < 0 {
hourlyRemaining = 0
}
dailyRemaining := u.DailyLimit - dailyTokens
if dailyRemaining < 0 {
dailyRemaining = 0
}
sessionRemaining := u.SessionLimit - u.sessionUsage
if sessionRemaining < 0 {
sessionRemaining = 0
}
costRemaining := u.CostLimitUSD - dailyCost
if costRemaining < 0 {
costRemaining = 0
}
var hourlyPct, dailyPct float64
if u.HourlyLimit > 0 {
hourlyPct = float64(hourlyTokens) / float64(u.HourlyLimit) * 100
}
if u.DailyLimit > 0 {
dailyPct = float64(dailyTokens) / float64(u.DailyLimit) * 100
}
return UsageSummary{
HourlyTokens: hourlyTokens,
HourlyRemaining: hourlyRemaining,
DailyTokens: dailyTokens,
DailyRemaining: dailyRemaining,
SessionTokens: u.sessionUsage,
SessionRemaining: sessionRemaining,
DailyCostUSD: dailyCost,
CostRemaining: costRemaining,
HourlyPct: hourlyPct,
DailyPct: dailyPct,
}
}
// CheckThresholds evaluates current usage against threshold levels and generates alerts.
func (u *UsageTracker) CheckThresholds() {
u.mu.Lock()
defer u.mu.Unlock()
u.checkThresholdsLocked()
}
func (u *UsageTracker) checkThresholdsLocked() {
u.pruneOldLocked()
hourlyTokens := u.hourlyTokensLocked()
dailyTokens := u.dailyTokensLocked()
dailyCost := u.dailyCostLocked()
// Hourly thresholds
if u.HourlyLimit > 0 {
pct := float64(hourlyTokens) / float64(u.HourlyLimit) * 100
u.emitAlert("hourly", pct, "hourly token usage")
}
// Daily thresholds
if u.DailyLimit > 0 {
pct := float64(dailyTokens) / float64(u.DailyLimit) * 100
u.emitAlert("daily", pct, "daily token usage")
}
// Session thresholds
if u.SessionLimit > 0 {
pct := float64(u.sessionUsage) / float64(u.SessionLimit) * 100
u.emitAlert("session", pct, "session token usage")
}
// Cost thresholds
if u.CostLimitUSD > 0 {
pct := dailyCost / u.CostLimitUSD * 100
u.emitAlert("cost", pct, "daily cost")
}
}
func (u *UsageTracker) emitAlert(category string, pct float64, label string) {
type threshold struct {
pct float64
level string
}
thresholds := []threshold{
{100, "limit_reached"},
{80, "critical"},
{50, "warning"},
}
for _, t := range thresholds {
if pct >= t.pct {
key := fmt.Sprintf("%s_%d", category, int(t.pct))
if !u.firedThresholds[key] {
u.firedThresholds[key] = true
u.Alerts = append(u.Alerts, Alert{
Level: t.level,
Message: fmt.Sprintf("%s at %.0f%% of limit", label, pct),
Timestamp: time.Now(),
Threshold: t.pct,
})
}
// Only fire the highest applicable threshold
return
}
}
}
// Reset clears the session counter and alerts.
func (u *UsageTracker) Reset() {
u.mu.Lock()
defer u.mu.Unlock()
u.sessionUsage = 0
u.Alerts = nil
u.firedThresholds = make(map[string]bool)
}
// PruneOld removes entries older than their respective windows.
func (u *UsageTracker) PruneOld() {
u.mu.Lock()
defer u.mu.Unlock()
u.pruneOldLocked()
}
func (u *UsageTracker) pruneOldLocked() {
now := time.Now()
hourAgo := now.Add(-1 * time.Hour)
dayAgo := now.Add(-24 * time.Hour)
// Prune hourly
pruned := u.hourlyUsage[:0]
for _, e := range u.hourlyUsage {
if !e.Timestamp.Before(hourAgo) {
pruned = append(pruned, e)
}
}
u.hourlyUsage = pruned
// Prune daily
pruned = u.dailyUsage[:0]
for _, e := range u.dailyUsage {
if !e.Timestamp.Before(dayAgo) {
pruned = append(pruned, e)
}
}
u.dailyUsage = pruned
}
// EstimateRemaining estimates how many more requests of the given size fit in the budget.
func (u *UsageTracker) EstimateRemaining(tokensPerRequest int) int {
u.mu.Lock()
defer u.mu.Unlock()
if tokensPerRequest <= 0 {
return 0
}
u.pruneOldLocked()
hourlyTokens := u.hourlyTokensLocked()
dailyTokens := u.dailyTokensLocked()
hourlyRemaining := u.HourlyLimit - hourlyTokens
dailyRemaining := u.DailyLimit - dailyTokens
sessionRemaining := u.SessionLimit - u.sessionUsage
// Find the minimum remaining across all token-based limits
minRemaining := hourlyRemaining
if dailyRemaining < minRemaining {
minRemaining = dailyRemaining
}
if sessionRemaining < minRemaining {
minRemaining = sessionRemaining
}
if minRemaining <= 0 {
return 0
}
return minRemaining / tokensPerRequest
}
// FormatUsageBar creates an ASCII progress bar.
func FormatUsageBar(pct float64, width int) string {
if width <= 0 {
return ""
}
if pct < 0 {
pct = 0
}
if pct > 100 {
pct = 100
}
filled := int(math.Round(pct / 100 * float64(width)))
if filled > width {
filled = width
}
bar := strings.Repeat("█", filled) + strings.Repeat("░", width-filled)
return fmt.Sprintf("[%s] %d%%", bar, int(pct))
}
// FormatSummary returns a human-readable usage summary.
func (u *UsageTracker) FormatSummary() string {
summary := u.GetUsage()
sessionPct := float64(0)
if u.SessionLimit > 0 {
sessionPct = float64(summary.SessionTokens) / float64(u.SessionLimit) * 100
}
costPct := float64(0)
if u.CostLimitUSD > 0 {
costPct = summary.DailyCostUSD / u.CostLimitUSD * 100
}
barWidth := 16
var sb strings.Builder
sb.WriteString("Token Usage:\n")
fmt.Fprintf(&sb, " Hourly: %s / %s (%d%%) %s\n",
formatNumber(summary.HourlyTokens),
formatNumber(u.HourlyLimit),
int(summary.HourlyPct),
FormatUsageBar(summary.HourlyPct, barWidth))
fmt.Fprintf(&sb, " Daily: %s / %s (%d%%) %s\n",
formatNumber(summary.DailyTokens),
formatNumber(u.DailyLimit),
int(summary.DailyPct),
FormatUsageBar(summary.DailyPct, barWidth))
fmt.Fprintf(&sb, " Session: %s / %s (%d%%) %s\n",
formatNumber(summary.SessionTokens),
formatNumber(u.SessionLimit),
int(sessionPct),
FormatUsageBar(sessionPct, barWidth))
fmt.Fprintf(&sb, " Cost: $%.2f / $%.2f (%d%%) %s",
summary.DailyCostUSD,
u.CostLimitUSD,
int(costPct),
FormatUsageBar(costPct, barWidth))
return sb.String()
}
// --- internal helpers ---
func (u *UsageTracker) hourlyTokensLocked() int {
total := 0
for _, e := range u.hourlyUsage {
total += e.Tokens
}
return total
}
func (u *UsageTracker) dailyTokensLocked() int {
total := 0
for _, e := range u.dailyUsage {
total += e.Tokens
}
return total
}
func (u *UsageTracker) dailyCostLocked() float64 {
total := 0.0
for _, e := range u.dailyUsage {
total += e.CostUSD
}
return total
}
// formatNumber is defined in format.go and shared across the package.