Skip to content

Commit 68e7658

Browse files
committed
fix: recognize claude cache creation input usage
1 parent 349c7f6 commit 68e7658

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

backend/internal/plugin/usage_adapter.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ func usageSnapshotFromSDK(usage *sdk.Usage) usageSnapshot {
5252
snap.OutputTokens += int(metric.Value)
5353
case "cached_input_tokens", "cached_input_token", "cache_read_tokens", "cache_read_token":
5454
snap.CachedInputTokens += int(metric.Value)
55-
case "cache_creation_tokens", "cache_creation_token":
55+
case "cache_creation_tokens", "cache_creation_token",
56+
"cache_creation_input_tokens", "cache_creation_input_token":
5657
snap.CacheCreationTokens += int(metric.Value)
57-
case "cache_creation_5m_tokens", "cache_creation_5m_token":
58+
case "cache_creation_5m_tokens", "cache_creation_5m_token",
59+
"cache_creation_5m_input_tokens", "cache_creation_5m_input_token":
5860
snap.CacheCreation5mTokens += int(metric.Value)
59-
case "cache_creation_1h_tokens", "cache_creation_1h_token":
61+
case "cache_creation_1h_tokens", "cache_creation_1h_token",
62+
"cache_creation_1h_input_tokens", "cache_creation_1h_input_token":
6063
snap.CacheCreation1hTokens += int(metric.Value)
6164
case "reasoning_output_tokens", "reasoning_tokens", "reasoning_token":
6265
snap.ReasoningOutputTokens += int(metric.Value)
@@ -147,8 +150,11 @@ func applyUsageCost(snap *usageSnapshot, key string, cost float64, metadata map[
147150
case "cached_input", "cached_input_tokens", "cached_input_token", "cache_read_tokens", "cache_read_token":
148151
snap.CachedInputCost += cost
149152
case "cache_creation", "cache_creation_tokens", "cache_creation_token",
153+
"cache_creation_input_tokens", "cache_creation_input_token",
150154
"cache_creation_5m", "cache_creation_5m_tokens", "cache_creation_5m_token",
151-
"cache_creation_1h", "cache_creation_1h_tokens", "cache_creation_1h_token":
155+
"cache_creation_5m_input_tokens", "cache_creation_5m_input_token",
156+
"cache_creation_1h", "cache_creation_1h_tokens", "cache_creation_1h_token",
157+
"cache_creation_1h_input_tokens", "cache_creation_1h_input_token":
152158
snap.CacheCreationCost += cost
153159
}
154160
}
@@ -170,9 +176,12 @@ func applyUsagePrice(snap *usageSnapshot, key string, metadata map[string]string
170176
case "cached_input", "cached_input_tokens", "cached_input_token", "cache_read_tokens", "cache_read_token":
171177
snap.CachedInputPrice = price
172178
case "cache_creation", "cache_creation_tokens", "cache_creation_token",
173-
"cache_creation_5m", "cache_creation_5m_tokens", "cache_creation_5m_token":
179+
"cache_creation_input_tokens", "cache_creation_input_token",
180+
"cache_creation_5m", "cache_creation_5m_tokens", "cache_creation_5m_token",
181+
"cache_creation_5m_input_tokens", "cache_creation_5m_input_token":
174182
snap.CacheCreationPrice = price
175-
case "cache_creation_1h", "cache_creation_1h_tokens", "cache_creation_1h_token":
183+
case "cache_creation_1h", "cache_creation_1h_tokens", "cache_creation_1h_token",
184+
"cache_creation_1h_input_tokens", "cache_creation_1h_input_token":
176185
snap.CacheCreation1hPrice = price
177186
}
178187
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package plugin
2+
3+
import (
4+
"math"
5+
"testing"
6+
7+
sdk "github.com/DouDOU-start/airgate-sdk/sdkgo"
8+
)
9+
10+
func TestUsageSnapshotFromSDK_RecognizesClaudeCacheCreationKeys(t *testing.T) {
11+
usage := &sdk.Usage{
12+
Metrics: []sdk.UsageMetric{
13+
{Key: "input_tokens", Value: 2},
14+
{Key: "output_tokens", Value: 163},
15+
{Key: "cached_input_tokens", Value: 141779},
16+
{Key: "cache_creation_input_tokens", Value: 1756},
17+
{Key: "cache_creation_5m_input_tokens", Value: 1756},
18+
{Key: "cache_creation_1h_input_tokens", Value: 0},
19+
},
20+
CostDetails: []sdk.UsageCostDetail{
21+
{Key: "input_tokens", AccountCost: 0.00001, Metadata: map[string]string{"unit_price": "5"}},
22+
{Key: "cached_input_tokens", AccountCost: 0.0708895, Metadata: map[string]string{"unit_price": "0.5"}},
23+
{Key: "cache_creation_5m_input_tokens", AccountCost: 0.010975, Metadata: map[string]string{"unit_price": "6.25"}},
24+
{Key: "output_tokens", AccountCost: 0.004075, Metadata: map[string]string{"unit_price": "25"}},
25+
},
26+
}
27+
28+
snap := usageSnapshotFromSDK(usage)
29+
30+
if snap.CacheCreationTokens != 1756 {
31+
t.Fatalf("CacheCreationTokens = %d, want 1756", snap.CacheCreationTokens)
32+
}
33+
if snap.CacheCreation5mTokens != 1756 {
34+
t.Fatalf("CacheCreation5mTokens = %d, want 1756", snap.CacheCreation5mTokens)
35+
}
36+
if snap.CacheCreation1hTokens != 0 {
37+
t.Fatalf("CacheCreation1hTokens = %d, want 0", snap.CacheCreation1hTokens)
38+
}
39+
if math.Abs(snap.CacheCreationCost-0.010975) > 1e-12 {
40+
t.Fatalf("CacheCreationCost = %.12f, want %.12f", snap.CacheCreationCost, 0.010975)
41+
}
42+
if math.Abs(snap.CacheCreationPrice-6.25) > 1e-12 {
43+
t.Fatalf("CacheCreationPrice = %.12f, want 6.25", snap.CacheCreationPrice)
44+
}
45+
}

0 commit comments

Comments
 (0)