Repository navigation
fix: AI_APICallError: prompt is too long: 209045 tokens > 200000 maximum due to large file read - #2328
Closed
roderik wants to merge 1 commit into
Closed
fix: AI_APICallError: prompt is too long: 209045 tokens > 200000 maximum due to large file read#2328roderik wants to merge 1 commit into
roderik wants to merge 1 commit into
Conversation
…mum due to large file read ## Summary of Changes The code was attempting to read files that were too large to fit in the model's context window, resulting in "prompt is too long" errors. We fixed this by: ### 1. Added Token Estimation Utility (packages/opencode/src/util/token.ts) • Created a conservative token estimation algorithm • Added truncation logic that breaks at natural boundaries (lines/words) • Handles both UTF-8 and base64 encoded content ### 2. Pre-emptive File Size Checking (packages/opencode/src/session/index.ts) • Load model early to get its actual context limit • Check file size before including it in messages • Use 75% of context limit for files (leaving room for system prompts and conversation) • Automatically truncate large files with informative messages • Pass model-specific limits to the Read tool ### 3. Updated Read Tool (packages/opencode/src/tool/read.ts) • Accept dynamic token limits from the model context • Provide helpful error messages suggesting offset/limit parameters for large files • Calculate suggested limits based on actual file size and model capacity ### Key Improvements: • Dynamic Limits: Uses each model's actual context window instead of hardcoded values • Conservative Approach: Uses 75% of available context for files to ensure safety • Graceful Degradation: Truncates content rather than failing completely • Informative Messages: Tells users exactly why truncation occurred and the limits involved • Model-Aware: Different models with different context windows are handled appropriately The fix ensures that files are checked before being added to messages, preventing the API error from occurring in the first place.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of Changes
The code was attempting to read files that were too large to fit in the model's context window, resulting in "prompt is too long" errors. We fixed this by:
1. Added Token Estimation Utility (packages/opencode/src/util/token.ts)
• Created a conservative token estimation algorithm • Added truncation logic that breaks at natural boundaries (lines/words) • Handles both UTF-8 and base64 encoded content
2. Pre-emptive File Size Checking (packages/opencode/src/session/index.ts)
• Load model early to get its actual context limit • Check file size before including it in messages
• Use 75% of context limit for files (leaving room for system prompts and conversation) • Automatically truncate large files with informative messages • Pass model-specific limits to the Read tool
3. Updated Read Tool (packages/opencode/src/tool/read.ts)
• Accept dynamic token limits from the model context • Provide helpful error messages suggesting offset/limit parameters for large files • Calculate suggested limits based on actual file size and model capacity
Key Improvements:
• Dynamic Limits: Uses each model's actual context window instead of hardcoded values
• Conservative Approach: Uses 75% of available context for files to ensure safety
• Graceful Degradation: Truncates content rather than failing completely
• Informative Messages: Tells users exactly why truncation occurred and the limits involved
• Model-Aware: Different models with different context windows are handled appropriately
The fix ensures that files are checked before being added to messages, preventing the API error from occurring in the first place.