Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

Merge remote-tracking branch 'upstream/main' into fix/anthropic-prefix-cache-stability

JerrettDavisd78fdfe02d45
diff --cc headroom/proxy/handlers/anthropic.py
index 37b236fbb,d32e49e7a..515516901
--- a/headroom/proxy/handlers/anthropic.py
+++ b/headroom/proxy/handlers/anthropic.py
@@@ -211,6 -106,25 +210,9 @@@ class AnthropicHandlerMixin
if _bypass:
logger.info(f"[{request_id}] Bypass: skipping compression (header)")
- # TODO: Re-enable image compression once token counting is accurate.
- # Image compression was disabled because the tokenizer counted base64
- # image data as text tokens (330K per 1MB image), inflating savings by
- # 3-4x. The compressor itself works, but reported metrics were wrong.
- # To re-enable: fix tokenizer to extract image dimensions and use
- # Anthropic's formula (width*height/750), then uncomment below.
- #
- # if self.config.image_optimize and messages and not _bypass:
- # compressor = _get_image_compressor()
- # if compressor and compressor.has_images(messages):
- # messages = compressor.compress(messages, provider="anthropic")
- # if compressor.last_result:
- # logger.info(
- # f"Image compression: {compressor.last_result.technique.value} "
- # f"({compressor.last_result.savings_percent:.0f}% saved, "
- # f"{compressor.last_result.original_tokens} -> "
- # f"{compressor.last_result.compressed_tokens} tokens)"
- # )
-
++ # NOTE: Upstream temporarily disabled broad image compression due to
++ # token-counting inaccuracies. We only compress the latest non-frozen
++ # user turn later in this handler to preserve Anthropic prefix caching.
# Extract headers and tags
headers = dict(request.headers.items())
headers.pop("host", None)