Files
magpie/docs
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
..