diff --git a/admin.go b/admin.go index 43c5eb4..688e8a9 100644 --- a/admin.go +++ b/admin.go @@ -1469,7 +1469,11 @@ type adminUsageModelEntryView struct { // mode: how many requests for this model were refused as unpriced // under a caller's cost budget (metricR402) over the requested span. R402 *int64 `json:"r402,omitempty"` - ID string `json:"id"` + // Prompt-cache counters are set in detail mode only, zero included. + PromptCacheReadTokens *int64 `json:"promptCacheReadTokens,omitempty"` + PromptCacheWriteTokens *int64 `json:"promptCacheWriteTokens,omitempty"` + PromptCacheSavedMicroUSD *int64 `json:"promptCacheSavedMicroUsd,omitempty"` + ID string `json:"id"` // PriceSource (admin dashboard redesign, WP-B) is populated only in // detail mode: billingPriceSource's own result (pricing.go) for this // model — the same "override"|"builtin"|"litellm"|"free"|"unpriced" @@ -1480,10 +1484,6 @@ type adminUsageModelEntryView struct { // Free is populated only by serveAdminUsageModelsDetail too; false // (the non-detail zero value) is dropped by its own omitempty. Free bool `json:"free,omitempty"` - // Prompt-cache counters (detail mode only) are always emitted, zero included. - PromptCacheReadTokens int64 `json:"promptCacheReadTokens"` - PromptCacheWriteTokens int64 `json:"promptCacheWriteTokens"` - PromptCacheSavedMicroUSD int64 `json:"promptCacheSavedMicroUsd"` } // adminUsageModelsResponse is GET /admin/api/usage/models' body. Span @@ -1881,6 +1881,7 @@ func (g *Gateway) serveAdminUsageModelsDetail(sw *statusTrackingWriter, ids []st _, bare, _ := strings.Cut(id, "/") source, _ := billingPriceSource(id, bare, g.cfg.Pricing, g.cfg.ModelMeta) r402 := r402ByID[i] + pcRead, pcWrite, pcSave := pcMulti[0][i], pcMulti[1][i], pcMulti[2][i] entry := adminUsageModelEntryView{ ID: id, Value: totalsByMetric[valueIdx][i], @@ -1892,9 +1893,9 @@ func (g *Gateway) serveAdminUsageModelsDetail(sw *statusTrackingWriter, ids []st PriceSource: source, R402: &r402, - PromptCacheReadTokens: pcMulti[0][i], - PromptCacheWriteTokens: pcMulti[1][i], - PromptCacheSavedMicroUSD: pcMulti[2][i], + PromptCacheReadTokens: &pcRead, + PromptCacheWriteTokens: &pcWrite, + PromptCacheSavedMicroUSD: &pcSave, } if chitByID != nil { chit := chitByID[i] diff --git a/prompt_cache_stats_test.go b/prompt_cache_stats_test.go index 798e21d..3beb2e0 100644 --- a/prompt_cache_stats_test.go +++ b/prompt_cache_stats_test.go @@ -114,9 +114,31 @@ func TestAdminUsageModelsPromptCache(t *testing.T) { var got adminUsageModelsResponse require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got)) require.Len(t, got.Models, 1) - assert.EqualValues(t, 900, got.Models[0].PromptCacheReadTokens) - assert.EqualValues(t, 50, got.Models[0].PromptCacheWriteTokens) - assert.EqualValues(t, 2430, got.Models[0].PromptCacheSavedMicroUSD) + require.NotNil(t, got.Models[0].PromptCacheReadTokens) + require.NotNil(t, got.Models[0].PromptCacheWriteTokens) + require.NotNil(t, got.Models[0].PromptCacheSavedMicroUSD) + assert.EqualValues(t, 900, *got.Models[0].PromptCacheReadTokens) + assert.EqualValues(t, 50, *got.Models[0].PromptCacheWriteTokens) + assert.EqualValues(t, 2430, *got.Models[0].PromptCacheSavedMicroUSD) +} + +func TestAdminUsageModelsLegacyShapeOmitsPromptCache(t *testing.T) { + t.Parallel() + h, gw := newAdminGatewayHandle(t, newAdminTestConfig()) + now := time.Date(2026, 8, 20, 12, 30, 0, 0, time.UTC) + gw.limiter.nowFn = func() time.Time { return now } + seedModelCounter(gw, "alpha/a-model-1", metricReq, windowDay, now, 1) + seedModelCounter(gw, "alpha/a-model-1", metricPcRead, windowDay, now, 900) + + for _, q := range []string{"?metric=req&window=day", "?metric=req&window=day&detail=0"} { + rec := httptest.NewRecorder() + h.ServeHTTP(rec, adminRequest(http.MethodGet, adminUsageModelsPath+q, "sk-admin1")) + require.Equal(t, http.StatusOK, rec.Code, rec.Body.String()) + require.Contains(t, rec.Body.String(), "alpha/a-model-1") + for _, f := range []string{"promptCacheReadTokens", "promptCacheWriteTokens", "promptCacheSavedMicroUsd"} { + assert.NotContains(t, rec.Body.String(), f, q) + } + } } func TestCacheHitDoesNotRecordPromptCache(t *testing.T) {