Commit Graph
2 Commits
Author SHA1 Message Date
yetone cbc9dd7f00 gateway: Mistral's num_cached_tokens counts as the cache it read (daoche_wuzui on X)
Mistral documents its cache hits in usage.prompt_tokens_details.cached_tokens
(docs.mistral.ai prompt-caching), which was already read. Its API also sends
an older top-level num_cached_tokens: a Mistral engineer's client-python
PR #452 reads it as "the legacy top-level cached token count" with a
streaming fixture carrying it, and counts it inside prompt_tokens, as
OpenAI's is. Its current SDKs (TS 3.5.0, Python 3.7.0) name neither in
UsageInfo. A usage telling the cache only there was read as no hit:
magpie's cost and cache-hit rate showed 0% for Mistral.

cUsage now reads num_cached_tokens after prompt_tokens_details,
DeepSeek's and Moonshot's names, as a fallback. otherCacheFields
names it, so openAIUsage tells it again in prompt_tokens_details for
clients that read only that, streamed (chatTidy) and whole. The
translations to Messages and Responses are built from cUsage's Usage,
so they follow. Every chat usage decoder (chatDecoder, the whole reply,
continuation, sniff) goes through cUsage.

Tests: chat-mistral (num_cached_tokens alone) and
chat-mistral-zero-details (beside a 0 in prompt_tokens_details) in
TestUsageRecordsCacheTokensOnEveryRoute and
TestAgentReadsCacheTokensOnEveryRoute, every agent route, whole and
streamed; TestMayMendUsageOnlyOtherNames and
TestOpenAIUsageLeavesOpenAIsAlone (num_cached_tokens 0 stays byte for
byte). The other vendors' cases (OpenAI, DeepSeek, Moonshot,
Anthropic names, Responses, Gemini) are unchanged and pass.

Run (mica-run, mote over its cores): gofmt -l clean; go vet,
GOOS=linux go vet -tags nogui, GOOS=windows go vet of internal/gateway;
go test -count=1 ./internal/gateway ok (900s).
Broken by hand: without NumCachedTokens in cUsage's fallback, 30
mistral subtests fail and no other; without num_cached_tokens in
otherCacheFields, the passthrough chat and chat-stream Mistral cases
and TestMayMendUsageOnlyOtherNames fail.
Not tried: a real Mistral API reply (no Mistral key on this machine).
2026-11-13 13:02:39 +08:00
yetone defbb1a159 gateway: a Chat reply's cache read reaches the agent in OpenAI's names (B3TT3R on Discord)
B3TT3R (Discord #feedback, 10-25): DeepSeek Harness on DS-v4.1-Flash showed
"缓存命中 0%" over 1.2M input, and dsh's message usage carried only
inputTokens, outputTokens and totalTokens.

dsh reaches magpie through @deepseek-ai/dsh-llm-pi-ai with api
openai-completions, so its usage comes from pi-ai 1.4.0's parseChunkUsage:
cacheRead = prompt_tokens_details.cached_tokens ?? prompt_cache_hit_tokens
?? cached_tokens ?? 0, and nothing of Anthropic's. dsh's mapUsage drops a
0. magpie's Chat passthrough sent the upstream's usage as it came, so when
a provider told the cache only in another vendor's names (a relay giving
Anthropic's cache_read_input_tokens, or DeepSeek's prompt_cache_hit_tokens
or Moonshot's top-level cached_tokens beside a 0 in prompt_tokens_details)
the gateway counted the hit in its own usage log while the agent saw none.

Now chatTidy (stream) and chatWhole (whole reply) tell such a usage in
OpenAI's names too: prompt_tokens_details.cached_tokens, cache_write_tokens
when a write was told, and prompt_tokens counting both, with total_tokens
moved by the same amount. Every field the upstream sent stays. A usage in
OpenAI's names alone is never decoded (mayMendUsage looks for the other
names first), and one that already agrees comes back byte for byte.
Translated routes (Responses, Messages, Gemini) and plugin replies already
build the usage from cUsage and needed nothing.

Not proven: which provider B3TT3R uses. "DS-v4.1-Flash" is not how magpie
names a dsh model, and DeepSeek's own API sends both names, which pi-ai
already read right through magpie (checked live below). So this fixes the
case where magpie is at fault; if B3TT3R is on DeepSeek's own key the
cause is elsewhere and the reply asks.

Run, at this head (rebased on 4dfd88206):
- go test -count=1 -timeout 45m ./internal/gateway/... : ok (457s)
- gofmt -l internal/gateway docs: empty
- GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet
  ./internal/gateway/: clean

Broken by hand: with chattidy.go from the parent,
TestAgentReadsCacheTokensOnEveryRoute fails for chat-anthropic-names,
chat-zero-details and chat-moonshot, each on chat whole and chat stream;
every translated route and the plugin route pass there as on the new code.
TestUsageRecordsCacheTokensOnEveryRoute (gateway's own count),
TestOpenAIUsageLeavesOpenAIsAlone, TestMayMendUsageOnlyOtherNames and
TestPluginAgentReadsCacheTokens pass on the new code.

Real use, sandbox HOMEs, real dsh 0.2.1-rc.2 headless, real DeepSeek
behind a local relay that rewrites usage into Anthropic's names: through
the parent's binary dsh's session log has usage
{"inputTokens":367,"outputTokens":97,"totalTokens":464}, the reporter's
shape; through this one {"inputTokens":367,...,"cacheReadTokens":5376},
then 5632 and 5888. Straight to DeepSeek's own API (no relay), through
this change, dsh read cacheReadTokens 5248 to 5888; the parent sends that
usage byte for byte, so it was read the same before (not run live).

Not tried: a real Moonshot or OpenRouter key; the dsh desktop app (same
llm-pi-ai provider); the -responses and -anthropic dsh routes with a real
relay (covered by the route tests only).
2026-10-25 14:41:03 +08:00