fix(admin): keep the legacy usage models shape for prompt-cache fields

The three prompt-cache fields are now optional pointers. Only the detail
view sets them, so the default response stays the same as before.
This commit is contained in:
2026-11-26 01:08:48 +00:00
parent d661166eab
commit fd8ca1e95a
2 changed files with 34 additions and 11 deletions
+9 -8
View File
@@ -1469,7 +1469,11 @@ type adminUsageModelEntryView struct {
// mode: how many requests for this model were refused as unpriced // mode: how many requests for this model were refused as unpriced
// under a caller's cost budget (metricR402) over the requested span. // under a caller's cost budget (metricR402) over the requested span.
R402 *int64 `json:"r402,omitempty"` 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 // PriceSource (admin dashboard redesign, WP-B) is populated only in
// detail mode: billingPriceSource's own result (pricing.go) for this // detail mode: billingPriceSource's own result (pricing.go) for this
// model — the same "override"|"builtin"|"litellm"|"free"|"unpriced" // model — the same "override"|"builtin"|"litellm"|"free"|"unpriced"
@@ -1480,10 +1484,6 @@ type adminUsageModelEntryView struct {
// Free is populated only by serveAdminUsageModelsDetail too; false // Free is populated only by serveAdminUsageModelsDetail too; false
// (the non-detail zero value) is dropped by its own omitempty. // (the non-detail zero value) is dropped by its own omitempty.
Free bool `json:"free,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 // 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, "/") _, bare, _ := strings.Cut(id, "/")
source, _ := billingPriceSource(id, bare, g.cfg.Pricing, g.cfg.ModelMeta) source, _ := billingPriceSource(id, bare, g.cfg.Pricing, g.cfg.ModelMeta)
r402 := r402ByID[i] r402 := r402ByID[i]
pcRead, pcWrite, pcSave := pcMulti[0][i], pcMulti[1][i], pcMulti[2][i]
entry := adminUsageModelEntryView{ entry := adminUsageModelEntryView{
ID: id, ID: id,
Value: totalsByMetric[valueIdx][i], Value: totalsByMetric[valueIdx][i],
@@ -1892,9 +1893,9 @@ func (g *Gateway) serveAdminUsageModelsDetail(sw *statusTrackingWriter, ids []st
PriceSource: source, PriceSource: source,
R402: &r402, R402: &r402,
PromptCacheReadTokens: pcMulti[0][i], PromptCacheReadTokens: &pcRead,
PromptCacheWriteTokens: pcMulti[1][i], PromptCacheWriteTokens: &pcWrite,
PromptCacheSavedMicroUSD: pcMulti[2][i], PromptCacheSavedMicroUSD: &pcSave,
} }
if chitByID != nil { if chitByID != nil {
chit := chitByID[i] chit := chitByID[i]
+25 -3
View File
@@ -114,9 +114,31 @@ func TestAdminUsageModelsPromptCache(t *testing.T) {
var got adminUsageModelsResponse var got adminUsageModelsResponse
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got)) require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
require.Len(t, got.Models, 1) require.Len(t, got.Models, 1)
assert.EqualValues(t, 900, got.Models[0].PromptCacheReadTokens) require.NotNil(t, got.Models[0].PromptCacheReadTokens)
assert.EqualValues(t, 50, got.Models[0].PromptCacheWriteTokens) require.NotNil(t, got.Models[0].PromptCacheWriteTokens)
assert.EqualValues(t, 2430, got.Models[0].PromptCacheSavedMicroUSD) 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) { func TestCacheHitDoesNotRecordPromptCache(t *testing.T) {