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.

Fix image token counting: Anthropic images counted as text tokens

chopratejasdf664d5ad95f
diff --git a/headroom/tokenizers/base.py b/headroom/tokenizers/base.py
index 6e0a5745f..3b449c0ac 100644
--- a/headroom/tokenizers/base.py
+++ b/headroom/tokenizers/base.py
@@ -132,8 +132,11 @@ class BaseTokenizer(ABC):
if part_type == "text":
total += self.count_text(part.get("text", ""))
- elif part_type == "image_url":
- # Images have fixed token cost (varies by model)
+ elif part_type in ("image_url", "image"):
+ # Images have fixed token cost — NOT tokenized as text.
+ # Anthropic images are type="image" with base64 in source.data.
+ # Without this, the base64 string gets json.dumps'd and counted
+ # as text tokens (1MB image = ~330K fake tokens).
total += 85 # Base image token count
elif part_type == "tool_result":
content = part.get("content", "")