Skip to content

Commit c7f61dd

Browse files
committed
fix(ai-gateway): stop double-charging cache tokens in spend
domain.Usage.PromptTokens is cache-inclusive, but usageFromDomain mapped it straight onto the spend wire's input_tokens while also sending the cache read and cache creation buckets separately, so the rating side charged cached tokens at both the input rate and the cache rate. Subtract the cache buckets from input_tokens, matching the span emitter's exclusive split. Fixes langwatch#7018.
1 parent d826d5a commit c7f61dd

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

services/aigateway/adapters/spendemitter/contract_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestContractConfirmedPayload(t *testing.T) {
110110
require.NoError(t, json.Unmarshal(records[0].Payload, &payload))
111111
usage, ok := payload["usage"].(map[string]any)
112112
require.True(t, ok)
113-
assert.EqualValues(t, 869, usage["input_tokens"])
113+
assert.EqualValues(t, 853, usage["input_tokens"])
114114
assert.EqualValues(t, 207, usage["output_tokens"])
115115
assert.EqualValues(t, 11, usage["cache_read_input_tokens"])
116116
assert.EqualValues(t, 5, usage["cache_creation_input_tokens"])

services/aigateway/adapters/spendemitter/record.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ type ErrorPayload struct {
108108
// reasoning-token count today.
109109
func usageFromDomain(u domain.Usage, reasoning int) UsagePayload {
110110
return UsagePayload{
111-
InputTokens: u.PromptTokens,
111+
InputTokens: u.PromptTokens - u.CacheReadTokens - u.CacheCreationTokens,
112112
OutputTokens: u.CompletionTokens,
113113
CacheReadTokens: u.CacheReadTokens,
114114
CacheCreationTokens: u.CacheCreationTokens,

0 commit comments

Comments
 (0)