Skip to content

Conversation

@nestharus
Copy link
Contributor

#356

PR details:

  • Base: dev
  • Title: fix(codex): estimate reasoning tokens from accumulated content when upstream omits them
  • Body:

Summary

  • Adds AccumulatedReasoningLen field to track reasoning content length during streaming
  • Estimates reasoning tokens (~4 chars per token) when upstream doesn't provide
    output_tokens_details.reasoning_tokens
  • Applies to both streaming and non-streaming Codex responses

Test plan

  • Verify reasoning_tokens is populated in usage when upstream omits it
  • Verify upstream reasoning_tokens is preserved when provided

…pstream omits them

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@chatgpt-codex-connector
Copy link

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@gemini-code-assist
Copy link

Summary of Changes

Hello @nestharus, 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 enhances the Codex response translation logic by introducing a robust mechanism to handle cases where upstream services do not provide explicit reasoning_tokens. It ensures that token usage metrics are consistently available by estimating these tokens from the accumulated reasoning content, thereby improving the reliability of usage reporting for both streaming and non-streaming API interactions.

Highlights

  • Reasoning Token Estimation: Introduced a mechanism to estimate reasoning_tokens when the upstream service does not provide them, using an approximation of 4 characters per token.
  • Accumulated Reasoning Length Tracking: Added an AccumulatedReasoningLen field to ConvertCliToOpenAIParams to track the total length of reasoning content during streaming, which is then used for token estimation.
  • Consistent Application: Ensured the reasoning token estimation logic is applied consistently to both streaming and non-streaming Codex responses.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a fallback mechanism to estimate reasoning tokens for both streaming and non-streaming Codex responses when the upstream service does not provide them. The implementation is logical and correctly handles the estimation. My review includes suggestions to improve code quality by replacing a magic number with a named constant and addressing code duplication for the token estimation logic. These changes will enhance the code's readability and maintainability.

Comment on lines +99 to +103
// Estimate reasoning tokens from accumulated content (approx 4 chars per token)
estimatedTokens := int64((*param).(*ConvertCliToOpenAIParams).AccumulatedReasoningLen / 4)
if estimatedTokens == 0 {
estimatedTokens = 1
}

Choose a reason for hiding this comment

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

medium

The magic number 4 is used for token estimation. It's better to define this as a constant, like const defaultCharsPerToken = 4, to improve readability and maintainability. Also, this token estimation logic is duplicated in the non-streaming function (ConvertCodexResponseToOpenAINonStream). You might consider extracting it into a helper function to avoid code duplication and ensure consistency.

Suggested change
// Estimate reasoning tokens from accumulated content (approx 4 chars per token)
estimatedTokens := int64((*param).(*ConvertCliToOpenAIParams).AccumulatedReasoningLen / 4)
if estimatedTokens == 0 {
estimatedTokens = 1
}
// Estimate reasoning tokens from accumulated content (approx 4 chars per token)
const defaultCharsPerToken = 4
estimatedTokens := int64((*param).(*ConvertCliToOpenAIParams).AccumulatedReasoningLen / defaultCharsPerToken)
if estimatedTokens == 0 {
estimatedTokens = 1
}

Comment on lines +298 to +302
// Estimate from reasoning content (approx 4 chars per token)
reasoningTokens = int64(len(reasoningText) / 4)
if reasoningTokens == 0 {
reasoningTokens = 1
}

Choose a reason for hiding this comment

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

medium

The magic number 4 is used here for token estimation, similar to the streaming implementation. To improve maintainability and ensure consistency, this should be a shared constant. As this logic is duplicated, consider creating a common helper function for token estimation.

			// Estimate from reasoning content (approx 4 chars per token)
			const defaultCharsPerToken = 4
			reasoningTokens = int64(len(reasoningText) / defaultCharsPerToken)
			if reasoningTokens == 0 {
				reasoningTokens = 1
			}

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.

1 participant