# Enforce 5MB per-image limit when converting images for language models \(\#45313\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/b091cc4d9a42773e536960ffc0f2393775158bf9)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: b091cc4d9a42773e536960ffc0f2393775158bf9

Requested commit: b091cc4d9a42773e536960ffc0f2393775158bf9

Commit: b091cc4d9a42773e536960ffc0f2393775158bf9

Tree: 211281a415ee1f6e104afc81ba00f007a4c7300f

Author: Nathan Sobo

Committer: GitHub

## Message

```
Enforce 5MB per-image limit when converting images for language models (#45313)

## Problem

When users paste or drag large images into the agent panel, the encoded
payload can exceed upstream provider limits (e.g., Anthropic's 5MB
per-image limit), causing API errors.

## Solution

Enforce a default 5MB limit on encoded PNG bytes in
`LanguageModelImage::from_image`:

1. Apply existing Anthropic dimension limits first (1568px max in either
dimension)
2. Iteratively downscale by ~15% per pass until the encoded PNG is under
5MB
3. Return `None` if the image can't be shrunk within 8 passes
(fail-safe)

The limit is enforced at the `LanguageModelImage` conversion layer,
which is the choke point for all image ingestion paths (agent panel
paste/drag, file mentions, text threads, etc.).

## Future Work

The 5MB limit is a conservative default. Provider-specific limits can be
introduced later by adding a `from_image_with_constraints` API.

## Testing

Added a regression test that:
1. Generates a noisy 4096x4096 PNG (guaranteed >5MB)
2. Converts it via `LanguageModelImage::from_image`
3. Asserts the result is ≤5MB and was actually downscaled

---

**Note:** This PR builds on #45312 (prompt store fail-open fix). Please
merge that first.

cc @rtfeldman

---------

Co-authored-by: Zed Zippy <234243425+zed-zippy[bot]@users.noreply.github.com>
```

## Parents

- [8e5d33ebc6093c6fd2dbcd4dc3a5e0c35a15ec49](https://git.cafe/gitcafe/zed/commit/8e5d33ebc6093c6fd2dbcd4dc3a5e0c35a15ec49?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/b091cc4d9a42773e536960ffc0f2393775158bf9?format=markdown)
