Skip to content

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
anomalyco:devfrom
roderik:fix/reading-too-large-files
Closed

roderik wants to merge 1 commit into
anomalyco:devfrom
roderik:fix/reading-too-large-files

Conversation

@roderik

@roderik roderik commented Aug 30, 2025

Copy link
Copy Markdown
Contributor

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.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant