On X: "Claude code 通过 Magpie 链接 DeepSeek 缓存命中率只有 50% 怎么解决呀".
Measured with a real DeepSeek key, Claude Code 2.1.314 in a sandbox
HOME through a sandbox magpie (usage cache_read / in per request):
- one interactive conversation on one key, Anthropic path: 0% on its
first request (13.7k system + tools), then 90–99% on every turn; the
Chat path (a chat-only provider) the same, 90–99%;
- masking on (redact + personal + local model): 98–99%;
- left idle 7 minutes: 99%, 98%; left idle 26 minutes: 99% (14336 of
14468);
- the misses are Claude Code's own requests: each session's first one,
the title call, an Explore subagent's first (5.6k, 0%), the auto
mode classifier's first (31.7k, 0%, and it misses again when Claude
Code changes its rules in that prompt), and --resume, where Claude
Code drops the thinking blocks from the history (76% on that turn).
A session with auto-mode tool calls and a subagent came to 76%.
Cause in magpie: affinity treated every vendor but the ChatGPT backend
as cold after five minutes (cacheCold). With two or more DeepSeek keys
or accounts (or a group over them), a conversation paused over five
minutes was handed to routing again and could go to another key, which
reads none of it from cache. DeepSeek's kv_cache guide (read 10-31)
says an unused cache is cleared "usually within a few hours to a few
days", and the 7 and 26 minute pauses above read 98–99% back.
Fix: coldAfter gives DeepSeek's own API (api.deepseek.com, Anthropic and
Chat paths alike) an hour, deepseekCold, as chatGPTCold does for Codex.
A group's rules read the same window for keepsEffort. Other vendors,
Anthropic included, keep five minutes. Nothing sent upstream changes.
Run (mote):
- TestDeepSeekConversationStaysPastFiveMinutes and TestDeepSeekColdAfter
red on the old code ("DeepSeek, 6 minutes on: why "cold" kept false";
"DeepSeek's cold is 5m0s"), green with the fix, beside the ChatGPT
cold, effort and affinity tests;
at this tree, rebased on origin/main ef8dfaa46 (affine's
new inOrder argument taken in);
- full suite (go test -tags nogui -timeout 45m ./...) started at the
pre-rebase tree b7545c89a and still running when this was pushed, by
the owner's rule not to hold a verified commit behind it; a red it
shows gets its own issue;
- gofmt -l clean; GOOS=linux and GOOS=windows go vet ./internal/gateway.
Not tried: a real move between two DeepSeek keys (one key here); a
pause past 26 minutes, so an hour is the docs' word plus that; other
prefix-caching vendors (Kimi, GLM, OpenAI-compatible relays) keep five
minutes, their unread TTLs unmeasured (#4515).
86 lines
3.7 KiB
Go
86 lines
3.7 KiB
Go
package gateway
|
||
|
||
import (
|
||
"net/http"
|
||
"testing"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
// A Claude Code conversation on one of two DeepSeek keys stays on it
|
||
// across a pause DeepSeek still has it cached through (on X: "Claude code
|
||
// 通过 Magpie 链接 DeepSeek 缓存命中率只有 50%"). DeepSeek clears an unused
|
||
// cache after hours, and a real conversation read 98–99% back after 7 and
|
||
// 26 minutes idle, but five minutes was its cold, so routing moved the
|
||
// conversation to the other key, which read none of it from cache.
|
||
// Another vendor's key is cold at five minutes as before.
|
||
func TestDeepSeekConversationStaysPastFiveMinutes(t *testing.T) {
|
||
deepseek := func(key string) candidate {
|
||
return candidate{p: provider.Provider{ID: "deepseek", Key: key, Chat: "https://api.deepseek.com/v1", Anthropic: "https://api.deepseek.com/anthropic"}, model: "deepseek-v4-pro", rest: "deepseek"}
|
||
}
|
||
other := func(key string) candidate {
|
||
return candidate{p: provider.Provider{ID: "ant", Key: key, Anthropic: "https://api.anthropic.com"}, model: "claude-sonnet-5-5", rest: "ant"}
|
||
}
|
||
first := []byte(`{"messages":[{"role":"user","content":"fix the bug"}]}`)
|
||
again := []byte(`{"messages":[{"role":"user","content":"fix the bug"},{"role":"assistant","content":"done"},{"role":"user","content":"now the tests"}]}`)
|
||
for i, c := range []struct {
|
||
name string
|
||
last, next candidate
|
||
ago time.Duration
|
||
why string
|
||
}{
|
||
{"DeepSeek, 6 minutes on", deepseek("sk-a"), deepseek("sk-b"), 6 * time.Minute, "cache"},
|
||
{"DeepSeek, 26 minutes on", deepseek("sk-a"), deepseek("sk-b"), 26 * time.Minute, "cache"},
|
||
{"DeepSeek, past its window", deepseek("sk-a"), deepseek("sk-b"), deepseekCold + time.Minute, "cold"},
|
||
{"another vendor, 6 minutes on", other("sk-a"), other("sk-b"), 6 * time.Minute, "cold"},
|
||
{"another vendor, 4 minutes on", other("sk-a"), other("sk-b"), 4 * time.Minute, "cache"},
|
||
} {
|
||
h := http.Header{}
|
||
h.Set("x-claude-code-session-id", "deepseek-cold-"+string(rune('a'+i)))
|
||
cs := []candidate{c.next, c.last}
|
||
pl := planned{order: []Weighed{{}, {}}}
|
||
_, _, _, key := affine("t", "", false, false, false, h, provider.Anthropic, first, first, cs, pl)
|
||
answered(key, c.last, 1, Usage{Input: 300, CacheRead: 13696})
|
||
t.Cleanup(func() { sticks.Lock(); delete(sticks.m, key); sticks.Unlock() })
|
||
sticks.Lock()
|
||
st := sticks.m[key]
|
||
st.at = st.at.Add(-c.ago)
|
||
sticks.m[key] = st
|
||
sticks.Unlock()
|
||
got, _, a, _ := affine("t", "", false, false, false, h, provider.Anthropic, again, again, cs, pl)
|
||
if a.Why != c.why || a.Kept != (c.why == "cache") {
|
||
t.Fatalf("%s: why %q kept %v, want %q", c.name, a.Why, a.Kept, c.why)
|
||
}
|
||
want := c.next
|
||
if c.why == "cache" {
|
||
want = c.last
|
||
}
|
||
if got[0].who() != want.who() {
|
||
t.Fatalf("%s: went to %s, want %s", c.name, got[0].who(), want.who())
|
||
}
|
||
}
|
||
}
|
||
|
||
// A group's rules keep a DeepSeek conversation's effort through the same
|
||
// pause (keepsEffort): the window comes from coldAfter.
|
||
func TestDeepSeekColdAfter(t *testing.T) {
|
||
ds := provider.Provider{ID: "deepseek", Anthropic: "https://api.deepseek.com/anthropic"}
|
||
if got := coldAfter(ds); got != deepseekCold {
|
||
t.Fatalf("DeepSeek's cold is %v, want %v", got, deepseekCold)
|
||
}
|
||
for _, p := range []provider.Provider{
|
||
{ID: "ant", Anthropic: "https://api.anthropic.com"},
|
||
{ID: "relay", Chat: "https://relay.example.com/v1"},
|
||
} {
|
||
if got := coldAfter(p); got != cacheCold {
|
||
t.Fatalf("%s's cold is %v, want %v", p.ID, got, cacheCold)
|
||
}
|
||
}
|
||
at := time.Date(2026, 10, 31, 9, 0, 0, 0, time.UTC)
|
||
tr := turnRule{effort: "high", at: at, answered: at, size: 40_000, cold: coldAfter(ds)}
|
||
if !keepsEffort(tr, "low", 41_000, at.Add(26*time.Minute)) {
|
||
t.Fatal("a DeepSeek conversation 26 minutes on dropped its effort")
|
||
}
|
||
}
|