Skip to content

fix(apicompat): Responses↔Anthropic 转换补齐 cache_creation_input_tokens#3991

Merged
Wei-Shaw merged 2 commits into
Wei-Shaw:mainfrom
fengshao1227:fix/anthropic-usage-cache-creation-tokens
Jul 10, 2026
Merged

fix(apicompat): Responses↔Anthropic 转换补齐 cache_creation_input_tokens#3991
Wei-Shaw merged 2 commits into
Wei-Shaw:mainfrom
fengshao1227:fix/anthropic-usage-cache-creation-tokens

Conversation

@fengshao1227

Copy link
Copy Markdown
Contributor

背景

Fixes #3990

#3898 给 ResponsesUsage 加了 CacheCreationInputTokens 字段,但 Responses↔Anthropic 双向转换没跟上,导致 cache creation token 在协议桥接时丢失。

问题

  1. Responses→Anthropic(anthropicUsageFromResponsesUsage):input_tokens 只减了 cache_read 没减 cache_creation,多算了;CacheCreationInputTokens 始终 0
  2. Anthropic→Responses 非流式(AnthropicToResponsesResponse):cache_creation 加进了 total 但没写 CacheCreationInputTokens 字段
  3. Anthropic→Responses 流式 completed event:同上

修改

  • responses_to_anthropic.go:anthropicUsageFromResponsesUsage 减去 cache_creation 并设置 CacheCreationInputTokens
  • anthropic_to_responses_response.go:非流式和流式 completed 都写入 CacheCreationInputTokens

测试

go test ./internal/pkg/apicompat/ -run CacheCreation -v  # 3 个新测试
go test ./internal/pkg/apicompat/ -count=1              # 全量通过

@Wei-Shaw Wei-Shaw merged commit 07fac34 into Wei-Shaw:main Jul 10, 2026
7 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Responses↔Anthropic 协议转换丢失 cache_creation_input_tokens

2 participants