Repository navigation
fix(gateway): normalize reasoning tokens #3774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
8c0c05b
d5a8e8a
7dd44d0
179e64d
0e4aa54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,34 @@ export function extractBedrockCacheCreationDetails(usage: any): { | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Normalize completion tokens to the inclusive billable output count. | ||
| * | ||
| * Most providers already include reasoning in completion tokens. A few return | ||
| * visible completion and reasoning separately, which is unambiguous when the | ||
| * reported total equals prompt + completion + reasoning. | ||
| */ | ||
| export function normalizeCompletionTokens( | ||
| promptTokens: number | null, | ||
| completionTokens: number | null, | ||
| reasoningTokens: number | null, | ||
| totalTokens: number | null, | ||
| ): number | null { | ||
| if ( | ||
| promptTokens === null || | ||
| completionTokens === null || | ||
| reasoningTokens === null || | ||
| reasoningTokens <= 0 || | ||
| totalTokens === null | ||
| ) { | ||
| return completionTokens; | ||
|
Comment on lines
+77
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an xAI-style response reports Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| return totalTokens === promptTokens + completionTokens + reasoningTokens | ||
| ? completionTokens + reasoningTokens | ||
| : completionTokens; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts token usage information from streaming data based on provider format | ||
| */ | ||
|
|
@@ -224,7 +252,10 @@ export function extractTokenUsage( | |
| promptTokens = data.usage.prompt_tokens ?? null; | ||
| completionTokens = data.usage.completion_tokens ?? null; | ||
| totalTokens = data.usage.total_tokens ?? null; | ||
| reasoningTokens = data.usage.reasoning_tokens ?? null; | ||
| reasoningTokens = | ||
| data.usage.reasoning_tokens ?? | ||
| data.usage.completion_tokens_details?.reasoning_tokens ?? | ||
| null; | ||
| cachedTokens = data.usage.prompt_tokens_details?.cached_tokens ?? null; | ||
| const cacheCreation = | ||
| data.usage.prompt_tokens_details?.cache_creation_input_tokens ?? 0; | ||
|
|
@@ -234,22 +265,6 @@ export function extractTokenUsage( | |
| } | ||
| } | ||
| break; | ||
| case "xai": | ||
| case "vertex-openai": | ||
| // xAI and Vertex's xAI endpoint report reasoning outside | ||
| // `completion_tokens`, so the nested count has to reach the cost engine | ||
| // to be billed. The default OpenAI branch only reads a top-level count. | ||
| if (data.usage) { | ||
| promptTokens = data.usage.prompt_tokens ?? null; | ||
| completionTokens = data.usage.completion_tokens ?? null; | ||
| totalTokens = data.usage.total_tokens ?? null; | ||
| reasoningTokens = | ||
| data.usage.completion_tokens_details?.reasoning_tokens ?? | ||
| data.usage.reasoning_tokens ?? | ||
| null; | ||
| cachedTokens = data.usage.prompt_tokens_details?.cached_tokens ?? null; | ||
| } | ||
| break; | ||
| case "sakana": | ||
| // Fugu streams over Chat Completions and bills the orchestration tokens | ||
| // consumed by its underlying agent pool on top of the user-visible | ||
|
|
@@ -295,7 +310,10 @@ export function extractTokenUsage( | |
| promptTokens = data.usage.prompt_tokens ?? null; | ||
| completionTokens = data.usage.completion_tokens ?? null; | ||
| totalTokens = data.usage.total_tokens ?? null; | ||
| reasoningTokens = data.usage.reasoning_tokens ?? null; | ||
| reasoningTokens = | ||
| data.usage.reasoning_tokens ?? | ||
| data.usage.completion_tokens_details?.reasoning_tokens ?? | ||
| null; | ||
| cachedTokens = data.usage.prompt_tokens_details?.cached_tokens ?? null; | ||
| // GPT-5.6+ bills prompt-cache writes at 1.25x and reports them in | ||
| // `cache_write_tokens` (a subset of prompt_tokens, like cached_tokens). | ||
|
|
@@ -308,6 +326,13 @@ export function extractTokenUsage( | |
| break; | ||
| } | ||
|
|
||
| completionTokens = normalizeCompletionTokens( | ||
| promptTokens, | ||
| completionTokens, | ||
| reasoningTokens, | ||
| totalTokens, | ||
| ); | ||
|
|
||
| return { | ||
| promptTokens, | ||
| completionTokens, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
EXPERIMENTALis enabled and an upstream response reports positive reasoning tokens, this now returnstotal_tokens = prompt_tokens + completion_tokens, butapps/gateway/src/chat-basic.e2e.ts:161-165still asserts thatreasoning_tokensmust be added again. Any reasoning-capable model exercised by that suite will therefore fail even thoughcompletion_tokenshas intentionally become inclusive; update the existing assertion as part of this semantic change.Useful? React with 👍 / 👎.