fix(apicompat): Anthropic 转 Responses 时按 OpenAI 语义汇总 input_tokens#1
Open
stabey wants to merge 1 commit into
Open
fix(apicompat): Anthropic 转 Responses 时按 OpenAI 语义汇总 input_tokens#1stabey wants to merge 1 commit into
stabey wants to merge 1 commit into
Conversation
…hropic to Responses Anthropic Messages reports input_tokens excluding cache_read/cache_creation, but OpenAI Responses input_tokens is the total including cached tokens. The reverse converter passed Anthropic's input_tokens straight through, so client-facing prompt_tokens/input_tokens were short by the cached count and cache_creation was dropped entirely. Fix the non-stream path and the streaming state machine to add cache_read + cache_creation back into input_tokens, and track CacheCreationInputTokens on the streaming state. Six downstream paths benefit (Anthropic->Responses, Anthropic->ChatCompletions, Gemini->ChatCompletions, each sync + stream). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
5132367 to
89dffdd
Compare
3 tasks
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.
概述
input_tokens不包含cache_read_input_tokens和cache_creation_input_tokens;而 OpenAI Responses 的input_tokens是包含缓存命中的总输入。backend/internal/pkg/apicompat/anthropic_to_responses_response.go里的AnthropicToResponsesResponse和流式 state machine)把 Anthropic 的InputTokens直接透传给了 OpenAI 侧,导致客户端看到的prompt_tokens/input_tokens少计了缓存命中部分,cache_creation则被完全丢弃。makeResponsesCompletedEvent,在写入InputTokens时把cache_read + cache_creation加回去;同时在流式 state 中新增CacheCreationInputTokens字段,并在message_start和message_delta中正确捕获。gateway_forward_as_responses.gogateway_forward_as_chat_completions.gogemini_chat_completions_compat_service.go(链路:Gemini → Anthropic → Responses → CC)计费路径不受影响:计费读的是
ForwardResult.Usage(Anthropic 语义,从原始事件构建),不依赖被翻坏的响应体。测试计划
go test ./internal/pkg/apicompat/...—— 新增测试覆盖非流式 + 流式,cache 字段分别在message_start和message_delta中下发两种情况go test -tags unit -run "TestHandleResponses|TestHandleCC|Gemini|Antigravity" ./internal/service/go vet ./internal/pkg/apicompat/... ./internal/service/...新增测试位于
backend/internal/pkg/apicompat/anthropic_responses_test.go:TestAnthropicToResponsesResponse_CacheTokensUseOpenAIInputSemanticsTestAnthropicToResponsesResponse_NoCacheTokensTestAnthropicEventToResponses_CacheTokensRoundTripFromMessageStartTestAnthropicEventToResponses_CacheTokensFromMessageDelta🤖 Generated with Claude Code