Skip to content

fix(claude): restore max_tokens=1 probe short-circuit compatibility - #3571

Open
lyzgeorge wants to merge 16 commits into
router-for-me:devfrom
lyzgeorge:main
Open

lyzgeorge wants to merge 16 commits into
router-for-me:devfrom
lyzgeorge:main

Conversation

@lyzgeorge

Copy link
Copy Markdown

Summary

  • restore the original Claude probe short-circuit behavior for max_tokens=1 requests
  • keep the OAuth-side context-1m-2025-08-07 silent fallback logic intact
  • preserve the later SSE usage field cleanup from e17b18d

Why

Claude Code uses max_tokens=1 for several probe flows such as API key verification, model validation, and quota preflight. The later predicate tightening caused some valid probe shapes to bypass the local short-circuit and hit upstream, which breaks compatibility for requests that only need an HTTP 200 Claude Messages response.

This PR keeps the working behavior from the earlier branch state while still retaining the separate context-1m-2025-08-07 OAuth downgrade fix.

Notes

  • fork main was first synced to the latest upstream main
  • this PR effectively reverts commits 5e4bd686 and 3da61624 from the feature branch history while keeping d531723 and e17b18d

@github-actions
github-actions Bot changed the base branch from main to dev May 26, 2026 17:10
@github-actions

Copy link
Copy Markdown

This pull request targeted main.

The base branch has been automatically changed to dev.

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

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.

Code Review

This pull request introduces short-circuiting for Claude sample requests (when max_tokens is 1) to return mock responses directly, and strips the context-1m-2025-08-07 beta header for OAuth upstreams to prevent 429 errors. The review feedback suggests flushing the response writer immediately for streaming/SSE responses to prevent buffering issues by intermediate proxies.

Comment thread sdk/api/handlers/claude/code_handlers.go Outdated

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

Copy link
Copy Markdown

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: 50ea891610

ℹ️ 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".

Comment thread sdk/api/handlers/claude/code_handlers.go Outdated

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

Copy link
Copy Markdown

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: 45c767985a

ℹ️ 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".


func isClaudeMaxTokensOneProbe(rawJSON []byte) bool {
maxTokens := gjson.GetBytes(rawJSON, "max_tokens")
return maxTokens.Exists() && maxTokens.Type == gjson.Number && maxTokens.Int() == 1

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 Compare max_tokens without truncating fractional values

When max_tokens is a fractional JSON number such as 1.5, gjson.Result.Int() truncates it to 1, so this predicate misclassifies the request as a probe and returns the synthetic 200 response. This masks the invalid non-integer parameter instead of allowing normal request validation or upstream handling; require the numeric value to be exactly integer 1 before short-circuiting.

Useful? React with 👍 / 👎.

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

Copy link
Copy Markdown

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: a82a83e876

ℹ️ 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".

Comment on lines +16 to +17
cfg.Codex.StreamBootstrapBuffering = true
cfg.Codex.StreamBootstrapTimeout = DefaultCodexBootstrapTimeout

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 Avoid enabling buffering without an enforceable deadline

When a Codex SSE upstream emits an initial bufferable event such as response.created and then sends no further complete line, enabling buffering by default with a 30-second timeout can leave downstream headers uncommitted indefinitely. The timeout in codex_executor_stream.go is checked only after scanner.Scan() returns another line, so it cannot release the already buffered event while the scan is blocked; native Responses clients that previously received response.created immediately can therefore hit reverse-proxy/client read timeouts despite this configured ceiling. Use an asynchronous release mechanism or keep buffering opt-in for this path.

Useful? React with 👍 / 👎.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

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