fix(usage): parse Kimi K2 cached_tokens from prompt_tokens_details#25436
Merged
steipete merged 2 commits intoopenclaw:mainfrom Feb 24, 2026
Merged
fix(usage): parse Kimi K2 cached_tokens from prompt_tokens_details#25436steipete merged 2 commits intoopenclaw:mainfrom
steipete merged 2 commits intoopenclaw:mainfrom
Conversation
This was referenced Feb 24, 2026
Kimi K2 models use automatic prefix caching and return cache stats in a nested field: usage.prompt_tokens_details.cached_tokens This fixes issue openclaw#7073 where cacheRead was showing 0 for K2.5 users. Also adds cached_tokens (top-level) for moonshot-v1 explicit caching API. Closes openclaw#7073
d2a1cf0 to
181a502
Compare
Contributor
|
Landed via temp rebase onto main.
Thanks @Elarwei001! |
obviyus
pushed a commit
to Glucksberg/OpenClaw
that referenced
this pull request
Feb 24, 2026
margulans
pushed a commit
to margulans/Neiron-AI-assistant
that referenced
this pull request
Feb 25, 2026
Jackson3195
pushed a commit
to Jackson3195/openclaw-with-a-personal-touch
that referenced
this pull request
Feb 25, 2026
kevinWangSheng
pushed a commit
to kevinWangSheng/openclaw
that referenced
this pull request
Feb 26, 2026
kevinWangSheng
pushed a commit
to kevinWangSheng/openclaw
that referenced
this pull request
Feb 26, 2026
brianleach
pushed a commit
to brianleach/openclaw
that referenced
this pull request
Feb 26, 2026
2 tasks
execute008
pushed a commit
to execute008/openclaw
that referenced
this pull request
Feb 27, 2026
r4jiv007
pushed a commit
to r4jiv007/openclaw
that referenced
this pull request
Feb 28, 2026
6 tasks
hughdidit
pushed a commit
to hughdidit/DAISy-Agency
that referenced
this pull request
Mar 1, 2026
…nks @Elarwei001) (cherry picked from commit 760671e) # Conflicts: # CHANGELOG.md
hughdidit
pushed a commit
to hughdidit/DAISy-Agency
that referenced
this pull request
Mar 3, 2026
…nks @Elarwei001) (cherry picked from commit 760671e) # Conflicts: # CHANGELOG.md
joelnishanth
pushed a commit
to joelnishanth/openclaw
that referenced
this pull request
Mar 3, 2026
zooqueen
pushed a commit
to hanzoai/bot
that referenced
this pull request
Mar 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #7073 - Kimi K2.5 cache stats showing
cacheRead: 0Root Cause
Kimi K2 models use automatic prefix caching (like Anthropic) and return cache stats in a nested field:
{ "usage": { "prompt_tokens": 1113, "cached_tokens": 1024, "prompt_tokens_details": { "cached_tokens": 1024 } } }OpenClaw wasn't parsing
prompt_tokens_details.cached_tokens, so cache stats were lost.Changes
src/agents/usage.ts: Addprompt_tokens_details.cached_tokensto cacheRead parsing chainsrc/agents/usage.test.ts: Add test for K2 usage formatTesting
Verified with real Kimi K2.5 API calls -
cached_tokensis correctly returned on repeated requests with the same prefix.Note
This also adds
cached_tokens(top-level) for moonshot-v1 explicit caching API, which will be used by a separate PR (#25104).Greptile Summary
Adds support for Kimi K2 automatic prefix caching stats by parsing
prompt_tokens_details.cached_tokensin the usage normalization chain. This fixes cache stats showing as 0 for K2 models that return cache information in a nested field structure (similar to Anthropic). The implementation includes proper test coverage for both the Moonshot v1 explicit caching format (cached_tokens) and the K2 automatic prefix caching format (prompt_tokens_details.cached_tokens).Confidence Score: 5/5
normalizeUsageto handle two additional cache token formats without breaking existing behavior. The implementation follows the established pattern of checking multiple field variations using the nullish coalescing operator. Both new formats have dedicated unit tests that verify correct parsing.Last reviewed commit: d2a1cf0