-
-
Notifications
You must be signed in to change notification settings - Fork 70
fix: ensure MaxTokens >= budget when extended thinking is enabled #48
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -169,6 +169,14 @@ func reasoningEffortToBudgetTokens(effort string) int { | |
| } | ||
| } | ||
|
|
||
| // logMaxTokensAdjustment logs when MaxTokens is adjusted to meet Anthropic requirements | ||
| func logMaxTokensAdjustment(original, adjusted int, reason string) { | ||
| slog.Info("MaxTokens adjusted to meet Anthropic extended thinking requirements", | ||
| "original", original, | ||
| "adjusted", adjusted, | ||
| "reason", reason) | ||
| } | ||
|
|
||
| // convertToAnthropicRequest converts core.ChatRequest to Anthropic format | ||
| func convertToAnthropicRequest(req *core.ChatRequest) *anthropicRequest { | ||
| anthropicReq := &anthropicRequest{ | ||
|
|
@@ -185,9 +193,16 @@ func convertToAnthropicRequest(req *core.ChatRequest) *anthropicRequest { | |
|
|
||
| // Map reasoning effort to Anthropic extended thinking | ||
| if req.Reasoning != nil && req.Reasoning.Effort != "" { | ||
| budget := reasoningEffortToBudgetTokens(req.Reasoning.Effort) | ||
| anthropicReq.Thinking = &anthropicThinking{ | ||
| Type: "enabled", | ||
| BudgetTokens: reasoningEffortToBudgetTokens(req.Reasoning.Effort), | ||
| BudgetTokens: budget, | ||
| } | ||
| // Ensure MaxTokens is at least the budget tokens | ||
| if anthropicReq.MaxTokens < budget { | ||
| logMaxTokensAdjustment(anthropicReq.MaxTokens, budget, | ||
| "extended thinking budget_tokens must be <= max_tokens") | ||
| anthropicReq.MaxTokens = budget | ||
|
Contributor
Author
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. @copilot Add some logs here - that MaxTokens has been increased because ... Anthropic require etc... |
||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| // Extended thinking requires temperature to be unset (defaults to 1) | ||
| if anthropicReq.Temperature != nil { | ||
|
|
@@ -509,9 +524,16 @@ func convertResponsesRequestToAnthropic(req *core.ResponsesRequest) *anthropicRe | |
|
|
||
| // Map reasoning effort to Anthropic extended thinking | ||
| if req.Reasoning != nil && req.Reasoning.Effort != "" { | ||
| budget := reasoningEffortToBudgetTokens(req.Reasoning.Effort) | ||
| anthropicReq.Thinking = &anthropicThinking{ | ||
| Type: "enabled", | ||
| BudgetTokens: reasoningEffortToBudgetTokens(req.Reasoning.Effort), | ||
| BudgetTokens: budget, | ||
| } | ||
| // Ensure MaxTokens is at least the budget tokens | ||
| if anthropicReq.MaxTokens < budget { | ||
| logMaxTokensAdjustment(anthropicReq.MaxTokens, budget, | ||
| "extended thinking budget_tokens must be <= max_tokens") | ||
| anthropicReq.MaxTokens = budget | ||
| } | ||
| // Extended thinking requires temperature to be unset (defaults to 1) | ||
| if anthropicReq.Temperature != nil { | ||
|
|
||
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
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.
@copilot, @coderabbitai Do we need a checking against - req.Reasoning.Effort not set / nil ?
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.
@SantiagoDePoloniaThe current checking is already sufficient. Here's why:The condition
if req.Reasoning != nil && req.Reasoning.Effort != ""properly handles all cases:req.Reasoning != nil- This prevents a nil pointer dereference by checking if the Reasoning object exists firstreq.Reasoning.Effort != ""- This checks if Effort is set (non-empty)Since Go uses short-circuit evaluation with
&&, ifreq.Reasoningis nil, the second condition won't be evaluated, preventing any panic.Regarding
Effortspecifically: Based on the usage pattern (comparing with""),Effortappears to be astringtype, not a*string. In Go:""(empty string) when not set!= ""already handles the "not set" caseConclusion: No additional nil checking is needed. The current implementation correctly handles:
req.Reasoningbeing nilreq.Reasoning.Effortbeing empty/not set