Skip to content

fix(aigateway): read input/output token usage in audio transcription responses - #1642

Open
pcbeingused333 wants to merge 2 commits into
OpenCSGs:mainfrom
pcbeingused333:fix/transcription-input-output-tokens
Open

pcbeingused333 wants to merge 2 commits into
OpenCSGs:mainfrom
pcbeingused333:fix/transcription-input-output-tokens

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

What is this feature?

A fix for usage accounting on non-stream audio transcription responses: usage.input_tokens / usage.output_tokens are now recorded as prompt / completion tokens.

Why do we need this feature?

OpenAI transcribe models (gpt-4o-transcribe, gpt-4o-mini-transcribe) return usage as {"type":"tokens","input_tokens":N,"output_tokens":M,"total_tokens":T}. nonStreamAudioResponseWriter.captureText only deserialized prompt_tokens / completion_tokens, so only total_tokens was recorded and prompt and completion tokens were stored as 0. speechAudioResponseWriter already maps input_tokens / output_tokens. The writer now falls back to those fields when the prompt_* / 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_UsesInputOutputTokensUsage fails 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.

…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.
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 {

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.

issue (blocking): [P1] 仅在旧计数字段缺失时回退

这里用 == 0 判断字段是否缺失,会把上游显式返回的零值也覆盖掉。例如,兼容上游同时返回 completion_tokens: 0 和 output_tokens: 52 时,网关会把 completion 用量记录为 52。输入方向也有同样的问题。可以把两个旧字段解析为 *int64,只在字段未提供时读取别名,并加一个两套字段并存、旧值为零的回归用例。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

已修复(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>
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.

2 participants