Repository navigation
fix(aigateway): read input/output token usage in audio transcription responses - #1642
Open
pcbeingused333 wants to merge 2 commits into
Open
pcbeingused333 wants to merge 2 commits into
pcbeingused333 wants to merge 2 commits into
Conversation
…responses
OpenAI transcribe models (gpt-4o-transcribe, gpt-4o-mini-transcribe) report usage as {type:tokens,input_tokens,output_tokens,total_tokens}. The non-stream audio writer only read prompt_tokens/completion_tokens, so prompt and completion were recorded as 0 while the speech writer already maps input/output tokens. Fall back to input_tokens/output_tokens when the prompt/completion fields are absent.
CementZhang
reviewed
Nov 27, 2026
| if err := json.Unmarshal(body, &resp); err == nil { | ||
| rw.captureDuration(resp.Usage.Seconds, resp.Duration) | ||
| // OpenAI transcribe models report usage as input_tokens/output_tokens. | ||
| if resp.Usage.PromptTokens == 0 { |
Collaborator
There was a problem hiding this comment.
issue (blocking): [P1] 仅在旧计数字段缺失时回退
这里用 == 0 判断字段是否缺失,会把上游显式返回的零值也覆盖掉。例如,兼容上游同时返回 completion_tokens: 0 和 output_tokens: 52 时,网关会把 completion 用量记录为 52。输入方向也有同样的问题。可以把两个旧字段解析为 *int64,只在字段未提供时读取别名,并加一个两套字段并存、旧值为零的回归用例。
Contributor
Author
There was a problem hiding this comment.
已修复(db66f92e):prompt_tokens / completion_tokens 现在解析为 *int64,仅当字段未提供时才读取 input_tokens / output_tokens,显式返回的 0 会保留。新增了两套字段并存且旧值为 0 的回归用例。
This comment was drafted with Claude Code.
…bsent Parse prompt_tokens/completion_tokens as pointers so an explicit zero from the upstream is kept instead of being replaced by input_tokens/output_tokens. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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.
What is this feature?
A fix for usage accounting on non-stream audio transcription responses:
usage.input_tokens/usage.output_tokensare now recorded as prompt / completion tokens.Why do we need this feature?
OpenAI transcribe models (
gpt-4o-transcribe,gpt-4o-mini-transcribe) returnusageas{"type":"tokens","input_tokens":N,"output_tokens":M,"total_tokens":T}.nonStreamAudioResponseWriter.captureTextonly deserializedprompt_tokens/completion_tokens, so onlytotal_tokenswas recorded and prompt and completion tokens were stored as 0.speechAudioResponseWriteralready mapsinput_tokens/output_tokens. The writer now falls back to those fields when theprompt_*/completion_*ones are absent, so the existing format is unchanged.Who is this feature for?
Operators metering audio transcription through the AI gateway with OpenAI-style transcribe models.
Which issue(s) does this PR fix?:
No separate issue.
Special notes for your reviewer:
New test
TestResponseWriterWrapperAudio_UsesInputOutputTokensUsagefails before the change (prompt tokens 0, expected 371) and passes after;go test ./aigateway/handler/passes.This change was prepared with the help of an AI assistant (Claude Code), and the author is responsible for the change.