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(proxy/anthropic): None-guard usage token counts on the direct buffered path (#2434)

Abhay Singh2b5ee7cde809
diff --git a/headroom/proxy/handlers/anthropic.py b/headroom/proxy/handlers/anthropic.py
index 8a17311a5..d19413ac0 100644
--- a/headroom/proxy/handlers/anthropic.py
+++ b/headroom/proxy/handlers/anthropic.py
@@ -3576,13 +3576,13 @@ class AnthropicHandlerMixin:
uncached_input_tokens = 0
if resp_json:
usage = resp_json.get("usage", {})
- output_tokens = usage.get("output_tokens", 0)
- cr_tokens = usage.get("cache_read_input_tokens", 0)
- cw_tokens = usage.get("cache_creation_input_tokens", 0)
+ output_tokens = int(usage.get("output_tokens", 0) or 0)
+ cr_tokens = int(usage.get("cache_read_input_tokens", 0) or 0)
+ cw_tokens = int(usage.get("cache_creation_input_tokens", 0) or 0)
cw_5m_tokens, cw_1h_tokens = self._extract_anthropic_cache_ttl_metrics(
usage
)
- uncached_input_tokens = usage.get("input_tokens", 0)
+ uncached_input_tokens = int(usage.get("input_tokens", 0) or 0)
# Track cache bust: tokens that lost their cache discount due to compression.
# If we had X tokens cached last turn and only Y hit cache this turn,
diff --git a/tests/test_proxy/test_anthropic_buffered_timeout.py b/tests/test_proxy/test_anthropic_buffered_timeout.py
index 69e44e7f5..6e0c753bb 100644
--- a/tests/test_proxy/test_anthropic_buffered_timeout.py
+++ b/tests/test_proxy/test_anthropic_buffered_timeout.py
@@ -110,6 +110,59 @@ def _anthropic_list_response() -> httpx.Response:
)
+def _anthropic_null_usage_response() -> dict[str, object]:
+ """An Anthropic Messages response whose usage counts are present but null.
+
+ An Anthropic-compatible gateway (custom ANTHROPIC_TARGET_API_URL) can emit
+ this shape on a stopped or empty turn — the same class that crashed the
+ Gemini path in #2347.
+ """
+ return {
+ "id": "msg_test_null",
+ "type": "message",
+ "role": "assistant",
+ "content": [{"type": "text", "text": "ok"}],
+ "usage": {
+ "input_tokens": None,
+ "output_tokens": None,
+ "cache_read_input_tokens": None,
+ "cache_creation_input_tokens": None,
+ },
+ }
+
+
+def test_anthropic_messages_buffered_survives_null_usage_counts():
+ config = _make_config()
+ app = create_app(config)
+ with TestClient(app) as client:
+ proxy = client.app.state.proxy
+ # Use the real prefix tracker (its `_cached_token_count` is read on this
+ # path); only pin the session id so the tracker resolves deterministically.
+ proxy.session_tracker_store.compute_session_id = lambda request, model, messages: "s1"
+
+ async def _fake_retry(method, url, headers, body, stream=False, **kwargs): # noqa: ANN001
+ return httpx.Response(200, json=_anthropic_null_usage_response())
+
+ proxy._retry_request = _fake_retry # type: ignore[assignment]
+
+ response = client.post(
+ "/v1/messages",
+ headers={
+ "x-api-key": "test-key",
+ "anthropic-version": "2023-06-01",
+ "content-type": "application/json",
+ },
+ json={
+ "model": "claude-sonnet-4-6",
+ "max_tokens": 64,
+ "messages": [{"role": "user", "content": "hello"}],
+ },
+ )
+
+ # Null usage counts must not crash outcome recording / cache-bust math.
+ assert response.status_code == 200, response.text
+
+
def test_anthropic_messages_buffered_timeout_override_reaches_retry_request():
config = _make_config()
app = create_app(config)