Expand description
Image compression for vision-model content (#1149).
When LLM requests contain base64-encoded images (Anthropic image blocks,
OpenAI image_url with data URIs), this module can reduce their token cost
by adjusting resolution and quality — the visual equivalent of text
compression.
§Strategy
- Detect base64 image content in message arrays.
- Classify the image purpose (screenshot, diagram, photo, code screenshot).
- Route to optimal resize/quality parameters per class:
- Screenshots/diagrams: aggressive resize (high text-signal, low spatial detail)
- Photos: moderate resize (preserve spatial features)
- Code screenshots: OCR-aware resize (keep text readable)
- Re-encode with lower quality JPEG or WebP (for photos) or optimized PNG (for screenshots/diagrams).
§Token economics
Vision tokens are calculated from resolution:
- Anthropic:
(width × height) / 750tokens per image - OpenAI:
170 + (tiles × 85)where tiles = ceil(w/512) × ceil(h/512)
A 1920×1080 screenshot costs ~2765 tokens (Anthropic) or ~850 tokens (OpenAI). Resizing to 1024×576 costs ~786 or ~510 — a 50-72% reduction.
§Configuration
[proxy]
image_compression = true # default: false (opt-in)
image_max_dimension = 1536 # max width or height
image_quality = 75 # JPEG/WebP quality (1-100)
image_min_size_bytes = 50000 # skip images smaller than this§Safety
- Never applied when
detail: "high"is explicitly set by the client. - Never applied to images with
detail: "low"(already minimal tokens). - Original preserved in CCR for retrieval if the model needs more detail.
Structs§
- Image
Compress Result - Result of attempting to compress an image.
- Image
Compression Config - Configuration for image compression.
- Image
Stats - Statistics for monitoring.
Enums§
- Image
Class - Image classification for routing to optimal parameters.
Functions§
- compress_
anthropic_ images - Attempt to compress images in an Anthropic request body.
Mutates
contentblocks in-place. Returns number of images compressed. - compress_
openai_ images - Attempt to compress images in an OpenAI request body.
Handles
image_urlcontent parts with data URIs. - stats
- Snapshot compression statistics.