0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
153 lines
4.2 KiB
Plaintext
153 lines
4.2 KiB
Plaintext
---
|
|
title: Image Compression
|
|
description: ML-powered image compression that reduces vision model token usage by 40-90% while maintaining answer accuracy.
|
|
---
|
|
|
|
Vision models charge by the token, and images are expensive. A single 1024x1024 image costs ~765 tokens on OpenAI. Headroom's image compression uses a trained ML router to analyze your query and automatically select the optimal compression technique, saving 40-90% of image tokens.
|
|
|
|
## How It Works
|
|
|
|
```
|
|
User uploads image + asks question
|
|
|
|
|
[Query Analysis]
|
|
TrainedRouter (MiniLM from HuggingFace)
|
|
Classifies: "What animal is this?" -> full_low
|
|
|
|
|
[Image Analysis]
|
|
SigLIP analyzes image properties
|
|
(has text? complex? fine details?)
|
|
|
|
|
[Apply Compression]
|
|
OpenAI: detail="low"
|
|
Anthropic: Resize to 512px
|
|
Google: Resize to 768px
|
|
|
|
|
Compressed request to LLM
|
|
```
|
|
|
|
The router is a fine-tuned MiniLM classifier (`chopratejas/technique-router` on HuggingFace) with 93.7% accuracy across 1,157 training examples.
|
|
|
|
## Compression Techniques
|
|
|
|
| Technique | Savings | When Used | Example Query |
|
|
|---|---|---|---|
|
|
| `full_low` | ~87% | General understanding | "What is this?", "Describe the scene" |
|
|
| `preserve` | 0% | Fine details needed | "Count the whiskers", "Read the serial number" |
|
|
| `crop` | 50-90% | Region-specific queries | "What's in the corner?", "Focus on the background" |
|
|
| `transcode` | ~99% | Text extraction | "Read the sign", "Transcribe the document" |
|
|
|
|
## Quick Start
|
|
|
|
### With Headroom Proxy (Zero Code Changes)
|
|
|
|
```bash
|
|
# Start the proxy
|
|
headroom proxy --port 8787
|
|
|
|
# Connect your client -- images are compressed automatically
|
|
ANTHROPIC_BASE_URL=http://localhost:8787 claude
|
|
```
|
|
|
|
### With HeadroomClient
|
|
|
|
```python
|
|
from headroom import HeadroomClient
|
|
|
|
client = HeadroomClient(provider="openai")
|
|
|
|
response = client.chat.completions.create(
|
|
model="gpt-4o",
|
|
messages=[{
|
|
"role": "user",
|
|
"content": [
|
|
{"type": "text", "text": "What animal is this?"},
|
|
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,..."}}
|
|
]
|
|
}]
|
|
)
|
|
# Image automatically compressed with detail="low" (87% savings)
|
|
```
|
|
|
|
### Direct API
|
|
|
|
```python
|
|
from headroom.image import ImageCompressor
|
|
|
|
compressor = ImageCompressor()
|
|
|
|
# Compress images in messages
|
|
compressed_messages = compressor.compress(messages, provider="openai")
|
|
|
|
# Check savings
|
|
print(f"Saved {compressor.last_savings:.0f}% tokens")
|
|
print(f"Technique: {compressor.last_result.technique.value}")
|
|
```
|
|
|
|
## Provider Support
|
|
|
|
The compressor adapts its strategy per provider:
|
|
|
|
| Provider | Compression Method | Details |
|
|
|---|---|---|
|
|
| **OpenAI** | Sets `detail="low"` | Native detail parameter |
|
|
| **Anthropic** | Resizes to 512px | PIL-based resize |
|
|
| **Google Gemini** | Resizes to 768px | Optimized for Gemini's 768x768 tile system |
|
|
|
|
### Token Savings by Provider
|
|
|
|
**OpenAI** (1024x1024 image):
|
|
|
|
| Technique | Before | After | Savings |
|
|
|---|---|---|---|
|
|
| `full_low` | 765 tokens | 85 tokens | 89% |
|
|
| `preserve` | 765 tokens | 765 tokens | 0% |
|
|
|
|
**Anthropic** (1024x1024 image):
|
|
|
|
| Before | After | Savings |
|
|
|---|---|---|
|
|
| ~1,398 tokens | ~349 tokens | 75% |
|
|
|
|
**Google Gemini** (1536x1536 image):
|
|
|
|
| Before | After | Savings |
|
|
|---|---|---|
|
|
| 1,032 tokens (4 tiles) | 258 tokens (1 tile) | 75% |
|
|
|
|
## Configuration
|
|
|
|
```python
|
|
from headroom.image import ImageCompressor
|
|
|
|
compressor = ImageCompressor(
|
|
model_id="chopratejas/technique-router", # HuggingFace model
|
|
use_siglip=True, # Enable image analysis
|
|
device="cuda", # Use GPU if available (auto, cuda, cpu, mps)
|
|
)
|
|
```
|
|
|
|
### Proxy Configuration
|
|
|
|
```bash
|
|
# Image optimization is part of the ContentRouter path when the optional image
|
|
# dependencies are installed. There are no public proxy CLI image toggles in
|
|
# the current release.
|
|
headroom proxy
|
|
```
|
|
|
|
## Performance
|
|
|
|
| Metric | Value |
|
|
|---|---|
|
|
| Router inference | ~10ms (CPU), ~2ms (GPU) |
|
|
| Image resize | ~5-20ms |
|
|
| First request | +2-3s (model download, cached after) |
|
|
| Router accuracy | 93.7% |
|
|
| Model size | ~128MB |
|
|
| GPU memory (SigLIP) | ~400MB |
|
|
|
|
<Callout type="info" title="Automatic with the proxy">
|
|
When using the Headroom proxy, image compression happens automatically on every request that contains images. No code changes needed.
|
|
</Callout>
|