Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Aug 19, 2025

This PR fixes an issue where the Claude Sonnet 4 1M context window beta feature was not working correctly, causing 429 rate limit errors at ~239K tokens even for Tier 4 users.

Problem

Users on Tier 4 were experiencing 429 rate limit errors when using Claude Sonnet 4 with the 1M context window beta enabled, hitting the error at around 239K tokens instead of being able to use the full 1M context window.

Solution

The issue was in how the beta headers were being handled in the AnthropicHandler:

  • The betas array was being initialized from the model's betas which could be mutated
  • Beta headers were not being properly deduplicated
  • The combination of multiple beta headers might not have been working correctly

Changes

  • Fixed beta header array initialization to prevent mutation of model betas by creating a proper copy
  • Added deduplication checks to prevent duplicate beta headers
  • Ensured the context-1m-2025-08-07 beta header is properly combined with the prompt caching beta
  • Added comprehensive tests for the 1M context window feature

Testing

  • Added unit tests to verify the 1M context window is properly enabled when the flag is set
  • Added tests to verify the beta headers are correctly combined
  • All existing tests continue to pass

Fixes #7229


Important

Fixes beta header handling in AnthropicHandler for Claude Sonnet 4 1M context window, ensuring proper initialization and deduplication.

  • Behavior:
    • Fixes beta header handling in AnthropicHandler to prevent mutation of model betas and ensure proper deduplication.
    • Ensures context-1m-2025-08-07 beta header is combined with prompt caching beta.
  • Testing:
    • Adds unit tests in anthropic.spec.ts to verify 1M context window is enabled when anthropicBeta1MContext is true.
    • Tests added to ensure correct combination of beta headers.
  • Misc:
    • Updates createMessage in anthropic.ts to initialize beta headers correctly and add deduplication checks.

This description was created by Ellipsis for 5006092. You can customize this summary. It will automatically update as commits are pushed.

- Fixed beta header array initialization to prevent mutation of model betas
- Added proper deduplication of beta headers to avoid duplicates
- Ensured context-1m-2025-08-07 beta is properly combined with prompt caching beta
- Added comprehensive tests for 1M context window feature

Fixes #7229
@roomote roomote bot requested review from cte, jr and mrubens as code owners August 19, 2025 20:13
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Aug 19, 2025
Copy link
Contributor Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

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

Reviewing my own code is like debugging in a mirror - everything looks backwards but the bugs are still mine.

betas.push("prompt-caching-2024-07-31")
}
// Only set headers if we have betas to include
return betas.length > 0 ? { headers: { "anthropic-beta": betas.join(",") } } : undefined
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is returning undefined here intentional when betas.length is 0? The API might expect headers to always be present. Could we consider returning an empty headers object instead to avoid potential issues?

let { id: modelId, betas: modelBetas, maxTokens, temperature, reasoning: thinking } = this.getModel()

// Initialize betas array properly
const betas: string[] = modelBetas ? [...modelBetas] : []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could we extract this beta header initialization and deduplication logic into a separate method? Something like prepareBetaHeaders(modelBetas, modelId) would make this more maintainable and easier to extend for future beta features.

let { id: modelId, betas: modelBetas, maxTokens, temperature, reasoning: thinking } = this.getModel()

// Initialize betas array properly
const betas: string[] = modelBetas ? [...modelBetas] : []
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call on creating a copy here to prevent mutation! Might be worth adding a comment explaining why we need to copy modelBetas to prevent mutating the original model configuration.

if (modelId === "claude-sonnet-4-20250514" && this.options.anthropicBeta1MContext) {
betas.push("context-1m-2025-08-07")
// Only add if not already present
if (!betas.includes("context-1m-2025-08-07")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consider using a Set for handling beta headers instead of checking with includes() and pushing. It would be more efficient and cleaner:

Suggested change
if (!betas.includes("context-1m-2025-08-07")) {
const betaSet = new Set(modelBetas || []);
if (modelId === "claude-sonnet-4-20250514" && this.options.anthropicBeta1MContext) {
betaSet.add("context-1m-2025-08-07");
}
const betas = Array.from(betaSet);

})
})

describe("createMessage with 1M context beta", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could we add a test case to verify behavior when the beta header is already present in the model's betas array? This would ensure our deduplication logic works correctly and prevent duplicate headers from being sent.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 19, 2025
@daniel-lxs daniel-lxs closed this Aug 19, 2025
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Aug 19, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Claude Sonnet 4 - 1 Million Context Window

4 participants