mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-19 08:19:22 +08:00
fix(devin): count cache-write tokens in total input
Devin reports the prompt in three disjoint parts: uncached (field 2), cache write (field 4) and cache read (field 5). The executor summed only uncached + cache read, so a cache miss on the GPT route (almost all cache write) reported ~3 input tokens for a full prompt, and the Claude route lost the written part on every first request. CPA's own token accounting expects the total to include writes and marked those responses inconsistent; the Claude-format translator already subtracts read + write from the total. Live: GPT-6.1 Sol miss was 3 uncached + 6103 written, reported as 3 input; the next request read 6103 of 6106. Updates the #5910 regression test.
This commit is contained in:
@@ -1231,7 +1231,7 @@ func (e *DevinExecutor) streamDevinFramesForAuth(
|
||||
_ = completedDoc.Set("interaction.finish_reason", completionFinishReason)
|
||||
}
|
||||
if finalUsage != nil {
|
||||
totalInput := finalUsage.PromptTokens + finalUsage.CachedTokens
|
||||
totalInput := devinTotalInputTokens(finalUsage)
|
||||
totalOutput := finalUsage.CompletionTokens
|
||||
totalTokens := totalInput + totalOutput
|
||||
|
||||
@@ -1662,7 +1662,7 @@ func consumeDevinFramesToInteractionsForAuth(body io.Reader, model, chatModelUID
|
||||
}
|
||||
|
||||
if finalUsage != nil {
|
||||
totalInput := finalUsage.PromptTokens + finalUsage.CachedTokens
|
||||
totalInput := devinTotalInputTokens(finalUsage)
|
||||
totalOutput := finalUsage.CompletionTokens
|
||||
totalTokens := totalInput + totalOutput
|
||||
|
||||
@@ -2875,6 +2875,17 @@ func resolveDevinSessionAndCascadeIDs(ctx context.Context, sessionID, cascadeID
|
||||
return sessionID, cascadeID
|
||||
}
|
||||
|
||||
// devinTotalInputTokens returns the full prompt size. Devin reports the prompt in
|
||||
// three disjoint parts: uncached (field 2), cache write (field 4) and cache read
|
||||
// (field 5). A cache miss on the GPT route is almost all cache write, so leaving
|
||||
// it out reported a few tokens for a full prompt.
|
||||
func devinTotalInputTokens(u *helps.DevinUsage) int64 {
|
||||
if u == nil {
|
||||
return 0
|
||||
}
|
||||
return u.PromptTokens + u.CachedTokens + u.CacheWriteTokens
|
||||
}
|
||||
|
||||
func normalizeDevinUUID(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
|
||||
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/usage"
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
||||
log "github.com/sirupsen/logrus"
|
||||
logtest "github.com/sirupsen/logrus/hooks/test"
|
||||
@@ -3196,8 +3197,9 @@ func TestRegressionIssue5910_UsageStatsCacheWriteTokensInResponses(t *testing.T)
|
||||
}
|
||||
|
||||
root := ojson.ParseBytes(interactionsJSON)
|
||||
if root.Get("usage.total_input_tokens").Int() != 53 {
|
||||
t.Errorf("usage.total_input_tokens = %d, want 53", root.Get("usage.total_input_tokens").Int())
|
||||
// total input = uncached 3 + cache read 50 + cache write 14361
|
||||
if root.Get("usage.total_input_tokens").Int() != 14414 {
|
||||
t.Errorf("usage.total_input_tokens = %d, want 14414", root.Get("usage.total_input_tokens").Int())
|
||||
}
|
||||
if root.Get("usage.cache_write_tokens").Int() != 14361 {
|
||||
t.Errorf("usage.cache_write_tokens = %d, want 14361", root.Get("usage.cache_write_tokens").Int())
|
||||
@@ -3206,8 +3208,11 @@ func TestRegressionIssue5910_UsageStatsCacheWriteTokensInResponses(t *testing.T)
|
||||
if detail.CacheCreationTokens != 14361 {
|
||||
t.Errorf("ParseInteractionsUsage CacheCreationTokens = %d, want 14361", detail.CacheCreationTokens)
|
||||
}
|
||||
if detail.InputTokens != 53 {
|
||||
t.Errorf("ParseInteractionsUsage InputTokens = %d, want 53", detail.InputTokens)
|
||||
if detail.InputTokens != 14414 {
|
||||
t.Errorf("ParseInteractionsUsage InputTokens = %d, want 14414", detail.InputTokens)
|
||||
}
|
||||
if detail.TokenBreakdown.Quality != usage.TokenAccountingQualityComplete || detail.TokenBreakdown.Input.UncachedTokens != 3 {
|
||||
t.Errorf("token breakdown = %+v, want complete with 3 uncached", detail.TokenBreakdown)
|
||||
}
|
||||
|
||||
// 2. Streaming test
|
||||
@@ -3265,8 +3270,11 @@ func TestRegressionIssue5910_UsageStatsCacheWriteTokensInResponses(t *testing.T)
|
||||
if sDetail.CacheCreationTokens != 14361 {
|
||||
t.Errorf("ParseInteractionsStreamUsage CacheCreationTokens = %d, want 14361", sDetail.CacheCreationTokens)
|
||||
}
|
||||
if sDetail.InputTokens != 53 {
|
||||
t.Errorf("ParseInteractionsStreamUsage InputTokens = %d, want 53", sDetail.InputTokens)
|
||||
if sDetail.InputTokens != 14414 {
|
||||
t.Errorf("ParseInteractionsStreamUsage InputTokens = %d, want 14414", sDetail.InputTokens)
|
||||
}
|
||||
if got := cRoot.Get("interaction.usage.total_input_tokens").Int(); got != 14414 {
|
||||
t.Errorf("interaction.usage.total_input_tokens = %d, want 14414", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user