Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Sep 2, 2025

This PR attempts to address Issue #7598. Feedback and guidance are welcome.

Summary

Adds a configurable context window limit for the Qwen CLI provider to prevent performance issues when token usage exceeds ~200k tokens.

Problem

The Qwen CLI provider currently defaults to a large context window size of 1 million tokens. In practice, when token usage exceeds approximately 200,000 tokens, tasks in Roo Code become very slow and tend to over-engineer outputs, negatively impacting performance.

Solution

This PR introduces a new optional setting qwenCodeMaxContextWindow that allows users to limit the context window size for the Qwen CLI provider. When configured, the provider will use the minimum of the configured limit and the model's default context window.

Changes

  • Added qwenCodeMaxContextWindow setting to provider-settings schema with validation (min: 1000, max: 1,000,000)
  • Updated QwenCodeHandler to apply the context window limit when configured
  • Added comprehensive test coverage for the new functionality

Testing

  • All existing tests pass
  • New tests added to verify:
    • Default behavior (no limit applied)
    • Context window limiting when configured
    • Edge cases (0, negative values, values larger than original)
    • Behavior with different models

Related Issue

Fixes #7598


Important

Adds qwenCodeMaxContextWindow setting to limit context window size for Qwen CLI provider, with validation and tests.

  • Behavior:
    • Adds qwenCodeMaxContextWindow setting to limit context window size for Qwen CLI provider.
    • Applies minimum of configured limit and model's default context window in QwenCodeHandler.
  • Schema:
    • Adds qwenCodeMaxContextWindow to qwenCodeSchema in provider-settings.ts with validation (min: 1000, max: 1,000,000).
  • Testing:
    • New tests in qwen-code.spec.ts for default behavior, context window limiting, and edge cases (0, negative, larger values).
  • Related Issue:

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

- Add qwenCodeMaxContextWindow setting to provider-settings schema
- Update QwenCodeHandler to apply context window limit when configured
- Add comprehensive tests for the new functionality
- Addresses issue #7598 where large context windows cause performance issues
@roomote roomote bot requested review from cte, jr and mrubens as code owners September 2, 2025 20:01
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Sep 2, 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 because apparently I trust no one, not even myself.


const qwenCodeSchema = apiModelIdProviderModelSchema.extend({
qwenCodeOauthPath: z.string().optional(),
qwenCodeMaxContextWindow: z.number().int().min(1000).max(1000000).optional(),
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 the naming pattern intentional? I noticed AWS Bedrock uses awsModelContextWindow for a similar purpose. The "Max" prefix here does make the limiting behavior clearer, but we might want to consider consistency across providers. What do you think?

qwenCodeModels[id as keyof typeof qwenCodeModels] || qwenCodeModels[qwenCodeDefaultModelId]

// Apply custom context window limit if configured
if (this.options.qwenCodeMaxContextWindow && this.options.qwenCodeMaxContextWindow > 0) {
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 adding a debug or info log here when the context window is being limited? It could help users understand when and why their context is being capped:

Suggested change
if (this.options.qwenCodeMaxContextWindow && this.options.qwenCodeMaxContextWindow > 0) {
if (this.options.qwenCodeMaxContextWindow && this.options.qwenCodeMaxContextWindow > 0) {
const originalWindow = info.contextWindow;
info = {
...info,
contextWindow: Math.min(info.contextWindow, this.options.qwenCodeMaxContextWindow),
}
if (info.contextWindow < originalWindow) {
console.debug(`Qwen context window limited from ${originalWindow} to ${info.contextWindow} tokens`);
}
}

expect(info.maxTokens).toBe(qwenCodeModels["qwen3-coder-flash"].maxTokens)
expect(info.description).toBe(qwenCodeModels["qwen3-coder-flash"].description)
})
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great test coverage for the getModel() method! Would it be worth adding a test for the completePrompt method as well to ensure the context window limit is properly applied there too? The method calls getModel() internally, so it should work, but explicit verification never hurts.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Sep 2, 2025
@daniel-lxs
Copy link
Member

Issue needs scoping.

@daniel-lxs daniel-lxs closed this Sep 3, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Sep 3, 2025
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request 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.

Allow Limiting Qwen CLI Provider Context Size in Roo Code

4 participants