-
-
Notifications
You must be signed in to change notification settings - Fork 297
fix(codex): estimate reasoning tokens from accumulated content when u… #357
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
Open
nestharus
wants to merge
1
commit into
router-for-me:dev
Choose a base branch
from
nestharus:fix/codex-accumulated-reasoning-tokens
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+36
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,10 +20,11 @@ var ( | |
|
|
||
| // ConvertCliToOpenAIParams holds parameters for response conversion. | ||
| type ConvertCliToOpenAIParams struct { | ||
| ResponseID string | ||
| CreatedAt int64 | ||
| Model string | ||
| FunctionCallIndex int | ||
| ResponseID string | ||
| CreatedAt int64 | ||
| Model string | ||
| FunctionCallIndex int | ||
| AccumulatedReasoningLen int // Track reasoning content length for token estimation | ||
| } | ||
|
|
||
| // ConvertCodexResponseToOpenAI translates a single chunk of a streaming response from the | ||
|
|
@@ -90,15 +91,26 @@ func ConvertCodexResponseToOpenAI(_ context.Context, modelName string, originalR | |
| if inputTokensResult := usageResult.Get("input_tokens"); inputTokensResult.Exists() { | ||
| template, _ = sjson.Set(template, "usage.prompt_tokens", inputTokensResult.Int()) | ||
| } | ||
| if reasoningTokensResult := usageResult.Get("output_tokens_details.reasoning_tokens"); reasoningTokensResult.Exists() { | ||
| // Extract reasoning tokens from upstream, or estimate from accumulated content | ||
| reasoningTokensResult := usageResult.Get("output_tokens_details.reasoning_tokens") | ||
| if reasoningTokensResult.Exists() && reasoningTokensResult.Int() > 0 { | ||
| template, _ = sjson.Set(template, "usage.completion_tokens_details.reasoning_tokens", reasoningTokensResult.Int()) | ||
| } else if (*param).(*ConvertCliToOpenAIParams).AccumulatedReasoningLen > 0 { | ||
| // Estimate reasoning tokens from accumulated content (approx 4 chars per token) | ||
| estimatedTokens := int64((*param).(*ConvertCliToOpenAIParams).AccumulatedReasoningLen / 4) | ||
| if estimatedTokens == 0 { | ||
| estimatedTokens = 1 | ||
| } | ||
| template, _ = sjson.Set(template, "usage.completion_tokens_details.reasoning_tokens", estimatedTokens) | ||
| } | ||
| } | ||
|
|
||
| if dataType == "response.reasoning_summary_text.delta" { | ||
| if deltaResult := rootResult.Get("delta"); deltaResult.Exists() { | ||
| template, _ = sjson.Set(template, "choices.0.delta.role", "assistant") | ||
| template, _ = sjson.Set(template, "choices.0.delta.reasoning_content", deltaResult.String()) | ||
| // Accumulate reasoning content length for token estimation | ||
| (*param).(*ConvertCliToOpenAIParams).AccumulatedReasoningLen += len(deltaResult.String()) | ||
| } | ||
| } else if dataType == "response.reasoning_summary_text.done" { | ||
| template, _ = sjson.Set(template, "choices.0.delta.role", "assistant") | ||
|
|
@@ -194,6 +206,9 @@ func ConvertCodexResponseToOpenAINonStream(_ context.Context, _ string, original | |
| template, _ = sjson.Set(template, "id", idResult.String()) | ||
| } | ||
|
|
||
| // Track upstream reasoning tokens for later estimation | ||
| var upstreamReasoningTokens int64 | ||
|
|
||
| // Extract and set usage metadata (token counts). | ||
| if usageResult := responseResult.Get("usage"); usageResult.Exists() { | ||
| if outputTokensResult := usageResult.Get("output_tokens"); outputTokensResult.Exists() { | ||
|
|
@@ -206,7 +221,7 @@ func ConvertCodexResponseToOpenAINonStream(_ context.Context, _ string, original | |
| template, _ = sjson.Set(template, "usage.prompt_tokens", inputTokensResult.Int()) | ||
| } | ||
| if reasoningTokensResult := usageResult.Get("output_tokens_details.reasoning_tokens"); reasoningTokensResult.Exists() { | ||
| template, _ = sjson.Set(template, "usage.completion_tokens_details.reasoning_tokens", reasoningTokensResult.Int()) | ||
| upstreamReasoningTokens = reasoningTokensResult.Int() | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -275,10 +290,25 @@ func ConvertCodexResponseToOpenAINonStream(_ context.Context, _ string, original | |
| template, _ = sjson.Set(template, "choices.0.message.role", "assistant") | ||
| } | ||
|
|
||
| // Calculate reasoning tokens | ||
| var reasoningTokens int64 | ||
| if upstreamReasoningTokens > 0 { | ||
| reasoningTokens = upstreamReasoningTokens | ||
| } else if reasoningText != "" { | ||
| // Estimate from reasoning content (approx 4 chars per token) | ||
| reasoningTokens = int64(len(reasoningText) / 4) | ||
| if reasoningTokens == 0 { | ||
| reasoningTokens = 1 | ||
| } | ||
|
Comment on lines
+298
to
+302
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. The magic number // Estimate from reasoning content (approx 4 chars per token)
const defaultCharsPerToken = 4
reasoningTokens = int64(len(reasoningText) / defaultCharsPerToken)
if reasoningTokens == 0 {
reasoningTokens = 1
} |
||
| } | ||
|
|
||
| if reasoningText != "" { | ||
| template, _ = sjson.Set(template, "choices.0.message.reasoning_content", reasoningText) | ||
| template, _ = sjson.Set(template, "choices.0.message.role", "assistant") | ||
| } | ||
| if reasoningTokens > 0 { | ||
| template, _ = sjson.Set(template, "usage.completion_tokens_details.reasoning_tokens", reasoningTokens) | ||
| } | ||
|
|
||
| // Add tool calls if any | ||
| if len(toolCalls) > 0 { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The magic number
4is used for token estimation. It's better to define this as a constant, likeconst 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.