Skip to content

feat(adapters): Anthropic+Gemini ResponseParser 补读 cachedTokens - #2568

Open
littlebird69 wants to merge 2 commits into
agentscope-ai:mainfrom
littlebird69:feat/cache-pricing
Open

feat(adapters): Anthropic+Gemini ResponseParser 补读 cachedTokens#2568
littlebird69 wants to merge 2 commits into
agentscope-ai:mainfrom
littlebird69:feat/cache-pricing

Conversation

@littlebird69

Copy link
Copy Markdown

问题

ChatUsagecachedTokens 字段,但 Anthropic 和 Gemini 的 ResponseParser 从没填过它——恒为 0。

两个供应商其实在响应里都返回了缓存命中 token 数:

  • AnthropicUsage.cacheReadInputTokens()(字段 cache_read_input_tokens
  • GeminiGenerateContentResponseUsageMetadata.cachedContentTokenCount()

下游依赖 getCachedTokens() 的代码(如按缓存命中拆算金额的用量账本)即使在缓存命中时也拿到 0。

改动

  • AnthropicResponseParser.parseMessage:补读 cacheReadInputTokens()(非流式)
  • GeminiResponseParser.parseResponse:补读 cachedContentTokenCount()
  • DashScope / Ollama:不改(其 DTO 无此字段)
  • 流式路径不改(保持改动范围最小;Anthropic 流式 MessageDeltaUsage 也暴露了 cacheReadInputTokens(),可作为后续增强)

两个 SDK 返回类型不同(Optional<Long> vs Optional<Integer>),两侧写法略有差异,已内联注释说明。

测试

两个 parser 各加一条测试,断言 getCachedTokens() 等于构造响应里的缓存值。既有测试全过。

@CLAassistant

CLAassistant commented Aug 5, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@oss-maintainer

Copy link
Copy Markdown
Collaborator

⚠️ Merge conflict detected

This PR has conflicts with the main branch and cannot be merged. Please rebase or merge main into your branch and resolve the conflicts:

git fetch origin
git checkout feat/cache-pricing
git rebase origin/main
# resolve conflicts, then:
git push --force-with-lease

This is a one-time reminder. Feel free to @mention me for a re-review after conflicts are resolved.


Automated notification by github-manager-bot

littlebird69 and others added 2 commits August 5, 2026 17:36
Anthropic 非流式 parseMessage: message.usage().cacheReadInputTokens() 返回 Optional<Long>,orElse(0L).intValue() 强转(Gemini 侧是 Optional<Integer> 不需强转,两侧写法不同)。Gemini parseResponse: metadata.cachedContentTokenCount()。DashScope/Ollama 不改(供应商 DTO 无 cached 字段)。流式 parseStreamEvents 不改(Anthropic MessageDelta.usage() 只有 outputTokens,无缓存字段)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@littlebird69

Copy link
Copy Markdown
Author

👍 轻提醒一下这个 PR。

它让 Anthropic 和 Gemini 的 ResponseParser 读取供应商响应里已有的 cachedTokens——ChatUsage 有这个字段,但两个 parser 一直没填(恒为 0),导致下游依赖 getCachedTokens() 的场景(比如按缓存命中拆算用量金额)拿不到真实值。

改动很小(每个 parser 各加一行 + 对应测试),已 rebase 到最新 main、无冲突,既有测试全过。两个 SDK 返回类型不同(Optional<Long> vs Optional<Integer>)已内联注释说明。

麻烦有空时帮忙看看,谢谢!如果有需要先开 Issue 或调整写法的地方,也请告知。

@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Reviewed the changes in this PR. The modifications look reasonable and follow the project conventions.


Automated review by "github-manager-bot"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants