-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbilling_test.go
More file actions
273 lines (225 loc) · 9.28 KB
/
Copy pathbilling_test.go
File metadata and controls
273 lines (225 loc) · 9.28 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
package llmproxy
import (
"math"
"testing"
)
const epsilon = 1e-9
func assertFloat(t *testing.T, name string, got, want float64) {
t.Helper()
if math.Abs(got-want) > epsilon {
t.Errorf("%s = %f, want %f (diff: %e)", name, got, want, math.Abs(got-want))
}
}
func TestCalculateCost_NoCacheUsage(t *testing.T) {
costInfo := CostInfo{Input: 3.0, Output: 15.0, CacheRead: 1.5}
result := CalculateCost("openai", "gpt-4o", costInfo, 1000, 500, nil)
if result.Provider != "openai" {
t.Errorf("Provider = %q, want %q", result.Provider, "openai")
}
if result.Model != "gpt-4o" {
t.Errorf("Model = %q, want %q", result.Model, "gpt-4o")
}
if result.PromptTokens != 1000 {
t.Errorf("PromptTokens = %d, want 1000", result.PromptTokens)
}
if result.CompletionTokens != 500 {
t.Errorf("CompletionTokens = %d, want 500", result.CompletionTokens)
}
if result.CachedTokens != 0 {
t.Errorf("CachedTokens = %d, want 0", result.CachedTokens)
}
if result.TotalTokens != 1500 {
t.Errorf("TotalTokens = %d, want 1500", result.TotalTokens)
}
expectedInput := 3.0 * 1000 / 1_000_000
expectedOutput := 15.0 * 500 / 1_000_000
assertFloat(t, "InputCost", result.InputCost, expectedInput)
assertFloat(t, "CachedInputCost", result.CachedInputCost, 0)
assertFloat(t, "OutputCost", result.OutputCost, expectedOutput)
assertFloat(t, "TotalCost", result.TotalCost, expectedInput+expectedOutput)
}
func TestCalculateCost_WithOpenAICacheHit(t *testing.T) {
costInfo := CostInfo{Input: 3.0, Output: 15.0, CacheRead: 1.5}
cacheUsage := &CacheUsage{CachedTokens: 800}
result := CalculateCost("openai", "gpt-4o", costInfo, 1000, 500, cacheUsage)
if result.CachedTokens != 800 {
t.Errorf("CachedTokens = %d, want 800", result.CachedTokens)
}
// 200 non-cached at full rate, 800 cached at cache rate
expectedInput := 3.0 * 200 / 1_000_000
expectedCached := 1.5 * 800 / 1_000_000
expectedOutput := 15.0 * 500 / 1_000_000
assertFloat(t, "InputCost", result.InputCost, expectedInput)
assertFloat(t, "CachedInputCost", result.CachedInputCost, expectedCached)
assertFloat(t, "OutputCost", result.OutputCost, expectedOutput)
assertFloat(t, "TotalCost", result.TotalCost, expectedInput+expectedCached+expectedOutput)
}
func TestCalculateCost_WithAnthropicCacheHit(t *testing.T) {
// Anthropic reports input_tokens as non-cached only (excludes cached).
// Real example: input_tokens=3, cache_read_input_tokens=2022
costInfo := CostInfo{Input: 3.0, Output: 15.0, CacheRead: 0.3}
cacheUsage := &CacheUsage{CacheReadInputTokens: 2022}
// promptTokens=3 (Anthropic's input_tokens, non-cached only)
result := CalculateCost("anthropic", "claude-sonnet-4", costInfo, 3, 10, cacheUsage)
if result.CachedTokens != 2022 {
t.Errorf("CachedTokens = %d, want 2022", result.CachedTokens)
}
// PromptTokens should be adjusted to include cached: 3 + 2022 = 2025
if result.PromptTokens != 2025 {
t.Errorf("PromptTokens = %d, want 2025 (non-cached + cached)", result.PromptTokens)
}
// 3 non-cached at full rate, 2022 cached at cache rate
expectedInput := 3.0 * 3 / 1_000_000
expectedCached := 0.3 * 2022 / 1_000_000
expectedOutput := 15.0 * 10 / 1_000_000
assertFloat(t, "InputCost", result.InputCost, expectedInput)
assertFloat(t, "CachedInputCost", result.CachedInputCost, expectedCached)
assertFloat(t, "TotalCost", result.TotalCost, expectedInput+expectedCached+expectedOutput)
}
func TestCalculateCost_CacheUsageWithZeroTokens(t *testing.T) {
costInfo := CostInfo{Input: 3.0, Output: 15.0, CacheRead: 1.5}
// CacheUsage present but all fields are zero
cacheUsage := &CacheUsage{}
result := CalculateCost("openai", "gpt-4o", costInfo, 1000, 500, cacheUsage)
// Should behave exactly like no cache usage
if result.CachedTokens != 0 {
t.Errorf("CachedTokens = %d, want 0", result.CachedTokens)
}
assertFloat(t, "CachedInputCost", result.CachedInputCost, 0)
expectedInput := 3.0 * 1000 / 1_000_000
expectedOutput := 15.0 * 500 / 1_000_000
assertFloat(t, "InputCost", result.InputCost, expectedInput)
assertFloat(t, "TotalCost", result.TotalCost, expectedInput+expectedOutput)
}
func TestCalculateCost_CacheExceedsPromptTokens_AnthropicStyle(t *testing.T) {
// Anthropic style: prompt_tokens=10 (non-cached), cache_read=5000 (cached).
// cached > prompt means the provider reports non-cached only.
costInfo := CostInfo{Input: 3.0, Output: 15.0, CacheRead: 1.5}
cacheUsage := &CacheUsage{CacheReadInputTokens: 5000}
result := CalculateCost("anthropic", "claude-sonnet-4", costInfo, 10, 500, cacheUsage)
if result.CachedTokens != 5000 {
t.Errorf("CachedTokens = %d, want 5000", result.CachedTokens)
}
// PromptTokens adjusted to true total: 10 + 5000
if result.PromptTokens != 5010 {
t.Errorf("PromptTokens = %d, want 5010", result.PromptTokens)
}
// 10 non-cached at full rate, 5000 cached at cache rate
expectedInput := 3.0 * 10 / 1_000_000
expectedCached := 1.5 * 5000 / 1_000_000
assertFloat(t, "InputCost", result.InputCost, expectedInput)
assertFloat(t, "CachedInputCost", result.CachedInputCost, expectedCached)
}
func TestCalculateCost_NoCacheReadPrice(t *testing.T) {
// Provider doesn't have cache pricing — should fall back to full input rate
costInfo := CostInfo{Input: 3.0, Output: 15.0}
cacheUsage := &CacheUsage{CachedTokens: 800}
result := CalculateCost("groq", "llama-3.3-70b", costInfo, 1000, 500, cacheUsage)
if result.CachedTokens != 800 {
t.Errorf("CachedTokens = %d, want 800", result.CachedTokens)
}
// Cached tokens should fall back to full input rate
expectedInput := 3.0 * 200 / 1_000_000
expectedCached := 3.0 * 800 / 1_000_000 // same as input rate
expectedOutput := 15.0 * 500 / 1_000_000
assertFloat(t, "InputCost", result.InputCost, expectedInput)
assertFloat(t, "CachedInputCost", result.CachedInputCost, expectedCached)
assertFloat(t, "TotalCost", result.TotalCost, expectedInput+expectedCached+expectedOutput)
}
func TestCalculateCost_AllTokensCached(t *testing.T) {
costInfo := CostInfo{Input: 3.0, Output: 15.0, CacheRead: 1.5}
cacheUsage := &CacheUsage{CachedTokens: 1000}
result := CalculateCost("openai", "gpt-4o", costInfo, 1000, 500, cacheUsage)
if result.CachedTokens != 1000 {
t.Errorf("CachedTokens = %d, want 1000", result.CachedTokens)
}
// All prompt tokens cached — zero non-cached input cost
assertFloat(t, "InputCost", result.InputCost, 0)
expectedCached := 1.5 * 1000 / 1_000_000
assertFloat(t, "CachedInputCost", result.CachedInputCost, expectedCached)
}
func TestCalculateCost_ZeroTokens(t *testing.T) {
costInfo := CostInfo{Input: 3.0, Output: 15.0, CacheRead: 1.5}
result := CalculateCost("openai", "gpt-4o", costInfo, 0, 0, nil)
assertFloat(t, "InputCost", result.InputCost, 0)
assertFloat(t, "CachedInputCost", result.CachedInputCost, 0)
assertFloat(t, "OutputCost", result.OutputCost, 0)
assertFloat(t, "TotalCost", result.TotalCost, 0)
}
func TestCalculateCost_PerMillionCharacters(t *testing.T) {
result := CalculateCostWithMeteredUsage(
"openai",
"tts-1",
CostInfo{Input: 15, Unit: "per_million_characters"},
0,
0,
nil,
MeteredUsage{InputCharacters: 10},
)
assertFloat(t, "InputCost", result.InputCost, 0.00015)
assertFloat(t, "TotalCost", result.TotalCost, 0.00015)
if result.InputQuantity != 10 {
t.Fatalf("InputQuantity = %f, want 10", result.InputQuantity)
}
}
func TestCalculateCost_PerMinuteAudio(t *testing.T) {
result := CalculateCostWithMeteredUsage(
"openai",
"whisper-1",
CostInfo{Input: 0.006, Unit: "per_minute_audio"},
0,
0,
nil,
MeteredUsage{InputAudioSeconds: 30},
)
assertFloat(t, "InputCost", result.InputCost, 0.003)
assertFloat(t, "TotalCost", result.TotalCost, 0.003)
if result.InputQuantity != 0.5 {
t.Fatalf("InputQuantity = %f, want 0.5", result.InputQuantity)
}
}
func TestCalculateCost_PerSecondVideo(t *testing.T) {
result := CalculateCostWithMeteredUsage(
"googleai",
"veo-3.1-generate-preview",
CostInfo{Output: 0.4, Unit: "per_second_720p_1080p_video"},
0,
0,
nil,
MeteredUsage{OutputVideoSeconds: 16},
)
assertFloat(t, "OutputCost", result.OutputCost, 6.4)
assertFloat(t, "TotalCost", result.TotalCost, 6.4)
if result.OutputQuantity != 16 {
t.Fatalf("OutputQuantity = %f, want 16", result.OutputQuantity)
}
}
func TestMergeMeteredUsage_ResponseZeroOverridesRequestEstimate(t *testing.T) {
merged := mergeMeteredUsage(
MeteredUsage{InputAudioSeconds: 30, HasInputAudioSeconds: true},
MeteredUsage{InputAudioSeconds: 0, HasInputAudioSeconds: true},
)
if merged.InputAudioSeconds != 0 {
t.Fatalf("InputAudioSeconds = %f, want response-reported zero", merged.InputAudioSeconds)
}
if !merged.HasInputAudioSeconds {
t.Fatal("HasInputAudioSeconds = false, want true")
}
}
func TestCalculateCost_MixedProviderCacheFields(t *testing.T) {
// Both CachedTokens and CacheReadInputTokens set (shouldn't happen, but test summing)
costInfo := CostInfo{Input: 3.0, Output: 15.0, CacheRead: 1.5}
cacheUsage := &CacheUsage{
CachedTokens: 300,
CacheReadInputTokens: 200,
}
result := CalculateCost("test", "model", costInfo, 1000, 100, cacheUsage)
// Should sum both fields: 300 + 200 = 500
if result.CachedTokens != 500 {
t.Errorf("CachedTokens = %d, want 500", result.CachedTokens)
}
expectedInput := 3.0 * 500 / 1_000_000
expectedCached := 1.5 * 500 / 1_000_000
assertFloat(t, "InputCost", result.InputCost, expectedInput)
assertFloat(t, "CachedInputCost", result.CachedInputCost, expectedCached)
}