Support reasoning_tokens with openai style in serving_chat#17764
Support reasoning_tokens with openai style in serving_chat#17764lw9527 wants to merge 4 commits into
Conversation
Summary of ChangesHello @lw9527, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request integrates the capability to track and report 'reasoning tokens' within the OpenAI-compatible chat serving endpoint. This enhancement provides users with a more granular breakdown of token consumption, distinguishing between standard completion tokens and those generated during the model's internal reasoning steps. The feature is configurable via a new server-side flag, allowing for flexible adoption and detailed cost analysis for complex model interactions. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for reasoning_tokens in the OpenAI-style serving, including new fields in the protocol, logic for calculating these tokens in chat and responses, and corresponding server arguments and tests. The changes are well-covered by tests, ensuring the new functionality works as expected for both streaming and non-streaming scenarios, and for multiple choices. Overall, the implementation is robust and adds valuable functionality.
| completion_tokens_details = None | ||
| if num_reasoning_tokens > 0: | ||
| completion_tokens_details = CompletionTokensDetails( | ||
| reasoning_tokens=num_reasoning_tokens, | ||
| ) |
There was a problem hiding this comment.
The completion_tokens_details is only created if num_reasoning_tokens > 0. However, num_reasoning_tokens is only populated within the if self.use_harmony: block (lines 451-458). If self.use_harmony is False, num_reasoning_tokens will always be 0, preventing completion_tokens_details from being set for non-Harmony contexts, even if reasoning tokens were calculated and available in meta_info from serving_chat.py.
| reasoning_parser = ReasoningParser( | ||
| self.reasoning_parser, | ||
| request.stream_reasoning, | ||
| is_force_reasoning, |
There was a problem hiding this comment.
The ReasoningParser is instantiated inside the loop, which means it will be created for each choice (request.n). If the model_type, stream_reasoning, and force_reasoning are constant for all choices within a single request, it would be more efficient to instantiate reasoning_parser once outside this loop and reuse it.
| reasoning_parser = ReasoningParser( | |
| self.reasoning_parser, | |
| request.stream_reasoning, | |
| is_force_reasoning, | |
| reasoning_parser_instance = ReasoningParser( | |
| self.reasoning_parser, | |
| request.stream_reasoning, | |
| is_force_reasoning, | |
| ) | |
| for index, full_content in stream_buffers.items(): | |
| reasoning_tokens[index] = ( | |
| reasoning_parser_instance.calculate_reasoning_tokens( | |
| full_content, self.tokenizer_manager.tokenizer | |
| ) | |
| ) |
3fa4303 to
8c5be32
Compare
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Confirmation: We independently tested a similar fix and it works! ✅We encountered the same Our Test ResultsEnvironment:
Non-streaming test: curl -s http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model":"glm-4.7","messages":[{"role":"user","content":"What is 2+2?"}]}' \
| jq '.usage'Result: {
"prompt_tokens": 12,
"total_tokens": 88,
"completion_tokens": 76,
"reasoning_tokens": 67
}✅ Streaming test: curl -s http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model":"glm-4.7","messages":[{"role":"user","content":"What is 2+2?"}],"stream":true,"stream_options":{"include_usage":true}}' \
| grep '"usage"' | tail -1 | sed 's/^data: //' | jq '.usage'Result: {
"prompt_tokens": 12,
"total_tokens": 127,
"completion_tokens": 115,
"reasoning_tokens": 105
}✅ Why This Approach is CorrectWe analyzed the codebase and confirmed:
Your solution of re-tokenizing the reasoning text at the API layer is pragmatic and works. While the "ideal" fix would be tracking tokens at the engine level, that would be a much larger change requiring modifications to the generation pipeline. ObservationsYour PR improves on our initial implementation by:
Performance NoteThe re-tokenization overhead is minimal since:
RecommendationThis PR should be merged. We've been running this fix in production with several reasoning models (GLM-4.7, DeepSeek-R1, QwQ) and it works reliably. The bug significantly impacts usage tracking and billing accuracy for reasoning models. Tested models:
Happy to provide additional test results or feedback if helpful! |
|
Closing as duplicate of #15562 which has been merged. |
Motivation
Support reasoning_tokens with openai style in serving_chat
Modifications
Accuracy Tests
non stream

import openai client = openai.Client( api_key="", base_url="http://127.0.0.1:40823/v1", ) response = client.chat.completions.create( model="default", messages=[{"role":"user","content":"who are you?"}], max_tokens=20000, temperature=1, stream=False, stream_options={"include_usage":True} ) print(response)stream

Benchmarking and Profiling
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci