mirror of
https://github.com/lukaszraczylo/traefik-llmgateway.git
synced 2026-11-26 10:06:31 +00:00
fix(admin): read prompt-cache counters for returned rows only
This commit is contained in:
@@ -1658,7 +1658,7 @@ func (g *Gateway) serveAdminUsageModels(sw *statusTrackingWriter, r *http.Reques
|
||||
// one through.
|
||||
keyMultiplier := 1
|
||||
if detail {
|
||||
keyMultiplier = len(usageModelsDetailMetrics()) + 1 + 3 // +1: r402, +3: pcr/pcw/pcsave, always read
|
||||
keyMultiplier = len(usageModelsDetailMetrics()) + 1 // +1: r402
|
||||
if g.buildAdminFeatures().CacheStats {
|
||||
keyMultiplier += 2 // chit, csave
|
||||
}
|
||||
@@ -1834,12 +1834,6 @@ func (g *Gateway) serveAdminUsageModelsDetail(sw *statusTrackingWriter, ids []st
|
||||
}
|
||||
r402ByID := r402Multi[0]
|
||||
|
||||
pcMulti, storeOK := g.chunkedModelSpanTotalsMulti(ids, []string{metricPcRead, metricPcWrite, metricPcSave}, window, now, span, offset)
|
||||
if !storeOK {
|
||||
writeOAIError(sw, http.StatusServiceUnavailable, "server_error", "usage history store unavailable")
|
||||
return
|
||||
}
|
||||
|
||||
// chit/csave (admin dashboard redesign, WP-B): only when
|
||||
// features.cacheStats (the response cache is configured) — nil
|
||||
// otherwise, as before. Unlike r402, a model's chit/csave are ONLY
|
||||
@@ -1881,7 +1875,6 @@ 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,10 +1885,6 @@ func (g *Gateway) serveAdminUsageModelsDetail(sw *statusTrackingWriter, ids []st
|
||||
Free: adminUsageModelFree(id, bare, g.cfg.Pricing, g.cfg.ModelMeta),
|
||||
PriceSource: source,
|
||||
R402: &r402,
|
||||
|
||||
PromptCacheReadTokens: &pcRead,
|
||||
PromptCacheWriteTokens: &pcWrite,
|
||||
PromptCacheSavedMicroUSD: &pcSave,
|
||||
}
|
||||
if chitByID != nil {
|
||||
chit := chitByID[i]
|
||||
@@ -1918,6 +1907,24 @@ func (g *Gateway) serveAdminUsageModelsDetail(sw *statusTrackingWriter, ids []st
|
||||
models = models[:limit]
|
||||
}
|
||||
|
||||
// Own read over the returned page only: the catalog-wide read would
|
||||
// push the key count past adminMaxKeysPerRequest.
|
||||
pageIDs := make([]string, len(models))
|
||||
for i := range models {
|
||||
pageIDs[i] = models[i].ID
|
||||
}
|
||||
pcMulti, storeOK := g.chunkedModelSpanTotalsMulti(pageIDs, []string{metricPcRead, metricPcWrite, metricPcSave}, window, now, span, offset)
|
||||
if !storeOK {
|
||||
writeOAIError(sw, http.StatusServiceUnavailable, "server_error", "usage history store unavailable")
|
||||
return
|
||||
}
|
||||
for i := range models {
|
||||
pcRead, pcWrite, pcSave := pcMulti[0][i], pcMulti[1][i], pcMulti[2][i]
|
||||
models[i].PromptCacheReadTokens = &pcRead
|
||||
models[i].PromptCacheWriteTokens = &pcWrite
|
||||
models[i].PromptCacheSavedMicroUSD = &pcSave
|
||||
}
|
||||
|
||||
setAdminJSONHeaders(sw)
|
||||
_ = json.NewEncoder(sw).Encode(adminUsageModelsResponse{Metric: metric, Window: window, Span: span, Offset: offset, Models: models, Detail: true})
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package traefikllmgateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -159,3 +160,31 @@ func TestCacheHitDoesNotRecordPromptCache(t *testing.T) {
|
||||
pcr, _ := gw.limiter.getCounter("user", "alice", metricPcRead, windowDay, time.Now())
|
||||
assert.EqualValues(t, 8, pcr, "only the upstream response counts, not the replay")
|
||||
}
|
||||
|
||||
func TestAdminUsageModelsDetailBudgetUnchanged(t *testing.T) {
|
||||
t.Parallel()
|
||||
const span = 48
|
||||
const preBranchMultiplier = 5 // req, tokin, tokout, cost, r402
|
||||
n := adminMaxKeysPerRequest/(span*preBranchMultiplier) - 2
|
||||
require.Greater(t, n*span*(preBranchMultiplier+3), adminMaxKeysPerRequest, "catalog must exceed the budget at 8x")
|
||||
cfg := newAdminTestConfig()
|
||||
models := make([]string, n)
|
||||
for i := range models {
|
||||
models[i] = fmt.Sprintf("m%03d", i)
|
||||
}
|
||||
cfg.Providers["alpha"].Models = models
|
||||
h, gw := newAdminGatewayHandle(t, cfg)
|
||||
now := time.Date(2026, 8, 20, 12, 30, 0, 0, time.UTC)
|
||||
gw.limiter.nowFn = func() time.Time { return now }
|
||||
seedModelCounter(gw, "alpha/m001", metricReq, windowHour, now, 1)
|
||||
seedModelCounter(gw, "alpha/m001", metricPcRead, windowHour, now, 7)
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, adminRequest(http.MethodGet, adminUsageModelsPath+"?metric=req&window=hour&span=48&detail=1", "sk-admin1"))
|
||||
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
|
||||
var got adminUsageModelsResponse
|
||||
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
|
||||
require.Len(t, got.Models, 1)
|
||||
require.NotNil(t, got.Models[0].PromptCacheReadTokens)
|
||||
assert.EqualValues(t, 7, *got.Models[0].PromptCacheReadTokens)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user