-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: prevent LM Studio context overflow by limiting maxTokens to 20% of context window #7389
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
- Fix context limit crash issue by properly calculating maxTokens - Previously maxTokens was set equal to contextWindow causing overflow - Now maxTokens is calculated as 20% of contextWindow to leave room for input - Add test coverage for the new calculation logic Fixes #7388
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.
Reviewing my own code because apparently I trust no one, not even myself.
| // Calculate maxTokens as 20% of context window to prevent context overflow | ||
| // This ensures there's always room for input tokens and prevents crashes | ||
| // when approaching the context limit | ||
| const maxOutputTokens = Math.ceil(contextLength * 0.2) |
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.
Could we consider making this ratio configurable? While 20% is a reasonable default that matches other providers, some users might want to adjust this based on their specific use cases. Perhaps a setting like lmstudio.maxOutputRatio with a default of 0.2?
| // Calculate maxTokens as 20% of context window to prevent context overflow | ||
| // This ensures there's always room for input tokens and prevents crashes | ||
| // when approaching the context limit | ||
| const maxOutputTokens = Math.ceil(contextLength * 0.2) |
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.
For models with very small context windows (e.g., < 1000 tokens), this 20% calculation might result in very limited output capacity. Should we consider adding a minimum threshold? Something like:
| const maxOutputTokens = Math.ceil(contextLength * 0.2) | |
| // Calculate maxTokens as 20% of context window to prevent context overflow | |
| // This ensures there's always room for input tokens and prevents crashes | |
| // when approaching the context limit | |
| const calculatedMaxTokens = Math.ceil(contextLength * 0.2) | |
| // Ensure a minimum of 200 tokens for very small context windows | |
| const maxOutputTokens = Math.max(calculatedMaxTokens, Math.min(200, contextLength)) |
| { contextLength: 8192, expectedMaxTokens: Math.ceil(8192 * 0.2) }, // 1639 | ||
| { contextLength: 128000, expectedMaxTokens: Math.ceil(128000 * 0.2) }, // 25600 | ||
| { contextLength: 200000, expectedMaxTokens: Math.ceil(200000 * 0.2) }, // 40000 | ||
| ] |
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.
Great test coverage! The test cases cover a good range of context sizes. Consider also adding a test case for very small context windows (e.g., 512 tokens) to ensure the calculation works correctly at the lower bounds.
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.
So when to expect this to be merged in next update?
|
So when to expect this to be merged in next update? |
Summary
This PR fixes the LM Studio crash issue when the context reaches 128K tokens despite having a 200K limit configured.
Problem
Users were experiencing crashes with LM Studio when the context reached 128K tokens, even though they had configured a 200K context limit. The error message was:
Root Cause
The issue was in the
parseLMStudioModelfunction wheremaxTokens(maximum output tokens) was being set equal tocontextWindow. This meant the model was trying to reserve the entire context window for output, leaving no room for input tokens, causing an overflow.Solution
maxTokensas 20% of thecontextWindowinstead of using the full context sizeChanges
parseLMStudioModelfunction to calculatemaxTokens = Math.ceil(contextLength * 0.2)Testing
Fixes #7388
Important
Fixes LM Studio crash by setting
maxTokensto 20% ofcontextWindowinparseLMStudioModel, ensuring space for input tokens.maxTokensto 20% ofcontextWindowinparseLMStudioModel.lmstudio.test.tsfor variouscontextLengthvalues to verifymaxTokenscalculation.lmstudio.tsexplainingmaxTokenscalculation rationale.This description was created by
for 8119af6. You can customize this summary. It will automatically update as commits are pushed.