[PR #2328] fix: AI_APICallError: prompt is too long: 209045 tokens > 200000 maximum due to large file read #10278

Closed
opened 2026-02-16 18:14:54 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/2328

State: closed
Merged: No


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.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/2328 **State:** closed **Merged:** No --- ## 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.
yindo added the pull-request label 2026-02-16 18:14:54 -05:00
yindo closed this issue 2026-02-16 18:14:54 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#10278