Skip to content

Conversation

@roomote
Copy link

@roomote roomote bot commented Oct 24, 2025

This PR attempts to address Issue #8800. Feedback and guidance are welcome.

Problem

Claude 4.x models have a hard limit of 20 images maximum in the context window. When users run browser-based test campaigns with more than 20 screenshots, they encounter a fatal error: too many images and documents: 21 + 0 > 20

Solution

Implemented automatic image limit handling that:

  • Adds a maxImages property to model configurations (set to 20 for all Claude/Anthropic models)
  • Counts images in conversation history before API requests
  • Automatically trims oldest images when the limit would be exceeded
  • Shows user-friendly warnings when images are trimmed

Changes

  • Model Configuration: Added maxImages property to ModelInfo type and set it for all Anthropic models
  • Image Handler: Created new image-limit-handler.ts module with functions to count and trim images
  • Task Integration: Modified Task.ts to check and enforce image limits at two critical points:
    • When adding messages to conversation history
    • Before making API requests
  • Testing: Added comprehensive unit tests for the image limit handler

Testing

  • All existing tests pass
  • Added 11 new test cases for image limit handling
  • Tested edge cases including models without image support and undefined limits

Fixes #8800


Important

Adds automatic image limit handling for Claude models, trimming excess images and notifying users, with new tests and integration in task workflow.

  • Behavior:
    • Adds maxImages property to ModelInfo for Anthropic models, set to 20.
    • Automatically trims oldest images in Task.ts when image count exceeds maxImages.
    • Notifies users with warnings when images are trimmed.
  • Image Handling:
    • New image-limit-handler.ts module with countImagesInConversation(), trimImagesFromConversation(), and wouldExceedImageLimit() functions.
  • Testing:
    • Adds 11 new test cases in image-limit-handler.spec.ts for image limit handling.
  • Integration:
    • Modifies Task.ts to enforce image limits when adding messages and before API requests.

This description was created by Ellipsis for 0a99134. You can customize this summary. It will automatically update as commits are pushed.

- Add maxImages property to ModelInfo type and Anthropic models
- Implement automatic image trimming when limit is exceeded
- Add warnings when images are trimmed from conversation
- Add comprehensive tests for image limit handling

Fixes #8800
@roomote roomote bot requested review from cte, jr and mrubens as code owners October 24, 2025 10:28
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. bug Something isn't working labels Oct 24, 2025
@roomote
Copy link
Author

roomote bot commented Oct 24, 2025

Review Summary

I've reviewed the PR and found 2 issues that should be addressed:

Issues to Fix

  • Duplicate image trimming logic - The same image trimming code appears in two locations in Task.ts (lines 606-623 in addToApiConversationHistory and lines 2593-2610 in attemptApiRequest). This could result in duplicate warning messages shown to users and maintenance complexity. Consider consolidating this logic.

  • Unused function - The wouldExceedImageLimit function in image-limit-handler.ts is never called. Either remove this dead code or utilize it for proactive checking before adding content.


View Job

}
}

const warningMessage = `⚠️ Removed ${trimmedCount} image(s) from conversation history to stay within the model's limit of ${maxImages} images. The oldest images were removed first.`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning message is hardcoded in English. Consider using the translation function (e.g. t('...')) for this user‐facing string to support internationalization.

Suggested change
const warningMessage = `⚠️ Removed ${trimmedCount} image(s) from conversation history to stay within the model's limit of ${maxImages} images. The oldest images were removed first.`
const warningMessage = t(`⚠️ Removed ${trimmedCount} image(s) from conversation history to stay within the model's limit of ${maxImages} images. The oldest images were removed first.`)

This comment was generated because it violated a code review rule: irule_C0ez7Rji6ANcGkkX.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Oct 24, 2025
Comment on lines +606 to +623
// Check if we need to trim images after adding the new message
const modelInfo = this.api.getModel().info
if (modelInfo.supportsImages && modelInfo.maxImages) {
const imageCount = countImagesInConversation(this.apiConversationHistory)
if (imageCount > modelInfo.maxImages) {
const trimResult = trimImagesFromConversation(this.apiConversationHistory, modelInfo)

// Update conversation history with trimmed messages
this.apiConversationHistory = trimResult.messages

// Notify user about trimmed images
if (trimResult.warningMessage) {
await this.say("text", trimResult.warningMessage, undefined, false, undefined, undefined, {
isNonInteractive: true,
})
}
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This image trimming logic is duplicated at two locations (here in addToApiConversationHistory at lines 606-623, and again in attemptApiRequest at lines 2593-2610). This duplication creates multiple issues: (1) Users may see duplicate warning messages when images are trimmed since both locations call say() with the warning; (2) Code maintenance becomes more complex as any fixes or improvements need to be applied in two places; (3) The implementations could drift apart over time. Consider consolidating this logic into a single helper method that can be called from both locations, or better yet, perform the trimming only once before the API request is made in attemptApiRequest.

Comment on lines +108 to +132
/**
* Check if adding new content would exceed the image limit
*/
export function wouldExceedImageLimit(
currentMessages: ApiMessage[],
newContent: Anthropic.Messages.ContentBlockParam[],
modelInfo: ModelInfo,
): boolean {
if (!modelInfo.supportsImages || !modelInfo.maxImages) {
return false
}

const currentImageCount = countImagesInConversation(currentMessages)
let newImageCount = 0

if (Array.isArray(newContent)) {
for (const block of newContent) {
if (block.type === "image") {
newImageCount++
}
}
}

return currentImageCount + newImageCount > modelInfo.maxImages
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wouldExceedImageLimit function is never called anywhere in the codebase. This appears to be dead code that was likely intended for proactive checking before adding content, but the implementation uses direct trimming instead. Consider either removing this unused function or utilizing it before adding messages to provide more granular control over when trimming occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

[BUG] No limitation of image count in context window

2 participants