Files
magpie/docs
yetone f63cf0bfc2 affinity: a DeepSeek conversation stays on its key past five minutes, as DeepSeek keeps it cached for hours
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).
2026-10-31 11:02:36 +08:00
..