-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: add custom model context window override for all providers #8398
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
Conversation
- Add modelContextWindow field to base provider settings schema - Implement applyModelOverrides method in BaseProvider class - Update all provider implementations to apply context window override - Add comprehensive tests for context window override functionality This allows users to customize the context window size for any provider to work around corporate upload limits or other restrictions. Fixes #8397
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.
Self-review: evaluating my own code with the detached optimism of a robot auditing its own firmware.
|
|
||
| // Apply context window override if specified | ||
| if (options.modelContextWindow && options.modelContextWindow > 0) { | ||
| overriddenInfo.contextWindow = options.modelContextWindow |
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.
[P0] If modelContextWindow is smaller than the model's maxTokens, downstream calls may pass an invalid max_tokens > contextWindow to providers. Suggest clamping maxTokens to contextWindow after applying the override to maintain the invariant maxTokens ≤ contextWindow.
| overriddenInfo.contextWindow = options.modelContextWindow | |
| protected applyModelOverrides(info: ModelInfo, options: ApiHandlerOptions): ModelInfo { | |
| const overriddenInfo = { ...info } | |
| // Apply context window override if specified | |
| if (options.modelContextWindow && options.modelContextWindow > 0) { | |
| overriddenInfo.contextWindow = options.modelContextWindow | |
| // Keep invariant: maxTokens ≤ contextWindow | |
| if ( | |
| overriddenInfo.maxTokens && | |
| overriddenInfo.contextWindow && | |
| overriddenInfo.maxTokens > overriddenInfo.contextWindow | |
| ) { | |
| overriddenInfo.maxTokens = overriddenInfo.contextWindow | |
| } | |
| } | |
| return overriddenInfo | |
| } |
| modelMaxThinkingTokens: z.number().optional(), | ||
|
|
||
| // Model context window override. | ||
| modelContextWindow: z.number().optional(), |
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.
[P1] Schema allows floats/negatives; tighten to non-negative integers to reflect expected semantics and avoid silent float inputs.
| modelContextWindow: z.number().optional(), | |
| // Model context window override. | |
| modelContextWindow: z.number().int().nonnegative().optional(), |
| @@ -0,0 +1,126 @@ | |||
| import { describe, it, expect, beforeEach } from "vitest" | |||
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.
[P2] beforeEach is imported but unused; remove to keep tests clean.
| import { describe, it, expect, beforeEach } from "vitest" | |
| import { describe, it, expect } from "vitest" |
| expect(model.info.contextWindow).toBe(200000) | ||
| }) | ||
| }) | ||
| }) |
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.
[P2] Consider adding cases for AwsBedrockHandler and RouterProvider to guard the new override paths you added in those files (and any other provider base classes used). This will help catch regressions if provider wiring changes.
Summary
This PR addresses Issue #8397 by adding support for custom model parameter overrides, specifically the context window size, across all providers.
Problem
Users working in corporate environments with upload size restrictions need the ability to customize the context window size to prevent exceeding intranet upload limits.
Solution
modelContextWindowfield to the base provider settings schemaapplyModelOverridesmethod in the BaseProvider classChanges
BaseProviderclass that all providers inheritTesting
Usage
Users can now configure the context window size in their provider settings:
{ "apiProvider": "anthropic", "apiKey": "...", "modelContextWindow": 50000 // Custom context window size }Future Enhancements
Fixes #8397
Important
Adds support for user-configurable context window size overrides across all model providers.
modelContextWindowfield to provider settings schema inprovider-settings.ts.applyModelOverrides()inBaseProviderto apply context window overrides.getModel()inAnthropicHandler,OpenAiHandler,GeminiHandler,OpenRouterHandler,AwsBedrockHandler, andRouterProviderto useapplyModelOverrides().context-window-override.spec.tsforAnthropicHandler,OpenRouterHandler,OpenAiHandler, andGeminiHandlerto verify context window override functionality.This description was created by
for 74a1d9c. You can customize this summary. It will automatically update as commits are pushed.