Skip to content

fix(claude): add default max_tokens for models#2389

Merged
luispater merged 2 commits intodevfrom
claude
Mar 29, 2026
Merged

fix(claude): add default max_tokens for models#2389
luispater merged 2 commits intodevfrom
claude

Conversation

@hkfires
Copy link
Copy Markdown
Collaborator

@hkfires hkfires commented Mar 29, 2026

No description provided.

Copy link
Copy Markdown
Contributor

@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 mechanism to ensure that the max_tokens field is present in requests for Claude models, preventing potential crashes or rejections from upstream APIs. A helper function, ensureModelMaxTokens, was added and integrated into both the synchronous and streaming execution paths. Feedback was provided to enhance the robustness of this check by verifying that max_tokens is a positive integer rather than merely checking for its existence.

Comment on lines +61 to +63
if maxTokens := gjson.GetBytes(body, "max_tokens"); maxTokens.Exists() {
return body
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The current check for max_tokens only verifies its existence. If max_tokens is present but set to 0, null, or another non-positive value, the function will not set the default value. This could lead to the upstream API rejecting the request or crashing, which this function aims to prevent.

To make this more robust, the check should also ensure that max_tokens is a positive integer before returning.

Suggested change
if maxTokens := gjson.GetBytes(body, "max_tokens"); maxTokens.Exists() {
return body
}
if maxTokens := gjson.GetBytes(body, "max_tokens"); maxTokens.Exists() && maxTokens.Int() > 0 {
return body
}

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 145e0e0b5d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".


for _, provider := range registry.GetGlobalRegistry().GetModelProviders(strings.TrimSpace(modelID)) {
if strings.EqualFold(provider, "claude") {
body, _ = sjson.SetBytes(body, "max_tokens", defaultModelMaxTokens)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Derive max_tokens from thinking budget

ensureModelMaxTokens unconditionally injects max_tokens=1024 when the field is absent, but both Execute and ExecuteStream call it after thinking.ApplyThinking, which may already set thinking.budget_tokens to values much larger than 1024 (for example from a model suffix like (8192)). Anthropic requests require max_tokens to be greater than the thinking budget, so this change can turn previously valid thinking-enabled requests into deterministic 400 errors for dynamically registered Claude models.

Useful? React with 👍 / 👎.

@luispater luispater merged commit 1587ff5 into dev Mar 29, 2026
2 checks passed
@luispater luispater deleted the claude branch March 29, 2026 05:03
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f033d3a6df

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

for _, provider := range registry.GetGlobalRegistry().GetModelProviders(strings.TrimSpace(modelID)) {
if strings.EqualFold(provider, "claude") {
maxTokens := defaultModelMaxTokens
if info := registry.GetGlobalRegistry().GetModelInfo(strings.TrimSpace(modelID), "claude"); info != nil && info.MaxCompletionTokens > 0 {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Resolve max_tokens from the rewritten model ID

ensureModelMaxTokens looks up MaxCompletionTokens using the modelID argument instead of the request’s final body.model, but this function is called after applyPayloadConfigWithRoot, which can override model. In configurations that remap models via payload rules, this can inject a max_tokens value from the old model (too high or too low for the rewritten one), causing avoidable Anthropic validation errors or unintended truncation.

Useful? React with 👍 / 👎.

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.

2 participants