Skip to content

fix(usage): parse Kimi K2 cached_tokens from prompt_tokens_details#25436

Merged
steipete merged 2 commits intoopenclaw:mainfrom
Elarwei001:fix/kimi-k2-cache-stats
Feb 24, 2026
Merged

fix(usage): parse Kimi K2 cached_tokens from prompt_tokens_details#25436
steipete merged 2 commits intoopenclaw:mainfrom
Elarwei001:fix/kimi-k2-cache-stats

Conversation

@Elarwei001
Copy link
Copy Markdown
Contributor

@Elarwei001 Elarwei001 commented Feb 24, 2026

Summary

Fixes #7073 - Kimi K2.5 cache stats showing cacheRead: 0

Root 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: Add prompt_tokens_details.cached_tokens to cacheRead parsing chain
  • src/agents/usage.test.ts: Add test for K2 usage format

Testing

Verified with real Kimi K2.5 API calls - cached_tokens is 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_tokens in 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

  • This PR is safe to merge with minimal risk
  • The change is straightforward and well-tested. It extends the existing fallback chain in normalizeUsage to 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.
  • No files require special attention

Last reviewed commit: d2a1cf0

Elarwei001 and others added 2 commits February 24, 2026 14:40
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
@steipete steipete force-pushed the fix/kimi-k2-cache-stats branch from d2a1cf0 to 181a502 Compare February 24, 2026 14:40
@steipete steipete merged commit 760671e into openclaw:main Feb 24, 2026
9 checks passed
@steipete
Copy link
Copy Markdown
Contributor

Landed via temp rebase onto main.

  • Targeted tests: pnpm test src/agents/usage.test.ts
  • Land commit: 181a502d4124f2d6cbef8278f07f4b59911cc91f
  • Merge commit: 760671e

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
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Support Kimi K2.5 Cache

2 participants