mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:10:00 +08:00
fix(proxy): keep an absent Anthropic input_tokens absent in the SSE parser
_parse_sse_usage_from_buffer defaulted a message_start usage without input_tokens to 0, so the finalizer read it as a provider count of 0 and booked 0 uncached instead of falling back to the tokenizer derivation. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -304,7 +304,10 @@ class StreamingMixin:
|
||||
msg = data.get("message", {})
|
||||
msg_usage = msg.get("usage", {})
|
||||
if msg_usage:
|
||||
usage_found["input_tokens"] = msg_usage.get("input_tokens", 0)
|
||||
# Absent stays absent: the finalizer falls back to the
|
||||
# tokenizer derivation only when input_tokens is None.
|
||||
if "input_tokens" in msg_usage:
|
||||
usage_found["input_tokens"] = msg_usage["input_tokens"]
|
||||
usage_found["cache_read_input_tokens"] = msg_usage.get(
|
||||
"cache_read_input_tokens", 0
|
||||
)
|
||||
|
||||
@@ -838,6 +838,20 @@ def test_stream_finalizer_derives_anthropic_uncached_input_without_usage() -> No
|
||||
assert outcome.uncached_input_tokens == 400_000 - 360_949 - 840
|
||||
|
||||
|
||||
def test_sse_parser_leaves_absent_anthropic_input_tokens_absent() -> None:
|
||||
# A message_start usage without input_tokens must not read as a provider
|
||||
# count of 0, or the finalizer books 0 uncached instead of deriving it.
|
||||
proxy = object.__new__(HeadroomProxy)
|
||||
event = {"type": "message_start", "message": {"usage": {"cache_read_input_tokens": 9}}}
|
||||
state = {"sse_buffer": bytearray(f"data: {json.dumps(event)}\n\n".encode())}
|
||||
|
||||
usage = proxy._parse_sse_usage_from_buffer(state, "anthropic")
|
||||
|
||||
assert usage is not None
|
||||
assert "input_tokens" not in usage
|
||||
assert usage["cache_read_input_tokens"] == 9
|
||||
|
||||
|
||||
def test_vertex_gemini_non_text_generate_records_dashboard_outcome() -> None:
|
||||
handler = object.__new__(HeadroomProxy)
|
||||
handler.memory_handler = None
|
||||
|
||||
Reference in New Issue
Block a user