Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(utils): cap image binary at 3.75MB so base64 payload stays under Anthropic 5MB limit (#14711)

Arvin Xu5d8d2abe4c3a
diff --git a/packages/utils/src/compressImage.test.ts b/packages/utils/src/compressImage.test.ts
index 22954a2552d..fc8c1bfac05 100644
--- a/packages/utils/src/compressImage.test.ts
+++ b/packages/utils/src/compressImage.test.ts
@@ -119,8 +119,8 @@ describe('constants', () => {
expect(MAX_IMAGE_SIZE).toBe(1920);
});
- it('MAX_IMAGE_BYTES should be 5MB', () => {
- expect(MAX_IMAGE_BYTES).toBe(5 * 1024 * 1024);
+ it('MAX_IMAGE_BYTES should be 3MB (binary cap so base64 stays ~4MB, well under 5MB)', () => {
+ expect(MAX_IMAGE_BYTES).toBe(3 * 1024 * 1024);
});
});
diff --git a/packages/utils/src/compressImage.ts b/packages/utils/src/compressImage.ts
index b798067edb9..7e5ae349c0c 100644
--- a/packages/utils/src/compressImage.ts
+++ b/packages/utils/src/compressImage.ts
@@ -1,5 +1,8 @@
export const MAX_IMAGE_SIZE = 1920;
-export const MAX_IMAGE_BYTES = 5 * 1024 * 1024; // 5MB
+// Anthropic enforces a 5MB cap on the base64-encoded image payload. Base64
+// inflates binary by ~4/3, so a 3MB binary file maps to ~4MB base64 β€” gives
+// comfortable headroom under the 5MB ceiling.
+export const MAX_IMAGE_BYTES = 3 * 1024 * 1024; // 3MB binary β†’ ~4MB base64
export const COMPRESSIBLE_IMAGE_TYPES = new Set(['image/jpeg', 'image/png', 'image/webp']);
diff --git a/src/server/services/aiAgent/ingestAttachment.ts b/src/server/services/aiAgent/ingestAttachment.ts
index 22b285aa4c7..29fbc56ea47 100644
--- a/src/server/services/aiAgent/ingestAttachment.ts
+++ b/src/server/services/aiAgent/ingestAttachment.ts
@@ -9,7 +9,10 @@ const log = debug('lobe-server:file-ingestion');
// --------------- Constants ---------------
const MAX_IMAGE_SIZE = 1920;
-const MAX_IMAGE_BYTES = 5 * 1024 * 1024; // 5MB
+// Anthropic enforces a 5MB cap on the base64-encoded image payload. Base64
+// inflates binary by ~4/3, so a 3MB binary file maps to ~4MB base64 β€” gives
+// comfortable headroom under the 5MB ceiling.
+const MAX_IMAGE_BYTES = 3 * 1024 * 1024; // 3MB binary β†’ ~4MB base64
const COMPRESSIBLE_TYPES = new Set(['image/jpeg', 'image/png', 'image/webp']);
// --------------- Types ---------------