[Bugfix][Frontend] Count Responses reasoning tokens per tool round (#58927)

Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
Flora Feng
2026-09-28 00:34:38 +00:00
committed by GitHub
parent 31afa4f1bb
commit 3184226984
4 changed files with 102 additions and 64 deletions
@@ -359,6 +359,10 @@ class ParsableContext(ConversationContext):
enable_auto_tools=self.enable_auto_tools,
model_output_token_ids=completion.token_ids,
)
# Each round is a separate generation, so count it on its own ids.
self.num_reasoning_tokens += self.response_parser.count_reasoning_tokens(
completion.token_ids or []
)
if not self.request.include_reasoning:
reasoning = None
self.response_messages.extend(
+5 -11
View File
@@ -832,18 +832,12 @@ class OpenAIServingResponses(GenerateBaseServing):
num_generated_tokens = context.num_output_tokens
num_cached_tokens = context.num_cached_tokens
num_reasoning_tokens = context.num_reasoning_tokens
# For text-based reasoning parsers (e.g., <think>...</think>),
# HarmonyContext already counts reasoning tokens via channels.
# For Simple/Parsable contexts, derive reasoning_tokens from
# accumulated output token IDs using the parser if not already set.
if (
num_reasoning_tokens == 0
and isinstance(context, (SimpleContext, ParsableContext))
and context.response_parser is not None
):
accumulated = getattr(context, "_accumulated_token_ids", []) or []
# HarmonyContext and ParsableContext count reasoning tokens as each
# round is appended. SimpleContext is single-round but accumulates
# streaming deltas, so count its full output once here.
if isinstance(context, SimpleContext) and context.response_parser is not None:
num_reasoning_tokens = context.response_parser.count_reasoning_tokens(
accumulated
context._accumulated_token_ids
)
usage = ResponseUsage(